Exemplo n.º 1
0
 def test_on_message(self, mock_on_message, mock_init_messagebroker):
     messagedriven_service = MessagedrivenService([], 'name', {})
     messagedriven_service._get_service = MagicMock()
     messagedriven_service._on_message('connection', 'exchange', 'queue',
                                       'key', 'msg')
     messagedriven_service._get_service.assert_called_with('queue')
     mock_on_message.assert_called_with(
         'connection', messagedriven_service._get_service.return_value,
         'msg')
Exemplo n.º 2
0
    def test_messagedriven_service_startthreads(self, mock_init_broker):
        messagedriven_service = MessagedrivenService(
            [], 'Some name', {'thread_per_service': True})
        queues = ['queue1', 'queue2']
        messagedriven_service._start_thread = MagicMock()
        messagedriven_service._start_threads(queues)

        messagedriven_service._start_thread.assert_has_calls([
            call(['queue1']),
            call(['queue2']),
        ])
Exemplo n.º 3
0
    def test_listen(self, mock_connection, mock_init_messagebroker):
        messagedriven_service = MessagedrivenService([], 'name', {})

        class MockConnection:
            subscribe = MagicMock()

            def is_alive(self):
                # Trick to get around infinite while loop, while still being able to run the loop once
                messagedriven_service.keep_running = False
                return True

        mock_connection.return_value.__enter__.return_value = MockConnection()
        messagedriven_service.check_connection = 0

        messagedriven_service._listen(['q1', 'q1'])
Exemplo n.º 4
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()
Exemplo n.º 5
0
 def test_get_service(self, mock_init_broker):
     services = {
         "a": {
             "queue": "q1",
             "handler": "h1",
         },
         "b": {
             "queue": "q2",
             "handler": "h2",
         }
     }
     messagedriven_service = MessagedrivenService(services, 'name', {})
     self.assertTrue(
         messagedriven_service._get_service("q1") == services["a"])
     self.assertTrue(
         messagedriven_service._get_service("q2") == services["b"])
Exemplo n.º 6
0
    def test_messagedriven_service_startthread(self, mock_thread,
                                               mock_init_broker):
        messagedriven_service = MessagedrivenService(
            [], 'Some name', {'thread_per_service': True})
        queues_arg = ['some queue', 'some other queue']
        mock_thread.return_value = MagicMock()
        result = messagedriven_service._start_thread(queues_arg)
        mock_thread.assert_called_with(target=messagedriven_service._listen,
                                       args=(queues_arg, ),
                                       name="QueueHandler")
        mock_thread.return_value.start.assert_called_once()
        self.assertEqual(mock_thread.return_value, result)

        self.assertEqual([{
            'queues': queues_arg,
            'thread': mock_thread.return_value,
        }], messagedriven_service.threads)
Exemplo n.º 7
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()
Exemplo n.º 8
0
    def test_get_service(self, mock_init_broker):
        services = {
            "a": {
                "queue": "q1",
                "handler": "h1",
            },
            "b": {
                "queue": lambda: "q2",
                "handler": "h2",
            }
        }
        messagedriven_service = MessagedrivenService(services, 'name', {})
        self.assertTrue(
            messagedriven_service._get_service("q1") == services["a"])

        # Queue lambda function is replaced with its result
        self.assertTrue(
            messagedriven_service._get_service("q2") == {
                **services["b"], "queue": "q2"
            })
Exemplo n.º 9
0
    def test_heartbeat_loop(self, mock_heartbeat, mock_connection,
                            mock_init_messagebroker):
        messagedriven_service = MessagedrivenService([], 'name', {})

        class MockConnection:
            subscribe = MagicMock()

            def is_alive(self):
                # Trick to get around infinite while loop, while still being able to run the loop once
                messagedriven_service.keep_running = False
                return True

        mock_connection.return_value.__enter__.return_value = MockConnection()
        messagedriven_service.check_connection = 0
        messagedriven_service.heartbeat_interval = 0

        healthy_thread = type('MockThread', (object, ),
                              {'is_alive': lambda: True})
        unhealthy_thread = type('MockThread', (object, ),
                                {'is_alive': lambda: False})

        messagedriven_service.threads = [
            {
                'thread': healthy_thread,
                'queues': 'the queues'
            },
            {
                'thread': unhealthy_thread,
                'queues': 'the queues 2'
            },
        ]
        messagedriven_service._start_thread = MagicMock(
            return_value=healthy_thread)

        messagedriven_service._heartbeat_loop()

        messagedriven_service._start_thread.assert_has_calls(
            [call('the queues 2')])
        mock_heartbeat.return_value.send.assert_called_once()
Exemplo n.º 10
0
                    help='migrate the database tables, views and indexes')
parser.add_argument('--materialized_views',
                    action='store_true',
                    default=False,
                    help='force recreation of materialized views')
parser.add_argument('mv_name',
                    nargs='?',
                    help='The materialized view to update. Use with --materialized-views')
args = parser.parse_args()

if DEBUG:
    print("WARNING: Debug mode is ON")

# Initialize database tables
storage = GOBStorageHandler()

# Migrate on request only
if args.migrate:
    recreate = [args.mv_name] if args.materialized_views and args.mv_name else args.materialized_views
    storage.init_storage(force_migrate=True, recreate_materialized_views=recreate)
else:
    storage.init_storage()
    params = {
        "stream_contents": True,
        "thread_per_service": True,
        APPLY_QUEUE: {
            "load_message": False
        }
    }
    MessagedrivenService(SERVICEDEFINITION, "Upload", params).start()
Exemplo n.º 11
0
    def test_init_exception(self, mock_exit, mock_init_messagebroker):
        mock_init_messagebroker.side_effect = Exception

        MessagedrivenService([], 'name', {})

        mock_exit.assert_called_with(1)
Exemplo n.º 12
0
    def test_messagedriven_service_start(self, mock_init_broker):
        services = {
            "s1": {
                'queue': "q1",
                'handler': MagicMock(),
                'report': {
                    'exchange': "e1",
                    'key': "k1"
                }
            },
            "s2": {
                'queue': "q2",
                'handler': MagicMock(),
                RUNS_IN_OWN_THREAD: True,
                'report': {
                    'exchange': "e2",
                    'key': "k2"
                }
            },
            "s3": {
                'queue': "q3",
                'handler': MagicMock(),
                RUNS_IN_OWN_THREAD: False,
                'report': {
                    'exchange': "e3",
                    'key': "k3"
                }
            },
        }
        messagedriven_service = MessagedrivenService(
            services, 'Some name', {'thread_per_service': True})

        messagedriven_service._init = MagicMock()
        messagedriven_service._heartbeat_loop = MagicMock()
        messagedriven_service.keep_running = False

        messagedriven_service._start_threads = MagicMock()
        messagedriven_service._start_thread = MagicMock()
        messagedriven_service.start()

        messagedriven_service._start_threads.assert_called_with(
            ['q1', 'q2', 'q3'])
        messagedriven_service._start_thread.assert_not_called()

        messagedriven_service = MessagedrivenService(services, 'Some name')

        messagedriven_service._init = MagicMock()
        messagedriven_service._heartbeat_loop = MagicMock()
        messagedriven_service.keep_running = False

        messagedriven_service._start_threads = MagicMock()
        messagedriven_service._start_thread = MagicMock()
        messagedriven_service.start()

        messagedriven_service._start_threads.assert_called_with(['q2'])
        messagedriven_service._start_thread.assert_called_with(['q1', 'q3'])
Exemplo n.º 13
0
def init():
    if __name__ == "__main__":
        connect()
        MessagedrivenService(SERVICEDEFINITION, "KafkaProducer").start()