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)
def setUp(self): self.sessions = Sessions(False, 10, Clock())