def test_backlog_different_ttls(self): """ Test if the backlog discards requests whos ttl have expired but keeps others. """ conn = MockedConnection() chan = Channel(conn) with patch_time() as time: conn.mock_connection_ready(False) chan.request('name', 'payload0').send(ttl=2.0) chan.request('name', 'payload1').send(ttl=4.0) chan.request('name', 'payload2').send(ttl=2.0) chan.request('name', 'payload3').send(ttl=4.0) time.sleep(3.0) conn.mock_connection_ready(True) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload1' )) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload3' ))
def test_backlog_no_ttl(self): """ Test if the backlog discards requests with no ttl at all. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(False) chan.request('name', 'payload0').send(ttl=0) chan.request('name', 'payload1').send(ttl=4.0) chan.request('name', 'payload2').send(ttl=0) chan.request('name', 'payload3').send(ttl=4.0) conn.mock_connection_ready(True) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload1' )) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload3' ))
def __init__(self, core, name, payload, timeout, ttl, handlers): self.core = core self.name = name self.payload = payload self.timeout = timeout self.ttl = ttl self.handlers = handlers unidirectional = not bool(self.handlers) # fetch messageref, but only if the request is bidirectional if unidirectional: self.messageref = None else: self.messageref = self.core.new_messageref(self.__on_message) # create the verb and send it upstream self.verb = verbs.RequestVerb( name=encode_name(self.name), unidirectional=unidirectional, messageref=self.messageref, timeout=self.timeout, payload=self.core.encode_payload(self.payload), ) self.core.put_upstream(self.verb, ttl=self.ttl)
def test_backlog_order(self): """ Test if the order of requests is preserved when queued in the backlog. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(False) chan.request('name', 'payload0').send(ttl=5.0) chan.request('name', 'payload1').send(ttl=5.0) chan.request('name', 'payload2').send(ttl=5.0) conn.mock_connection_ready(True) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload0' )) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload1' )) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload2' ))
def test_request_1(self): """ Test if a request verb is pushed when the connection is ready immediately. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(True) chan.request('name', 'payload').send() conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload' ))
def test_request_2(self): """ Test if the pushed request verb is unidirectional and has no messageref when no handlers were set. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(True) req = chan.request('name', 'payload') req.send() conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload' ))
def test_request_3(self): """ Test if the pushed request verb is directional and has a messsageref when a handler is set. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(True) req = chan.request('name', 'payload') req.add_handler(Mock()) req.send() conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=False, messageref=1, timeout=5.0, payload=b'payload' ))
def test_request_2(self): """ Test that a request packet is send. """ mock = Sysmock() mock.system.add_unused_local_address(CLIENT) with patch(mock): loop = Mainloop() mock.expect_tcp_syn(CLIENT, SERVER) mock.do_tcp_syn_ack(SERVER, CLIENT) mock.do_tcp_input(SERVER, CLIENT, packets.welcome()) conn = NxtcpConnection(loop, SERVER.address) mock.run_events(loop.run_once) # the expected packet mock.expect_tcp_output( CLIENT, SERVER, packets.request( name=b'name', unidirectional=True, messageref=0, timeout_ms=0, payload=b'payload', )) # put the verb upstream conn.send_verb( verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=None, payload=b'payload', )) mock.run_events(loop.run_once)
def test_request_4(self): """ Test if a request is still pushed when the connection becomes ready just before the ttl is expires. """ conn = MockedConnection() chan = Channel(conn) with patch_time() as time: conn.mock_connection_ready(False) chan.request('name', 'payload').send(ttl=5.0) time.sleep(4.999) conn.mock_connection_ready(True) conn.assert_upstream_verb(verbs.RequestVerb( name=b'name', unidirectional=True, messageref=None, timeout=5.0, payload=b'payload' ))