Exemplo n.º 1
0
def check_query(backend_factory, backend_kwargs={}):

    message = pickle.dumps('some data')
    backend_kwargs.setdefault('type', 380)

    with nested(create_test_client(), closing(backend_factory(
        **backend_kwargs))) as (client, backend):

        with timedcontext(4):
            client.connect(server.server_address, sync=True)

            raises(OperationFailed)(lambda: client.query(fields={'to':
                backend.instance_id, 'workflow': 'some workflow'},
                message=message, type=1136, timeout=1))()

            backend.connect(server.server_address, sync=True)

            th = TestThread(target=backend.handle_one)
            th.setDaemon(True)
            th.start()

            response = client.query(fields={'to': backend.instance_id,
                'workflow': 'some workflow'}, message=message, type=1136,
                timeout=1)

            eq_(response.message, message)
            eq_(response.from_, backend.instance_id)
            eq_(response.to, client.instance_id)
            eq_(response.workflow, 'some workflow')

            th.join()
Exemplo n.º 2
0
def check_query_retransmission(how, notify):
    class MultiplexerBackendSubclass(MultiplexerBackend):
        __first = True
        __mxmsg = None

        def _be_nice(self):
            self.send_message(message=self.__mxmsg.message,
                    type=TestMessageTypes.TEST_RESPONSE)
            self.shutdown()

        def _no_response(self):
            pass

        def _raise_exception(self):
            raise Exception("there never will be any response")

        def _send_other(self):
            self.send_message(type=MessageTypes.PING, to=self.instance_id,
                    references=567890)

        def handle_message(self, mxmsg):
            if notify:
                self.notify_started()
            eq_(mxmsg.type, TestMessageTypes.TEST_REQUEST)
            self.__mxmsg = mxmsg
            if self.__first:
                self.__first = False
                getattr(self, how)()
            else:
                self._be_nice()

    message = pickle.dumps('some data')
    backend_factory = MultiplexerBackendSubclass
    backend_kwargs = {'type': TestPeerTypes.TEST_SERVER}

    with nested(create_test_client(), closing(backend_factory(
        **backend_kwargs))) as (client, backend):

        wait_all(client.connect(server.server_address),
                backend.connect(server.server_address), timeout=0.5)

        th = TestThread(target=backend.start)
        th.setDaemon(True)
        th.start()

        with timedcontext(2):
            # backend replies at the first time according to `how`, the
            # second time it's all right
            response = client.query(fields={'workflow': 'some workflow'},
                    message=message, type=TestMessageTypes.TEST_REQUEST,
                    timeout=0.5)

        eq_(response.message, message)
        eq_(response.type, TestMessageTypes.TEST_RESPONSE)
        eq_(response.from_, backend.instance_id)
        eq_(response.to, client.instance_id)
        eq_(response.workflow, 'some workflow')

        th.join()

    with nested(create_test_client(), closing(backend_factory(
        **backend_kwargs))) as (client, backend):

        wait_all(client.connect(server.server_address),
                backend.connect(server.server_address), timeout=0.5)

        th = TestThread(target=backend.handle_one)
        th.setDaemon(True)
        th.start()

        try:
            with timedcontext(2):
                # backend replies at the first time according to `how`,
                # then it's gone (handle_one is used in the backend thread)
                response = client.query(fields={'workflow':
                    'some workflow'}, message=message,
                    type=TestMessageTypes.TEST_REQUEST, timeout=0.5,
                    skip_resend=True)

        except BackendError:
            assert how == '_raise_exception'

        except OperationTimedOut:
            assert how in ('_no_response', '_send_other')

        else:
            assert how == '_be_nice'
            eq_(response.message, message)
            eq_(response.type, TestMessageTypes.TEST_RESPONSE)
            eq_(response.from_, backend.instance_id)
            eq_(response.to, client.instance_id)
            eq_(response.workflow, 'some workflow')

        th.join()