def test04_get_only_valid_requests(self): """ - create a lot of requests marked as 'expired' - wait some time - create another lot of requests marked as 'valid' -> check if only 'valid' requests present """ clock = Clock() sessions = Sessions(False, 10, clock) collector = Collector(sessions) dl = [] for i in xrange(10): d = collector.queue_and_process("192.168.45.12", "expired") dl.append(d) clock.advance(15) for i in xrange(10): d = collector.queue_and_process("192.168.45.12", "valid") dl.append(d) dfl = DeferredList(dl) @dfl.addCallback def get_result(ignored): for i in xrange(10): uid, ip, request = collector.get() self.assertEqual(request, "valid")
def test08_check_for_expired(self): """ - create some tasks - wait some time - create another lot of tasks - wait some time -> check if only second lot of tasks present """ sessions = Sessions(False, 10, self.clock) to_expire = [] to_stay = [] def add_elements(expire): print for i in range(10): uid, d = sessions.make() if expire: to_expire.append(uid) else: to_stay.append(uid) def check_result(): result = all([uid not in to_expire for uid in sessions.content]) self.assertTrue(result) add_elements(True) self.clock.advance(15) add_elements(False) result = all([uid not in to_expire for uid in sessions.content]) self.assertTrue(result)
def test03_make_echo_with_collector(self): """ Test with correct handler used in the package. - Get the request from server - Sending the same response """ clock = Clock() sessions = Sessions(False, 10, clock) trigger = DummyTrigger() factory = GatheringFactory() collector = Collector(sessions) factory.protocol.set_handler(collector) factory.protocol.set_trigger(trigger) protocol = factory.buildProtocol(("127.0.0.1", 0)) transport = StringTransport() protocol.makeConnection(transport) protocol.dataReceived("hello") uid, ip, data = collector.get() collector.release(uid, data) self.assertEqual(transport.value(), "hello")
def test07_expire(self): """ test if expired requests is no more in sessions """ # create sessions instance with specific result sessions = Sessions("I'm an expired request", 10, self.clock) uids = [] for i in xrange(10): uid, d = sessions.make() @d.addCallback def cb(reason): """ Callback of all expired requests """ # All expired requests must return this text self.assertEqual(reason, "I'm an expired request") uids.append(uid) for uid in uids: sessions._expire(uid) result = all([uid not in uids for uid in sessions.content]) # verify if all expired uis is no more in sessions content self.assertTrue(result)
def setUp(self): self.clock = Clock() self.sessions = Sessions(False, 10, self.clock)
class Test00_SessionsTestCase(TestCase): """ Test for sessions tracker""" def setUp(self): self.clock = Clock() self.sessions = Sessions(False, 10, self.clock) def test01_all_elements_are_deferred(self): """ To be sure if all elements are deferred instances""" for i in xrange(20): uid, d = self.sessions.make() result = all([isinstance(d[1], Deferred) for d in self.sessions.content.values()]) self.assertTrue(result) def test02_exists(self): """ An occurence test of added element in container """ uid, d = self.sessions.make() self.assertIn(uid, self.sessions) def test03_not_exists_if_removed(self): """ An occurence test of removed element from container """ uid, d = self.sessions.make() self.sessions.remove(uid) self.assertNotIn(uid, self.sessions) def test04_not_exists_if_pop(self): """ An occurence test of returned and removed element from container """ uid, d = self.sessions.make() self.sessions.pop(uid) self.assertNotIn(uid, self.sessions) def test05_try_remove_unexisting(self): """ An occurence test of removed element from container """ uid, d = self.sessions.make() self.sessions.remove(uid) self.assertRaises(SessionNotFound, self.sessions.remove, uid) self.assertRaises(SessionNotFound, self.sessions.get, uid) self.assertRaises(SessionNotFound, self.sessions.pop, uid) def test06_is_same_deferred_reference(self): """ - create a session and get its uid and deferred - get and remove its referrence (returning of deferred) - deferred is fired -> test if it is always the same deferred """ uid, d_before = self.sessions.make() d_after = self.sessions.pop(uid) # an action on d_before deferred d_before.callback(True) self.assertTrue(d_before is d_after) def test07_expire(self): """ test if expired requests is no more in sessions """ # create sessions instance with specific result sessions = Sessions("I'm an expired request", 10, self.clock) uids = [] for i in xrange(10): uid, d = sessions.make() @d.addCallback def cb(reason): """ Callback of all expired requests """ # All expired requests must return this text self.assertEqual(reason, "I'm an expired request") uids.append(uid) for uid in uids: sessions._expire(uid) result = all([uid not in uids for uid in sessions.content]) # verify if all expired uis is no more in sessions content self.assertTrue(result) def test08_check_for_expired(self): """ - create some tasks - wait some time - create another lot of tasks - wait some time -> check if only second lot of tasks present """ sessions = Sessions(False, 10, self.clock) to_expire = [] to_stay = [] def add_elements(expire): print for i in range(10): uid, d = sessions.make() if expire: to_expire.append(uid) else: to_stay.append(uid) def check_result(): result = all([uid not in to_expire for uid in sessions.content]) self.assertTrue(result) add_elements(True) self.clock.advance(15) add_elements(False) result = all([uid not in to_expire for uid in sessions.content]) self.assertTrue(result)
def setUp(self): self.sessions = Sessions(False, 10, Clock())