Ejemplo n.º 1
0
    def test_on_message(self):

        global return_message

        # setup mocks and fixtures
        mocked_handler = mock.Mock(wraps=handler)
        service = fixtures.get_service_fixture(mocked_handler)
        single_service = [v for v in service.values()][0]

        message = {}
        queue = {'name': single_service['queue']}
        key = {'name': single_service['key']}
        connection = AsyncConnection({})

        # setup expectations
        return_message = fixtures.random_string()
        return_queue = single_service['report']

        with mock.patch.object(connection, "publish") as mocked_publish:

            # on_message = messagedriven_service._get_on_message(single_service)
            result = messagedriven_service._on_message(connection, single_service, message)

            # The result should be True
            self.assertTrue(result)

            # The message handler should be called with the message
            mocked_handler.assert_called_with(message)

            # The return message should be published on the return queue
            mocked_publish.assert_called_with(return_queue, return_queue['key'], return_message)
Ejemplo n.º 2
0
    def test_messagedriven_service(self, mocked_connection, mock_init_broker):

        global return_method
        return_method = fixtures.random_string()

        service_definition = fixtures.get_service_fixture(handler)
        single_service = [v for v in service_definition.values()][0]

        expected_queue = single_service['queue']

        messagedriven_service = MessagedrivenService(service_definition,
                                                     'Any name')
        messagedriven_service._init = MagicMock()
        messagedriven_service._start_threads = MagicMock()
        messagedriven_service._heartbeat_loop = MagicMock()
        messagedriven_service.keep_running = False
        messagedriven_service.start()

        mock_init_broker.assert_called_with()
        mocked_connection.assert_called_with(CONNECTION_PARAMS, {})
        mocked_connection.return_value.__enter__.return_value.subscribe\
            .assert_called_with([expected_queue], mock.ANY)  # Inner function

        messagedriven_service._heartbeat_loop.assert_called_once()
        messagedriven_service._start_threads.assert_not_called()
Ejemplo n.º 3
0
    def test_messagedriven_service_multithreaded(self, mock_init_broker):
        service_definition = fixtures.get_service_fixture(handler)

        messagedriven_service = MessagedrivenService(
            service_definition, 'Some name', {'thread_per_service': True})
        messagedriven_service._init = MagicMock()
        messagedriven_service._start_threads = MagicMock()
        messagedriven_service._heartbeat_loop = MagicMock()
        messagedriven_service.keep_running = False
        messagedriven_service.start()

        mock_init_broker.assert_called_with()
        messagedriven_service._heartbeat_loop.assert_called_once()
        messagedriven_service._start_threads.assert_called_once()
Ejemplo n.º 4
0
    def test_messagedriven_service(self, mocked_connection, mocked_init, mocked_send, mocked_heartbeat):

        global return_method
        return_method = fixtures.random_string()

        service_definition = fixtures.get_service_fixture(handler)
        single_service = [v for v in service_definition.values()][0]

        expected_key = single_service['key']
        expected_queue = single_service['queue']
        expected_exchange = single_service['exchange']

        messagedriven_service.keep_running = False
        messagedriven_service.messagedriven_service(service_definition, "Any name")

        mocked_init.assert_called_with()
        mocked_connection.assert_called_with(CONNECTION_PARAMS, {})
        mocked_connection.return_value.__enter__.return_value.subscribe\
            .assert_called_with([{'exchange': expected_exchange,
                                  'name': expected_queue,
                                  'key': expected_key}], mock.ANY)  # Inner function

        mocked_heartbeat.asssert_not_called()
        mocked_send.assert_not_called()