Пример #1
0
def test_two_clients():
    with nested(create_test_client(), create_test_client()) as (client_a,
            client_b):
        wait_all(client_a.connect(server.server_address),
                client_b.connect(server.server_address), timeout=0.5)
        _check_ping(client_a, to=client_b)
        _check_ping(client_a, to=client_b, event=True)
Пример #2
0
def test_deduplication():
    with nested(create_mx_server_context(), create_test_client()) as (server_b,
            client):
        server_a = server

        with timedcontext(2):
            wait_all(client.connect(server_a.server_address),
                    client.connect(server_b.server_address), timeout=0.5)
            msg = client.create_message(to=client.instance_id, type=0)
            client.event(msg)
            first = client.receive(timeout=0.5)
            eq_(msg, first)
            try:
                second = client.receive(timeout=0.5)
            except OperationTimedOut:
                pass
            else:
                eq_(first, second)
                assert False, "duplicated message received"
Пример #3
0
def test_query():
    with nested(create_test_client(), create_test_client()) as (client_a,
            client_b):

        with timedcontext(0.5):
            wait_all(client_a.connect(server.server_address),
                    client_b.connect(server.server_address), timeout=0.5)

        th = TestThread(target=partial(_echo, client_b))
        th.setDaemon(True)
        th.start()

        with timedcontext(1.5):
            response = client_a.query(fields={'to': client_b.instance_id},
                    message='nictuniema', type=1136, timeout=1)

            eq_(response.type, 1136)
            eq_(response.message, 'nictuniema')
            eq_(response.from_, client_b.instance_id)
            eq_(response.to, client_a.instance_id)

        with timedcontext(0.5):
            th.join()
Пример #4
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()