Esempio n. 1
0
    def setUp(self):
        super(TestServer, self).setUp()
        self.server_topic = 'server-topic'
        self.server_exchange = 'server-exchange'
        self.broker_url = 'test-url'
        self.task = utils.TaskOneArgOneReturn()
        self.task_uuid = 'task-uuid'
        self.task_args = {'x': 1}
        self.task_action = 'execute'
        self.reply_to = 'reply-to'
        self.endpoints = [
            ep.Endpoint(task_cls=utils.TaskOneArgOneReturn),
            ep.Endpoint(task_cls=utils.TaskWithFailure),
            ep.Endpoint(task_cls=utils.ProgressingTask)
        ]

        # patch classes
        self.proxy_mock, self.proxy_inst_mock = self.patchClass(
            server.proxy, 'Proxy')
        self.response_mock, self.response_inst_mock = self.patchClass(
            server.pr, 'Response')

        # other mocking
        self.proxy_inst_mock.is_running = True
        self.executor_mock = mock.MagicMock(name='executor')
        self.message_mock = mock.MagicMock(name='message')
        self.message_mock.properties = {
            'correlation_id': self.task_uuid,
            'reply_to': self.reply_to,
            'type': pr.REQUEST
        }
        self.master_mock.attach_mock(self.executor_mock, 'executor')
        self.master_mock.attach_mock(self.message_mock, 'message')
    def setUp(self):
        super(TestWorkerTaskExecutor, self).setUp()
        self.task = test_utils.DummyTask()
        self.task_uuid = 'task-uuid'
        self.task_args = {'a': 'a'}
        self.task_result = 'task-result'
        self.task_failures = {}
        self.timeout = 60
        self.broker_url = 'broker-url'
        self.executor_uuid = 'executor-uuid'
        self.executor_exchange = 'executor-exchange'
        self.executor_topic = 'test-topic1'
        self.proxy_started_event = threading_utils.Event()

        # patch classes
        self.proxy_mock, self.proxy_inst_mock = self.patchClass(
            executor.proxy, 'Proxy')
        self.request_mock, self.request_inst_mock = self.patchClass(
            executor.pr, 'Request', autospec=False)

        # other mocking
        self.proxy_inst_mock.start.side_effect = self._fake_proxy_start
        self.proxy_inst_mock.stop.side_effect = self._fake_proxy_stop
        self.request_inst_mock.uuid = self.task_uuid
        self.request_inst_mock.expired = False
        self.request_inst_mock.task_cls = self.task.name
        self.wait_for_any_mock = self.patch(
            'taskflow.engines.worker_based.executor.async_utils.wait_for_any')
        self.message_mock = mock.MagicMock(name='message')
        self.message_mock.properties = {
            'correlation_id': self.task_uuid,
            'type': pr.RESPONSE
        }
Esempio n. 3
0
    def test_claim_lost_custom_handler(self):
        job = self._post_claim_job('test')
        f = self._make_dummy_flow(10)
        e = self._make_engine(f)

        handler = mock.MagicMock()
        ran_states = []
        try_destroy = True
        destroyed_at = -1
        with claims.CheckingClaimListener(e,
                                          job,
                                          self.board,
                                          self.board.name,
                                          on_job_loss=handler):
            for i, state in enumerate(e.run_iter()):
                ran_states.append(state)
                if state == states.SCHEDULING and try_destroy:
                    destroyed = bool(self._destroy_locks())
                    if destroyed:
                        destroyed_at = i
                        try_destroy = False

        self.assertTrue(handler.called)
        self.assertEqual(10, ran_states.count(states.SCHEDULING))
        self.assertNotEqual(-1, destroyed_at)

        after_states = ran_states[destroyed_at:]
        self.assertGreater(0, len(after_states))
Esempio n. 4
0
    def test_notify(self):
        barrier = threading_utils.Event()

        on_notify = mock.MagicMock()
        on_notify.side_effect = lambda *args, **kwargs: barrier.set()

        handlers = {pr.NOTIFY: on_notify}
        p = proxy.Proxy(TEST_TOPIC,
                        TEST_EXCHANGE,
                        handlers,
                        transport='memory',
                        transport_options={
                            'polling_interval': POLLING_INTERVAL,
                        })

        t = threading_utils.daemon_thread(p.start)
        t.start()
        p.wait()
        p.publish(pr.Notify(), TEST_TOPIC)

        self.assertTrue(barrier.wait(test_utils.WAIT_TIMEOUT))
        p.stop()
        t.join()

        self.assertTrue(on_notify.called)
        on_notify.assert_called_with({}, mock.ANY)
Esempio n. 5
0
    def test_response(self):
        barrier = threading_utils.Event()

        on_response = mock.MagicMock()
        on_response.side_effect = lambda *args, **kwargs: barrier.set()

        handlers = {pr.RESPONSE: on_response}
        p = proxy.Proxy(TEST_TOPIC,
                        TEST_EXCHANGE,
                        handlers,
                        transport='memory',
                        transport_options={
                            'polling_interval': POLLING_INTERVAL,
                        })

        t = threading_utils.daemon_thread(p.start)
        t.start()
        p.wait()
        resp = pr.Response(pr.RUNNING)
        p.publish(resp, TEST_TOPIC)

        self.assertTrue(barrier.wait(test_utils.WAIT_TIMEOUT))
        self.assertTrue(barrier.is_set())
        p.stop()
        t.join()

        self.assertTrue(on_response.called)
        on_response.assert_called_with(resp.to_dict(), mock.ANY)
Esempio n. 6
0
 def test_single_topic_worker(self):
     finder = worker_types.ProxyWorkerFinder('me', mock.MagicMock(), [])
     w, emit = finder._add('dummy-topic', [utils.DummyTask])
     self.assertIsNotNone(w)
     self.assertTrue(emit)
     self.assertEqual(1, finder._total_workers())
     w2 = finder.get_worker_for_task(utils.DummyTask)
     self.assertEqual(w.identity, w2.identity)
 def test_no_connect_channel(self, mock_socket_factory):
     mock_sock = mock.MagicMock()
     mock_socket_factory.return_value = mock_sock
     mock_sock.connect.side_effect = socket.error(errno.ECONNREFUSED,
                                                  'broken')
     c = pu.Channel(2222, b"me", b"secret")
     self.assertRaises(socket.error, c.send, "hi")
     self.assertTrue(c.dead)
     self.assertTrue(mock_sock.close.called)
Esempio n. 8
0
    def test_creation_with_custom_executor(self):
        executor_mock = mock.MagicMock(name='executor')
        self.worker(executor=executor_mock)

        master_mock_calls = [
            mock.call.Server(self.topic, self.exchange, executor_mock, [],
                             url=self.broker_url)
        ]
        self.assertEqual(self.master_mock.mock_calls, master_mock_calls)
Esempio n. 9
0
 def test_failed_ack(self):
     on_hello = mock.MagicMock()
     handlers = {'hello': dispatcher.Handler(on_hello)}
     d = dispatcher.TypeDispatcher(type_handlers=handlers)
     msg = mock_acked_message(ack_ok=False, properties={'type': 'hello'})
     d.on_message("", msg)
     self.assertTrue(msg.ack_log_error.called)
     self.assertFalse(msg.acknowledged)
     self.assertFalse(on_hello.called)
Esempio n. 10
0
 def test_on_message(self):
     on_hello = mock.MagicMock()
     handlers = {'hello': on_hello}
     d = dispatcher.TypeDispatcher(handlers)
     msg = mock_acked_message(properties={'type': 'hello'})
     d.on_message("", msg)
     self.assertTrue(on_hello.called)
     self.assertTrue(msg.ack_log_error.called)
     self.assertTrue(msg.acknowledged)
Esempio n. 11
0
    def test_run_with_custom_executor(self):
        executor_mock = mock.MagicMock(name='executor')
        self.worker(reset_master_mock=True,
                    executor=executor_mock).run()

        master_mock_calls = [
            mock.call.server.start()
        ]
        self.assertEqual(self.master_mock.mock_calls, master_mock_calls)
Esempio n. 12
0
 def test_expiry(self, mock_now):
     finder = worker_types.ProxyWorkerFinder('me',
                                             mock.MagicMock(), [],
                                             worker_expiry=60)
     w, emit = finder._add('dummy-topic', [utils.DummyTask])
     w.last_seen = 0
     mock_now.side_effect = [120]
     gone = finder.clean()
     self.assertEqual(0, finder.total_workers)
     self.assertEqual(1, gone)
Esempio n. 13
0
 def test_multi_different_topic_workers(self):
     finder = worker_types.ProxyWorkerFinder('me', mock.MagicMock(), [])
     added = []
     added.append(finder._add('dummy-topic', [utils.DummyTask]))
     added.append(finder._add('dummy-topic-2', [utils.DummyTask]))
     added.append(finder._add('dummy-topic-3', [utils.NastyTask]))
     self.assertEqual(3, finder._total_workers())
     w = finder.get_worker_for_task(utils.NastyTask)
     self.assertEqual(added[-1][0].identity, w.identity)
     w = finder.get_worker_for_task(utils.DummyTask)
     self.assertIn(w.identity, [w_a[0].identity for w_a in added[0:2]])
Esempio n. 14
0
 def test_multi_same_topic_workers(self):
     finder = worker_types.ProxyWorkerFinder('me', mock.MagicMock(), [])
     w, emit = finder._add('dummy-topic', [utils.DummyTask])
     self.assertIsNotNone(w)
     self.assertTrue(emit)
     w2, emit = finder._add('dummy-topic-2', [utils.DummyTask])
     self.assertIsNotNone(w2)
     self.assertTrue(emit)
     w3 = finder.get_worker_for_task(
         reflection.get_class_name(utils.DummyTask))
     self.assertIn(w3.identity, [w.identity, w2.identity])
Esempio n. 15
0
    def test_on_progress(self):
        progress_callback = mock.MagicMock(name='progress_callback')
        request = self.request(task=self.task,
                               progress_callback=progress_callback)
        request.on_progress('event_data', 0.0)
        request.on_progress('event_data', 1.0)

        expected_calls = [
            mock.call(self.task, 'event_data', 0.0),
            mock.call(self.task, 'event_data', 1.0)
        ]
        self.assertEqual(progress_callback.mock_calls, expected_calls)
Esempio n. 16
0
    def test_process_request_endpoint_not_found(self, to_mock):
        failure_dict = {
            'failure': 'failure',
        }
        to_mock.return_value = failure_dict
        request = self.make_request(task=mock.MagicMock(name='<unknown>'))

        # create server and process request
        s = self.server(reset_master_mock=True)
        s._process_request(request, self.message_mock)

        # check calls
        master_mock_calls = [
            mock.call.Response(pr.FAILURE, result=failure_dict),
            mock.call.proxy.publish(self.response_inst_mock,
                                    self.reply_to,
                                    correlation_id=self.task_uuid)
        ]
        self.master_mock.assert_has_calls(master_mock_calls)
Esempio n. 17
0
    def test_publish(self):
        msg_mock = mock.MagicMock()
        msg_data = 'msg-data'
        msg_mock.to_dict.return_value = msg_data
        routing_key = 'routing-key'
        task_uuid = 'task-uuid'

        p = self.proxy(reset_master_mock=True)
        p.publish(msg_mock, routing_key, correlation_id=task_uuid)

        mock_producer = mock.call.connection.Producer()
        master_mock_calls = self.proxy_publish_calls([
            mock_producer.__enter__().publish(body=msg_data,
                                              routing_key=routing_key,
                                              exchange=self.exchange_inst_mock,
                                              correlation_id=task_uuid,
                                              declare=[self.queue_inst_mock],
                                              type=msg_mock.TYPE,
                                              reply_to=None)
        ], routing_key)
        self.master_mock.assert_has_calls(master_mock_calls)
Esempio n. 18
0
    def setUp(self):
        super(TestProxy, self).setUp()
        self.topic = 'test-topic'
        self.broker_url = 'test-url'
        self.exchange = 'test-exchange'
        self.timeout = 5
        self.de_period = proxy.DRAIN_EVENTS_PERIOD

        # patch classes
        self.conn_mock, self.conn_inst_mock = self.patchClass(
            proxy.kombu, 'Connection')
        self.exchange_mock, self.exchange_inst_mock = self.patchClass(
            proxy.kombu, 'Exchange')
        self.queue_mock, self.queue_inst_mock = self.patchClass(
            proxy.kombu, 'Queue')
        self.producer_mock, self.producer_inst_mock = self.patchClass(
            proxy.kombu, 'Producer')

        # connection mocking
        def _ensure(obj, func, *args, **kwargs):
            return func

        self.conn_inst_mock.drain_events.side_effect = [
            socket.timeout, socket.timeout, KeyboardInterrupt
        ]
        self.conn_inst_mock.ensure = mock.MagicMock(side_effect=_ensure)

        # connections mocking
        self.connections_mock = self.patch(
            "taskflow.engines.worker_based.proxy.kombu.connections",
            attach_as='connections')
        self.connections_mock.__getitem__().acquire().__enter__.return_value =\
            self.conn_inst_mock

        # producers mocking
        self.conn_inst_mock.Producer.return_value.__enter__ = mock.MagicMock()
        self.conn_inst_mock.Producer.return_value.__exit__ = mock.MagicMock()

        # consumer mocking
        self.conn_inst_mock.Consumer.return_value.__enter__ = mock.MagicMock()
        self.conn_inst_mock.Consumer.return_value.__exit__ = mock.MagicMock()

        # other mocking
        self.on_wait_mock = mock.MagicMock(name='on_wait')
        self.master_mock.attach_mock(self.on_wait_mock, 'on_wait')

        # reset master mock
        self.resetMasterMock()
Esempio n. 19
0
    def test_multi_message(self):
        message_count = 30
        barrier = latch.Latch(message_count)
        countdown = lambda data, message: barrier.countdown()

        on_notify = mock.MagicMock()
        on_notify.side_effect = countdown

        on_response = mock.MagicMock()
        on_response.side_effect = countdown

        on_request = mock.MagicMock()
        on_request.side_effect = countdown

        handlers = {
            pr.NOTIFY: on_notify,
            pr.RESPONSE: on_response,
            pr.REQUEST: on_request,
        }
        p = proxy.Proxy(TEST_TOPIC,
                        TEST_EXCHANGE,
                        handlers,
                        transport='memory',
                        transport_options={
                            'polling_interval': POLLING_INTERVAL,
                        })

        t = threading_utils.daemon_thread(p.start)
        t.start()
        p.wait()

        for i in range(0, message_count):
            j = i % 3
            if j == 0:
                p.publish(pr.Notify(), TEST_TOPIC)
            elif j == 1:
                p.publish(pr.Response(pr.RUNNING), TEST_TOPIC)
            else:
                p.publish(
                    pr.Request(test_utils.DummyTask("dummy_%s" % i),
                               uuidutils.generate_uuid(), pr.EXECUTE, [],
                               None), TEST_TOPIC)

        self.assertTrue(barrier.wait(test_utils.WAIT_TIMEOUT))
        self.assertEqual(0, barrier.needed)
        p.stop()
        t.join()

        self.assertTrue(on_notify.called)
        self.assertTrue(on_response.called)
        self.assertTrue(on_request.called)

        self.assertEqual(10, on_notify.call_count)
        self.assertEqual(10, on_response.call_count)
        self.assertEqual(10, on_request.call_count)

        call_count = sum([
            on_notify.call_count,
            on_response.call_count,
            on_request.call_count,
        ])
        self.assertEqual(message_count, call_count)
Esempio n. 20
0
 def test_creation(self):
     on_hello = mock.MagicMock()
     handlers = {'hello': on_hello}
     dispatcher.TypeDispatcher(handlers)
Esempio n. 21
0
 def test_creation(self):
     on_hello = mock.MagicMock()
     handlers = {'hello': dispatcher.Handler(on_hello)}
     dispatcher.TypeDispatcher(type_handlers=handlers)