Exemplo n.º 1
0
 def __init__(self,
              topic,
              exchange,
              executor,
              endpoints,
              url=None,
              transport=None,
              transport_options=None,
              retry_options=None):
     type_handlers = {
         pr.NOTIFY:
         dispatcher.Handler(self._delayed_process(self._process_notify),
                            validator=functools.partial(pr.Notify.validate,
                                                        response=False)),
         pr.REQUEST:
         dispatcher.Handler(self._delayed_process(self._process_request),
                            validator=pr.Request.validate),
     }
     self._executor = executor
     self._proxy = proxy.Proxy(topic,
                               exchange,
                               type_handlers=type_handlers,
                               url=url,
                               transport=transport,
                               transport_options=transport_options,
                               retry_options=retry_options)
     self._topic = topic
     self._endpoints = dict([(endpoint.name, endpoint)
                             for endpoint in endpoints])
Exemplo n.º 2
0
    def test_response(self):
        barrier = threading.Event()

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

        handlers = {pr.RESPONSE: dispatcher.Handler(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)
Exemplo n.º 3
0
    def test_notify(self):
        barrier = threading.Event()

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

        handlers = {pr.NOTIFY: dispatcher.Handler(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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def __init__(self,
              uuid,
              proxy,
              topics,
              beat_periodicity=NOTIFY_PERIOD,
              worker_expiry=pr.EXPIRES_AFTER):
     super(ProxyWorkerFinder, self).__init__()
     self._proxy = proxy
     self._topics = topics
     self._workers = {}
     self._uuid = uuid
     self._seen_workers = 0
     self._messages_published = 0
     self._worker_expiry = worker_expiry
     self._watch = timeutils.StopWatch(duration=beat_periodicity)
     # TODO(jimbobhickville): this needs to be refactored
     self._proxy.dispatcher.type_handlers.update({
         pr.NOTIFY:
         dispatcher.Handler(self.process_response,
                            validator=functools.partial(pr.Notify.validate,
                                                        response=True)),
     })
Exemplo n.º 6
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: dispatcher.Handler(on_notify),
            pr.RESPONSE: dispatcher.Handler(on_response),
            pr.REQUEST: dispatcher.Handler(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)
Exemplo n.º 7
0
 def test_creation(self):
     on_hello = mock.MagicMock()
     handlers = {'hello': dispatcher.Handler(on_hello)}
     dispatcher.TypeDispatcher(type_handlers=handlers)