Ejemplo n.º 1
0
    def test_job_executed(self):
        from pseud._tornado import async_sleep
        from pseud.interfaces import OK, VERSION, WORK
        from pseud.packer import Packer
        peer_routing_id = b'echo'
        endpoint = 'ipc://{}'.format(self.__class__.__name__).encode()
        socket = self.make_one_server_socket(peer_routing_id, endpoint)
        client = self.make_one_client(peer_routing_id,
                                      io_loop=self.io_loop)
        client.connect(endpoint)

        stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop)
        future = client.please.do_that_job(1, 2, 3, b=4)
        yield async_sleep(self.io_loop, .1)
        request = yield tornado.gen.Task(stream.on_recv)
        client_routing_id, delimiter, version, uid, message_type, message =\
            request
        assert delimiter == b''
        assert version == VERSION
        assert uid
        # check it is a real uuid
        uuid.UUID(bytes=uid)
        assert message_type == WORK
        locator, args, kw = Packer().unpackb(message)
        assert locator == 'please.do_that_job'
        assert args == (1, 2, 3)
        assert kw == {'b': 4}
        reply = [
            client_routing_id, b'', version, uid, OK, Packer().packb(True)]
        yield tornado.gen.Task(stream.send_multipart, reply)
        result = yield future
        assert result is True
        assert not client.future_pool
        client.stop()
        stream.close()
Ejemplo n.º 2
0
    def test_job_server_never_reply(self):
        from pseud._tornado import async_sleep
        from pseud.interfaces import VERSION, WORK
        from pseud.packer import Packer
        peer_routing_id = b'echo'
        endpoint = 'ipc://{}'.format(self.__class__.__name__).encode()
        socket = self.make_one_server_socket(peer_routing_id, endpoint)
        client = self.make_one_client(peer_routing_id,
                                      timeout=1,
                                      io_loop=self.io_loop)
        client.connect(endpoint)

        stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop)
        future = client.please.do_that_job(1, 2, 3, b=4)
        yield async_sleep(self.io_loop, .1)
        request = yield tornado.gen.Task(stream.on_recv)
        _, delimiter, version, uid, message_type, message = request
        assert delimiter == b''
        assert version == VERSION
        assert uid
        # check it is a real uuid
        uuid.UUID(bytes=uid)
        assert message_type == WORK
        locator, args, kw = Packer().unpackb(message)
        assert locator == 'please.do_that_job'
        assert args == (1, 2, 3)
        assert kw == {'b': 4}
        with pytest.raises(TimeoutError):
            yield future
        assert not client.future_pool
        client.stop()
        stream.close()
Ejemplo n.º 3
0
    def test_untrusted_curve_with_allowed_password(self):
        from pseud import Client, Server
        from pseud.utils import register_rpc
        from pseud._tornado import async_sleep

        client_id = b'john'
        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:8999'
        server_public, server_secret = zmq.curve_keypair()
        client_public, client_secret = zmq.curve_keypair()
        security_plugin = 'untrusted_curve'
        password = b's3cret!'

        client = Client(server_id,
                        security_plugin=security_plugin,
                        public_key=client_public,
                        secret_key=client_secret,
                        peer_public_key=server_public,
                        user_id=client_id,
                        password=password,
                        io_loop=self.io_loop)

        server = Server(server_id,
                        security_plugin=security_plugin,
                        public_key=server_public,
                        secret_key=server_secret,
                        io_loop=self.io_loop)

        server.bind(endpoint)
        client.connect(endpoint)
        assert server.socket.mechanism == zmq.CURVE
        assert client.socket.mechanism == zmq.CURVE

        # configure manually authentication backend
        server.auth_backend.user_map[client_id] = password

        yield server.start()
        yield client.start()

        register_rpc(name='string.lower')(str.lower)

        future = client.string.lower('FOO')
        future2 = client.string.lower('FOO_JJ')
        yield async_sleep(self.io_loop, .01)
        future3 = server.send_to(client_id).string.lower('ABC')
        result = yield future
        result2 = yield future2
        result3 = yield future3
        assert result == 'foo'
        assert result2 == 'foo_jj'
        assert result3 == 'abc'
        server.stop()
        client.stop()
Ejemplo n.º 4
0
    def test_untrusted_curve_with_allowed_password_and_client_disconnect(self):
        from pseud import Client, Server
        from pseud._tornado import async_sleep

        client_id = b'john'
        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:8999'
        server_public, server_secret = zmq.curve_keypair()
        client_public, client_secret = zmq.curve_keypair()
        security_plugin = 'untrusted_curve'
        password = b's3cret!'

        client = Client(server_id,
                        security_plugin=security_plugin,
                        public_key=client_public,
                        secret_key=client_secret,
                        peer_public_key=server_public,
                        user_id=client_id,
                        password=password,
                        timeout=1,
                        io_loop=self.io_loop)

        server = Server(server_id,
                        security_plugin=security_plugin,
                        public_key=server_public,
                        secret_key=server_secret,
                        io_loop=self.io_loop)

        server.bind(endpoint)
        client.connect(endpoint)
        assert server.socket.mechanism == zmq.CURVE
        assert client.socket.mechanism == zmq.CURVE

        # configure manually authentication backend
        server.auth_backend.user_map[client_id] = password

        yield server.start()
        yield client.start()

        server.register_rpc(name='string.lower')(str.lower)

        result = yield client.string.lower('FOO')
        assert result == 'foo'
        # Simulate disconnection and reconnection with new identity
        client.disconnect(endpoint)
        client.connect(endpoint)
        yield async_sleep(self.io_loop, .15)
        result = yield client.string.lower('ABC')
        assert result == 'abc'
        server.stop()
        client.stop()
Ejemplo n.º 5
0
    def test_client_can_reconnect(self):
        from pseud import Client, Server
        from pseud._tornado import async_sleep

        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:8989'
        server_public, server_secret = zmq.curve_keypair()
        security_plugin = 'trusted_curve'

        server = Server(server_id, security_plugin=security_plugin,
                        public_key=server_public,
                        secret_key=server_secret,
                        io_loop=self.io_loop)
        server.bind(endpoint)

        bob_public, bob_secret = server.auth_backend.known_identities[b'bob']
        client = Client(server_id,
                        user_id=b'bob',
                        security_plugin=security_plugin,
                        public_key=bob_public,
                        secret_key=bob_secret,
                        peer_public_key=server_public,
                        io_loop=self.io_loop)
        client.connect(endpoint)
        assert server.socket.mechanism == zmq.CURVE
        assert client.socket.mechanism == zmq.CURVE

        yield server.start()
        yield client.start()

        server.register_rpc(name='string.upper')(str.upper)

        result = yield client.string.upper('hello')
        assert result == 'HELLO'

        client.disconnect(endpoint)
        client.connect(endpoint)
        yield async_sleep(self.io_loop, .1)

        result = yield client.string.upper('hello2')
        assert result == 'HELLO2'

        client.stop()
        server.stop()
Ejemplo n.º 6
0
    def test_timeout_and_error_received_later(self):
        from pseud._tornado import async_sleep

        server_id = b'server'
        endpoint = b'inproc://here'
        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)
        server.bind(endpoint)
        client.connect(endpoint)

        future = client.string.doesnotexists('QWERTY')
        future.set_exception(TimeoutError)
        yield async_sleep(self.io_loop, .01)
        # at this point the future is not in the pool of futures,
        # thought we will still received the answer from the server
        assert not client.future_pool

        server.close()
        client.close()
Ejemplo n.º 7
0
    def test_timeout_and_error_received_later(self):
        from pseud._tornado import async_sleep

        server_id = b'server'
        endpoint = b'inproc://here'
        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)
        server.bind(endpoint)
        client.connect(endpoint)

        future = client.string.doesnotexists('QWERTY')
        future.set_exception(TimeoutError)
        yield async_sleep(self.io_loop, .01)
        # at this point the future is not in the pool of futures,
        # thought we will still received the answer from the server
        assert not client.future_pool

        server.close()
        client.close()
Ejemplo n.º 8
0
 def aysnc_task():
     yield async_sleep(self.io_loop, .01)
     raise tornado.gen.Return(True)
Ejemplo n.º 9
0
 def aysnc_task():
     yield async_sleep(self.io_loop, .01)
     raise tornado.gen.Return(True)