def test_message_timeout_1(self): """ Test if a response message to a request results in the handler being called correctly. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(True) req = chan.request('name', 'payload') handler = Mock() req.add_handler(handler, MessageStatus.ANY) reqi = req.send() conn.mock_downstream_verb(verbs.MessageVerb( messageref=1, status=verbs.MessageVerb.STATUS_TIMEOUT, payload=b'response' )) self.__verify_handler_call( handler, Message, status=MessageStatus.TIMEOUT, payload=None, source=reqi, )
def test_subscribe_message_1(self): """ Test that the handler is called when a message is received for a subscription. """ conn = MockedConnection() chan = Channel(conn) conn.mock_connection_ready(True) handler = unittest.mock.Mock() sub = chan.subscribe('name', 'topic') sub.add_handler(handler) conn.mock_downstream_verb(verbs.MessageVerb( messageref=1, status=verbs.MessageVerb.STATUS_OK, payload=b'payload1', )) self.__verify_handler_call( handler, Message, status=MessageStatus.OK, payload='payload1', ) conn.mock_downstream_verb(verbs.MessageVerb( messageref=1, status=verbs.MessageVerb.STATUS_OK, payload=b'payload2', )) self.__verify_handler_call( handler, Message, status=MessageStatus.OK, payload='payload2', )
def __on_message_packet(self, packet): """ Called when a message packet has been received. """ # create verb from packet and send it downstream verb = verbs.MessageVerb( messageref=packet.messageref, status=[ verbs.MessageVerb.STATUS_OK, verbs.MessageVerb.STATUS_TIMEOUT, verbs.MessageVerb.STATUS_UNREACHABLE ][packet.status], payload=packet.payload, ) if self.downstream_handler: self.downstream_handler(verb)
def test_message_3(self): """ Test that a Message verb is produced. """ mock = Sysmock() mock.system.add_unused_local_address(CLIENT) downstream_handler = unittest.mock.Mock() 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()) # send the packet mock.do_tcp_input( SERVER, CLIENT, packets.message( messageref=1234, status=packets.MESSAGE_STATUS_UNREACHABLE, payload=b'ignoreme', )) conn = NxtcpConnection(loop, SERVER.address) conn.set_downstream_handler(downstream_handler) mock.run_events(loop.run_once) # verify the verb downstream_handler.assert_called_once_with( verbs.MessageVerb( messageref=1234, status=verbs.MessageVerb.STATUS_UNREACHABLE, payload=None, ))