async def test_can_process_empty_message(self): stomp = StompReader(None, self.loop) stomp._protocol = Mock() stomp.data_received(None) await asyncio.sleep(0.001) stomp._protocol.feed_data.assert_not_called()
async def test_connection_can_be_made(self, connect_mock): stomp = StompReader(None, self.loop) transport = Mock() stomp.connection_made(transport) connect_mock.assert_called_once()
async def test_can_process_connected_frame(self, connect_handle_mock): stomp = StompReader(None, self.loop) stomp.data_received(b"CONNECTED\n" b"heart-beat:1000,1000\n\n" b"{}\x00") await asyncio.sleep(0.001) connect_handle_mock.assert_called_once()
async def test_can_send_frame(self): stomp = StompReader(None, self.loop) stomp._transport = Mock() stomp.send_frame("SUBSCRIBE", {"ack": "auto"}, "ç") stomp._transport.write.assert_called_with(b"SUBSCRIBE\n" b"ack:auto\n" b"\n" b"\xc3\xa7\x00")
def test_can_close_connection_no_heartbeat(self): frame_handler = Mock() heartbeater = Mock() stomp = StompReader(frame_handler, self.loop) stomp.heartbeater = None stomp.close() heartbeater.shutdown.assert_not_called()
async def test_can_process_error(self, error_handle_mock): stomp = StompReader(None, self.loop) stomp.data_received(b'ERROR\n' b'message:Invalid error, blah, blah, blah\n' b'\n' b'Detail Error: blah, blah, blah\x00') await asyncio.sleep(0.001) error_handle_mock.assert_called_once()
async def test_can_process_exception(self, exception_handle_mock): stomp = StompReader(None, self.loop) stomp.data_received(b"SOMETHING\n" b"message:Invalid error, blah, blah, blah\n" b"\n" b"Detail Error: blah, blah, blah\x00") await asyncio.sleep(0.001) exception_handle_mock.assert_called_once()
async def test_can_send_frame(self): stomp = StompReader(None, self.loop) stomp._transport = Mock() stomp.send_frame('SUBSCRIBE', {'ack': 'auto'}, 'ç') stomp._transport.write.assert_called_with(b'SUBSCRIBE\n' b'ack:auto\n' b'\n' b'\xc3\xa7\x00')
def test_can_close_connection(self): frame_handler = Mock() heartbeater = Mock() stomp = StompReader(frame_handler, self.loop) stomp.heartbeater = heartbeater stomp.close() heartbeater.shutdown.assert_called_once()
def test_connection_can_be_lost_no_heartbeat(self): frame_handler = Mock() heartbeater = Mock() stomp = StompReader(frame_handler, self.loop) stomp.heartbeater = None exc = Exception() stomp.connection_lost(exc) heartbeater.shutdown.assert_not_called() frame_handler.connection_lost.assert_called_with(exc)
async def test_can_connect(self): stomp = StompReader(None, self.loop, heartbeat={ "enabled": True, "cx": 1000, "cy": 1000 }) stomp._transport = Mock() stomp.connect() stomp._transport.write.assert_called_with( b"CONNECT\naccept-version:1.1\nheart-beat:1000,1000\n\n\x00")
async def test_can_process_messages(self, message_handle_mock): stomp = StompReader(None, self.loop) await asyncio.sleep(0.001) stomp.data_received( b"MESSAGE\n" b"subscription:1\n" b"message-id:007\n" b"destination:/topic/test\n" b"\n" b"blahh-line-a\n\nblahh-line-b\n\nblahh-line-c\x00") await asyncio.sleep(0.001) message_handle_mock.assert_called_once()
async def test_can_connect_with_password(self): stomp = StompReader(None, self.loop, heartbeat={ 'enabled': True, 'cx': 1000, 'cy': 1000 }, password='******') stomp._transport = Mock() stomp.connect() stomp._transport.write.assert_called_with( b'CONNECT\naccept-version:1.1\nheart-beat:1000,1000\npasscode:pass\n\n\x00' ) # noqa
async def test_can_connect_with_username(self): stomp = StompReader(None, self.loop, heartbeat={ 'enabled': True, 'cx': 1000, 'cy': 1000 }, username='******') stomp._transport = Mock() stomp.connect() stomp._transport.write.assert_called_with( b'CONNECT\naccept-version:1.1\nheart-beat:1000,1000\nlogin:pkiefer\n\n\x00' ) # noqa
async def test_can_handle_connected_frame_without_heartbeat( self, heartbeater_klass_mock): frame = Frame("CONNECTED", {}, "{}") stomp = StompReader(None, self.loop) await stomp._handle_connect(frame) heartbeater_klass_mock.assert_not_called()
async def test_can_handle_exception(self, logger_mock): frame = Frame('SOMETHING', {'message': 'Invalid error, blah, blah, blah'}, 'Detail Error: blah, blahh-line-a') stomp = StompReader(None, self.loop) await stomp._handle_exception(frame) logger_mock.warn.assert_called_with('Unhandled frame: SOMETHING')
async def test_can_connect_with_login_pass(self): stomp = StompReader( None, self.loop, heartbeat={ "enabled": True, "cx": 1000, "cy": 1000 }, username="******", password="******", ) stomp._transport = Mock() stomp.connect() stomp._transport.write.assert_called_with( b"CONNECT\naccept-version:1.1\nheart-beat:1000,1000\nlogin:pkiefer\npasscode:pass\n\n\x00" ) # noqa
async def test_can_handle_connected_frame_with_heartbeat_disabled( self, heartbeater_klass_mock): frame = Frame("CONNECTED", {"heart-beat": "1000,1000"}, "{}") heartbeater_mock = heartbeater_klass_mock.return_value heartbeater_mock.start = CoroutineMock() stomp = StompReader(None, self.loop, heartbeat={ "enabled": False, "cx": 0, "cy": 0 }) stomp._transport = Mock() await stomp._handle_connect(frame) heartbeater_klass_mock.assert_not_called
async def test_can_handle_connected_frame_with_heartbeat_disabled( self, heartbeater_klass_mock): frame = Frame('CONNECTED', {'heart-beat': '1000,1000'}, '{}') heartbeater_mock = heartbeater_klass_mock.return_value heartbeater_mock.start = CoroutineMock() stomp = StompReader(None, self.loop, heartbeat={ 'enabled': False, 'cx': 0, 'cy': 0 }) stomp._transport = Mock() await stomp._handle_connect(frame) heartbeater_klass_mock.assert_not_called
async def test_can_process_long_messages(self, message_handle_mock): stomp = StompReader(None, self.loop) await asyncio.sleep(0.001) data = ( b"MESSAGE\n" b"content-length:14\nexpires:0\ndestination:/topic/" b"xxxxxxxxxxxxxxxxxxxxxxxxxl" b"\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id" b":ID\\cxxxxxx-35207-1543430467768-204" b"\\c363\\c-1\\c1\\c463859\npersistent:true\ntimestamp" b":1548945234003\n\n222.222.22.222" b"\x00\nMESSAGE\ncontent-length:12\nexpires:0\ndestination:" b"/topic/xxxxxxxxxxxxxxxxxxxxxxxxxx" b"\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id" b":ID\\cxxxxxx-35207-1543430467768-204" b"\\c363\\c-1\\c1\\c463860\npersistent:true\ntimestamp" b":1548945234005\n\n88.88.888.88" b"\x00\nMESSAGE\ncontent-length:11\nexpires:0\ndestination:" b"/topic/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" b"\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id" b":ID\\cxxxxxx-35207-1543430467768-204" b"\\c362\\c-1\\c1\\c290793\npersistent:true\ntimestamp" b":1548945234005\n\n111.11.1.11" b"\x00\nMESSAGE\ncontent-length:14\nexpires:0\ndestination:" b"/topic/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" b"\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id" b":ID\\cxxxxxx-35207-1543430467768-204" b"\\c362\\c-1\\c1\\c290794\npersistent:true\ntimestamp:" b"1548945234005\n\n222.222.22.222" b"\x00\nMESSAGE\ncontent-length:12\nexpires:0\ndestination:" b"/topic/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" b"\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id" b":ID\\cxxxxxx-35207-1543430467768-204" b"\\c362\\c-1\\c1\\c290795\npersistent:true\ntimestamp:" b"1548945234005\n\n88.88.888.88\x00\nMESS") stomp.data_received(data) await asyncio.sleep(0.001) self.assertEqual(message_handle_mock.call_count, 5) self.assertEqual(bytes(stomp._protocol.current_command), b"MESS")
async def test_can_process_long_partial_messages(self, message_handle_mock): stomp = StompReader(None, self.loop) await asyncio.sleep(0.001) stomp.data_received( b'MESSAGE\n' b'content-length:14\nexpires:0\ndestination:/topic/' b'xxxxxxxxxxxxxxxxxxxxxxxxxl' b'\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id') await asyncio.sleep(0.001) stomp.data_received( b':ID\\cxxxxxx-35207-1543430467768-204' b'\\c363\\c-1\\c1\\c463859\npersistent:true\ntimestamp' b':1548945234003\n\n222.222.22.222' b'\x00\nMESSAGE\ncontent-length:12\nexpires:0\ndestination:' b'/topic/xxxxxxxxxxxxxxxxxxxxxxxxxx' b'\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id' b':ID\\cxxxxxx-35207-1543430467768-204' b'\\c363\\c-1\\c1\\c463860\npersistent:true\ntimestamp' b':1548945234005\n\n88.88.888.88' b'\x00\nMESS') await asyncio.sleep(0.001) stomp.data_received( b'AGE\n' b'content-length:14\nexpires:0\ndestination:/topic/' b'xxxxxxxxxxxxxxxxxxxxxxxxxl' b'\nsubscription:1\npriority:4\nActiveMQ.MQTT.QoS:1\nmessage-id') await asyncio.sleep(0.001) stomp.data_received( b':ID\\cxxxxxx-35207-1543430467768-204' b'\\c363\\c-1\\c1\\c463859\npersistent:true\ntimestamp' b':1548945234003\n\n222.222.22.222' b'\x00') await asyncio.sleep(0.001) self.assertEqual(message_handle_mock.call_count, 3) self.assertEqual(b''.join(stomp._protocol.current_command), b'')
async def test_can_handle_exception(self): frame = Frame( 'SOMETHING', {'message': 'Invalid error, blah, blah, blah'}, 'Detail Error: blah, blahh-line-a', ) stomp = StompReader(None, self.loop) with self.assertLogs('aiostomp', level="WARNING") as cm: await stomp._handle_exception(frame) self.assertEqual(cm.output, ["WARNING:aiostomp:Unhandled frame: SOMETHING"])
async def test_can_handle_exception(self): frame = Frame( "SOMETHING", {"message": "Invalid error, blah, blah, blah"}, "Detail Error: blah, blahh-line-a", ) stomp = StompReader(None, self.loop) with self.assertLogs("aiostomp", level="WARNING") as cm: await stomp._handle_exception(frame) self.assertEqual(cm.output, ["WARNING:aiostomp:Unhandled frame: SOMETHING"])
async def test_can_handle_message_with_no_subscription(self): frame = Frame('MESSAGE', { 'subscription': '123', 'message-id': '321' }, 'blah') handler = CoroutineMock() frame_handler = Mock() frame_handler.get.return_value = None stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_not_called()
async def test_can_handle_message_with_no_subscription(self): frame = Frame("MESSAGE", { "subscription": "123", "message-id": "321" }, "blah") handler = CoroutineMock() frame_handler = Mock() frame_handler.get.return_value = None stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_not_called()
async def test_can_handle_message(self): frame = Frame('MESSAGE', { 'subscription': '123', 'message-id': '321' }, 'blah') handler = CoroutineMock() subscription = Subscription('123', 1, 'auto', {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body)
async def test_can_handle_message(self): frame = Frame("MESSAGE", { "subscription": "123", "message-id": "321" }, "blah") handler = CoroutineMock() subscription = Subscription("123", 1, "auto", {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body)
async def test_can_handle_error_frame(self, logger_mock): frame = Frame('ERROR', {'message': 'Invalid error, blah, blah, blah'}, 'Detail Error: blah, blahh-line-a') frame_handler = Mock() frame_handler._on_error = CoroutineMock() stomp = StompReader(frame_handler, self.loop) await stomp._handle_error(frame) frame_handler._on_error.assert_called_once() self.assertTrue( isinstance(frame_handler._on_error.call_args[0][0], StompError)) logger_mock.error.assert_called_with( 'Received error: Invalid error, blah, blah, blah') logger_mock.debug.assert_called_with( 'Error details: Detail Error: blah, blahh-line-a')
async def test_can_handle_message_can_nack(self, send_frame_mock): frame = Frame("MESSAGE", { "subscription": "123", "message-id": "321" }, "blah") handler = CoroutineMock() handler.return_value = False subscription = Subscription("123", 1, "client-individual", {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body) send_frame_mock.assert_called_with("NACK", { "subscription": "123", "message-id": "321" })
async def test_can_handle_message_can_nack(self, send_frame_mock): frame = Frame('MESSAGE', { 'subscription': '123', 'message-id': '321' }, 'blah') handler = CoroutineMock() handler.return_value = False subscription = Subscription('123', 1, 'client-individual', {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body) send_frame_mock.assert_called_with('NACK', { 'subscription': '123', 'message-id': '321' })