Exemplo n.º 1
0
    def test_assert_handlers_called(self):
        handler_vars = {
            'create_handler_called': False,
            'update_handler_called': False,
            'delete_handler_called': False
        }

        def create_handler(sensor_db):
            handler_vars['create_handler_called'] = True

        def update_handler(sensor_db):
            handler_vars['update_handler_called'] = True

        def delete_handler(sensor_db):
            handler_vars['delete_handler_called'] = True

        sensor_watcher = SensorWatcher(create_handler, update_handler, delete_handler)

        message = Message(None, delivery_info={'routing_key': 'create'})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars['create_handler_called'], 'create handler should be called.')

        message = Message(None, delivery_info={'routing_key': 'update'})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars['update_handler_called'], 'update handler should be called.')

        message = Message(None, delivery_info={'routing_key': 'delete'})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars['delete_handler_called'], 'delete handler should be called.')
Exemplo n.º 2
0
    def test_assert_handlers_called(self):
        handler_vars = {
            "create_handler_called": False,
            "update_handler_called": False,
            "delete_handler_called": False,
        }

        def create_handler(sensor_db):
            handler_vars["create_handler_called"] = True

        def update_handler(sensor_db):
            handler_vars["update_handler_called"] = True

        def delete_handler(sensor_db):
            handler_vars["delete_handler_called"] = True

        sensor_watcher = SensorWatcher(create_handler, update_handler,
                                       delete_handler)

        message = Message(None, delivery_info={"routing_key": "create"})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars["create_handler_called"],
                        "create handler should be called.")

        message = Message(None, delivery_info={"routing_key": "update"})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars["update_handler_called"],
                        "update handler should be called.")

        message = Message(None, delivery_info={"routing_key": "delete"})
        sensor_watcher.process_task(MOCK_SENSOR_DB, message)
        self.assertTrue(handler_vars["delete_handler_called"],
                        "delete handler should be called.")
Exemplo n.º 3
0
 def test_decode(self):
     m = Message('body', channel=Mock())
     decode = m._decode = Mock()
     assert m._decoded_cache is None
     assert m.decode() is m._decode.return_value
     assert m._decoded_cache is m._decode.return_value
     m._decode.assert_called_with()
     m._decode = Mock()
     assert m.decode() is decode.return_value
Exemplo n.º 4
0
 def test_decode(self):
     m = Message(Mock(), 'body')
     decode = m._decode = Mock()
     self.assertIsNone(m._decoded_cache)
     self.assertIs(m.decode(), m._decode.return_value)
     self.assertIs(m._decoded_cache, m._decode.return_value)
     m._decode.assert_called_with()
     m._decode = Mock()
     self.assertIs(m.decode(), decode.return_value)
Exemplo n.º 5
0
    def test_reraise_error(self):
        m = Message(Mock(), 'body')
        callback = Mock(name='callback')
        try:
            raise KeyError('foo')
        except KeyError as exc:
            m.errors.append(sys.exc_info())
        m._reraise_error(callback)
        self.assertTrue(callback.called)

        with self.assertRaises(KeyError):
            m._reraise_error(None)
Exemplo n.º 6
0
    def test_reraise_error(self):
        m = Message('body', channel=Mock())
        callback = Mock(name='callback')
        try:
            raise KeyError('foo')
        except KeyError:
            m.errors.append(sys.exc_info())
        m._reraise_error(callback)
        callback.assert_called()

        with pytest.raises(KeyError):
            m._reraise_error(None)
Exemplo n.º 7
0
def test_unexpected_correlation_id(container_factory, rabbit_config):
    container = container_factory(FooService, rabbit_config)
    container.start()

    with ServiceRpcProxy("foobar", rabbit_config) as proxy:

        message = Message(channel=None, properties={
            'reply_to': proxy.reply_listener.routing_key,
            'correlation_id': 'invalid',
        })
        responder = Responder(container.config, message)
        with patch('nameko.standalone.rpc._logger', autospec=True) as logger:
            responder.send_response(None, None)
            assert proxy.spam(ham='eggs') == 'eggs'
            assert logger.debug.call_count == 1
Exemplo n.º 8
0
    def test_handle_message(self, monkeypatch, mongo_db_mock, body,
                            routing_key, expected_save_data):
        body = json.dumps(body)
        monkeypatch.setattr(LoggingConsumerStep, "connect_db", Mock())
        logging_consumer = LoggingConsumerStep(None)
        logging_consumer.db = mongo_db_mock
        msg = Message(Mock(),
                      body=body,
                      delivery_info={'routing_key': routing_key})

        def tested_func():
            logging_consumer.handle_message(body, msg)

        if expected_save_data is not None:
            tested_func()
            mongo_db_mock.logs.insert_one.assert_called_once_with(
                expected_save_data)
        else:
            with raises(TypeError):
                tested_func()
Exemplo n.º 9
0
def test_nova_consumer_bad_provider():
    consumer = NovaRpcConsumer()
    consumer.container = Mock()
    message = Message(
        channel=None,
        delivery_info={'routing_key': 'some route'},
        properties={},
    )
    with patch.multiple(
            consumer,
            get_provider_for_method=DEFAULT,
            handle_result=DEFAULT,
    ) as mocks:
        provider = mocks['get_provider_for_method']
        handle_result = mocks['handle_result']

        exception = LookupError('broken')
        provider.side_effect = exception
        consumer.handle_message({'args': ()}, message)
        assert handle_result.call_count == 1
        args, kwargs = handle_result.call_args
        exc_info = args[-1]
        exc_type, exc_value, traceback = exc_info
        assert exc_value is exception
Exemplo n.º 10
0
 def test_repr(self):
     assert repr(Message('b', channel=Mock()))
Exemplo n.º 11
0
 def test_postencode(self):
     m = Message('FOO', channel=self.channel, postencode='ccyzz')
     with pytest.raises(LookupError):
         m._reraise_error()
     m.ack()
Exemplo n.º 12
0
 def setup(self):
     self.conn = Connection('memory://')
     self.channel = self.conn.channel()
     self.message = Message(channel=self.channel, delivery_tag=313)
Exemplo n.º 13
0
 def test_repr(self):
     self.assertTrue(repr(Message(Mock(), 'b')))
Exemplo n.º 14
0
 def test_postencode(self):
     m = Message(self.channel, text_t('FOO'), postencode='ccyzz')
     with self.assertRaises(LookupError):
         m._reraise_error()
     m.ack()
Exemplo n.º 15
0
 def test_repr(self):
     assert repr(Message(Mock(), 'b'))
Exemplo n.º 16
0
 def test_postencode(self):
     with self.assertRaises(LookupError):
         Message(self.channel, text_t('FOO'), postencode='ccyzz')
Exemplo n.º 17
0
 def test_decompression_stores_error(self, decompress):
     decompress.side_effect = RuntimeError()
     m = Message('body', channel=Mock(), headers={'compression': 'zlib'})
     with pytest.raises(RuntimeError):
         m._reraise_error(None)
Exemplo n.º 18
0
 def test_decompression_stores_error(self, decompress):
     decompress.side_effect = RuntimeError()
     m = Message(Mock(), 'body', headers={'compression': 'zlib'})
     with self.assertRaises(RuntimeError):
         m._reraise_error(None)