def test_send_internal_and_broker(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.vdsm_requests', Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) ids = {} subscription = FakeSubscription('jms.topic.vdsm_requests', 'e8a936a6-d886-4cfa-97b9-2d54209053ff') client = FakeAsyncClient() subscription.set_client(client) destinations = defaultdict(list) destinations['jms.topic.vdsm_requests'].append(subscription) adapter = StompAdapterImpl(Reactor(), destinations, ids) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) data = adapter.pop_message() self.assertIsNot(data, None) request = JsonRpcRequest.decode(data) self.assertEqual(request.method, 'Host.getAllVmStats') self.assertEqual(len(ids), 1) resp_frame = client.pop_message() self.assertIsNot(resp_frame, None) self.assertEqual(resp_frame.command, Command.MESSAGE) self.assertEqual(resp_frame.body, data)
def test_no_id(self): frame = Frame(Command.UNSUBSCRIBE) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.ERROR) self.assertEqual(resp_frame.body, b'Missing id header')
def test_no_headers(self): frame = Frame(Command.CONNECT) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.ERROR) self.assertEqual(resp_frame.body, b'Version unsupported')
def test_receive_connected(self): client = AsyncClient() frame = Frame(Command.CONNECTED, { 'version': '1.2', Headers.HEARTBEAT: '8000,0' }) client.handle_frame(FakeAsyncDispatcher(''), frame) self.assertTrue(client.connected)
def test_send_no_destination(self): frame = Frame(Command.SEND, {Headers.DESTINATION: 'jms.topic.unknown'}) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.ERROR) self.assertEqual(resp_frame.body, b'Subscription not available')
def test_no_heartbeat(self): frame = Frame(Command.CONNECT, {Headers.ACCEPT_VERSION: '1.2'}) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.CONNECTED) self.assertEqual(resp_frame.headers['version'], '1.2') self.assertEqual(resp_frame.headers[Headers.HEARTBEAT], '0,0')
def test_no_id(self): frame = Frame(Command.SUBSCRIBE, {'ack': 'auto', Headers.DESTINATION: 'jms.queue.events'}) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.ERROR) self.assertEqual(resp_frame.body, b'Missing destination or subscription id header')
def test_no_destination(self): frame = Frame(Command.SUBSCRIBE, {'ack': 'auto', 'id': 'ad052acb-a934-4e10-8ec3-00c7417ef8d1'}) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.ERROR) self.assertEqual(resp_frame.body, b'Missing destination or subscription id header')
def test_handle_read(self): frame_handler = FakeFrameHandler() headers = {Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: 'ad052acb-a934-4e10-8ec3-00c7417ef8d'} body = ('{"jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053f' 'f", "result": []}') frame = Frame(command=Command.MESSAGE, headers=headers, body=body) dispatcher = AsyncDispatcher(FakeConnection(), frame_handler) dispatcher.handle_read(FakeAsyncDispatcher(None, data=frame.encode())) self.assertTrue(frame_handler.has_outgoing_messages) recv_frame = frame_handler.pop_message() self.assertEqual(Command.MESSAGE, recv_frame.command) self.assertEqual(body, recv_frame.body)
def test_unsubscribe(self): frame = Frame(Command.UNSUBSCRIBE, {'id': 'ad052acb-a934-4e10-8ec3-00c7417ef8d1'}) subscription = FakeSubscription('jms.queue.events', 'ad052acb-a934-4e10-8ec3-00c7417ef8d1') destinations = defaultdict(list) destinations['jms.queue.events'].append(subscription) adapter = StompAdapterImpl(Reactor(), destinations, {}) adapter._sub_ids['ad052acb-a934-4e10-8ec3-00c7417ef8d1'] = subscription adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) self.assertEqual(len(adapter._sub_ids), 0) self.assertEqual(len(destinations), 0)
def test_subscribe(self): frame = Frame(Command.SUBSCRIBE, {Headers.DESTINATION: 'jms.queue.events', 'ack': 'auto', 'id': 'ad052acb-a934-4e10-8ec3-00c7417ef8d1'}) destinations = defaultdict(list) adapter = StompAdapterImpl(Reactor(), destinations, {}) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) subscription = destinations['jms.queue.events'][0] self.assertEqual(subscription.id, 'ad052acb-a934-4e10-8ec3-00c7417ef8d1') self.assertEqual(subscription.destination, 'jms.queue.events')
def test_handle_write(self): headers = {Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: 'ad052acb-a934-4e10-8ec3-00c7417ef8d'} body = ('{"jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053f' 'f", "result": []}') frame = Frame(command=Command.MESSAGE, headers=headers, body=body) frame_handler = FakeFrameHandler() frame_handler.handle_frame(None, frame) dispatcher = AsyncDispatcher(FakeConnection(), frame_handler) self.assertTrue(dispatcher.writable(None)) dispatcher.handle_write(FakeAsyncDispatcher('')) self.assertFalse(frame_handler.has_outgoing_messages)
def test_no_flow_header(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: SUBSCRIPTION_ID_REQUEST, Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) adapter = StompAdapterImpl(Reactor(), defaultdict(list), {}) dispatcher = FakeAsyncDispatcher(adapter) adapter.handle_frame(dispatcher, frame) self.assertIsNone(dispatcher.connection.flow_id)
def test_handle_read(): frame_handler = FakeFrameHandler() headers = {Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: 'ad052acb-a934-4e10-8ec3-00c7417ef8d'} body = json.dumps({ "jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053ff", "result": [], }).encode("utf-8") frame = Frame(command=Command.MESSAGE, headers=headers, body=body) dispatcher = AsyncDispatcher(FakeConnection(), frame_handler) dispatcher.handle_read(FakeAsyncDispatcher(None, data=frame.encode())) assert frame_handler.has_outgoing_messages recv_frame = frame_handler.pop_message() assert Command.MESSAGE == recv_frame.command assert body == recv_frame.body
def test_multipe_subscriptions(self): frame = Frame(Command.UNSUBSCRIBE, {'id': 'ad052acb-a934-4e10-8ec3-00c7417ef8d1'}) subscription = FakeSubscription('jms.queue.events', 'ad052acb-a934-4e10-8ec3-00c7417ef8d1') subscription2 = FakeSubscription('jms.queue.events', 'e8a93a6-d886-4cfa-97b9-2d54209053ff') destinations = defaultdict(list) destinations['jms.queue.events'].append(subscription) destinations['jms.queue.events'].append(subscription2) adapter = StompAdapterImpl(Reactor(), destinations, {}) adapter._sub_ids['ad052acb-a934-4e10-8ec3-00c7417ef8d1'] = subscription adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) self.assertEqual(len(adapter._sub_ids), 0) self.assertEqual(len(destinations), 1) self.assertEqual(destinations['jms.queue.events'], [subscription2])
def test_send_batch(self): body = ('[{"jsonrpc":"2.0","method":"Host.getAllVmStats","params":{},' '"id":"e8a936a6-d886-4cfa-97b9-2d54209053ff"},' '{"jsonrpc":"2.0","method":"Host.getAllVmStats","params":{},' '"id":"1202b274-5a06-4671-8b13-1d2715429668"}]') frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.vdsm_requests', Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '209'}, body=body ) ids = {} adapter = StompAdapterImpl(Reactor(), defaultdict(list), ids) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) data = adapter.pop_message() self.assertIsNot(data, None) self.assertEqual(len(ids), 2)
def test_send(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.vdsm_requests', Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) ids = {} adapter = StompAdapterImpl(Reactor(), defaultdict(list), ids) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) data = adapter.pop_message() self.assertIsNot(data, None) request = JsonRpcRequest.decode(data) self.assertEqual(request.method, 'Host.getAllVmStats') self.assertEqual(len(ids), 1)
def test_send_broker(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.destination', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) subscription = FakeSubscription('jms.topic.destination', 'e8a936a6-d886-4cfa-97b9-2d54209053ff') destinations = defaultdict(list) destinations['jms.topic.destination'].append(subscription) adapter = StompAdapterImpl(Reactor(), destinations, {}) subscription.set_client(adapter) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.MESSAGE)