Beispiel #1
0
    def test_notify(self):
        """
        Verify BusMixin.notify can send off notifications.
        """
        result_dict = {'jsonrpc': '2.0', 'result': []}

        instance = bus.BusMixin()
        instance.logger = mock.MagicMock()
        instance.connection = mock.MagicMock()
        instance.producer = mock.MagicMock()
        instance._exchange = 'exchange'

        method = 'ping'
        routing_key = 'routing_key.' + method
        params = {}

        # Omit the method to test extracting it from the routing key.
        result = instance.notify(routing_key, params=params)
        self.assertIsNone(result)

        # A jsonrpc notification should have been published to the bus
        instance.producer.publish.assert_called_once_with(
            {
                'jsonrpc': '2.0',
                'method': method,
                'params': params,
            },
            routing_key,
            declare=[instance._exchange])
Beispiel #2
0
    def test_request(self):
        """
        Verify BusMixin.request can request method calls.
        """
        result_dict = {'jsonrpc': '2.0', 'result': []}

        instance = bus.BusMixin()
        instance.logger = mock.MagicMock()
        instance.connection = mock.MagicMock()
        instance.producer = mock.MagicMock()
        instance.connection.SimpleQueue().get.return_value = mock.MagicMock(
            payload=json.dumps(result_dict))
        # Reset call count
        instance.connection.SimpleQueue.call_count = 0
        instance._exchange = 'exchange'

        method = 'ping'
        routing_key = 'routing_key.' + method
        params = {}
        queue_opts = {'durable': False, 'auto_delete': True}

        # Omit the method to test extracting it from the routing key.
        result = instance.request(routing_key, params=params)
        self.assertEquals(result_dict, result)
        # A new SimpleQueue should have been created
        instance.connection.SimpleQueue.assert_called_once_with(
            mock.ANY, queue_opts=queue_opts)
        # A jsonrpc message should have been published to the bus
        instance.producer.publish.assert_called_once_with(
            {
                'jsonrpc': '2.0',
                'id': mock.ANY,
                'method': method,
                'params': params,
            },
            routing_key,
            declare=[instance._exchange],
            reply_to=mock.ANY)
        # The simple queue should be used to get a response
        instance.connection.SimpleQueue.__call__().get.assert_called_once_with(
            block=True, timeout=mock.ANY)
        # And finally the queue should be closed
        instance.connection.SimpleQueue.__call__(
        ).close.assert_called_once_with()
Beispiel #3
0
    def test_failed_request(self):
        """
        Verify BusMixin.request raises an exception for failed method calls.
        """
        result_dict = {
            'jsonrpc': '2.0',
            'error': {
                'code': -32601,
                'message': 'Method not found',
                'data': {
                    'exception': 'whatever'
                }
            }
        }

        instance = bus.BusMixin()
        instance.logger = mock.MagicMock()
        instance.connection = mock.MagicMock()
        instance.producer = mock.MagicMock()
        instance.connection.SimpleQueue().get.return_value = mock.MagicMock(
            payload=json.dumps(result_dict))
        # Reset call count
        instance.connection.SimpleQueue.call_count = 0
        instance._exchange = 'exchange'

        routing_key = 'routing_key'
        method = 'ping'
        params = {}
        queue_opts = {'durable': False, 'auto_delete': True}

        with self.assertRaises(bus.RemoteProcedureCallError) as cm:
            instance.request(routing_key, method, param=params)

        self.assertEqual(cm.exception.code, -32601)
        self.assertEqual(cm.exception.message, 'Method not found')
        self.assertIn('exception', cm.exception.data)