Beispiel #1
0
    def test_with_client(self):
        addr = get_random_ipc_socket()
        with utils.TestingServer(methods=self.get_methods(), addresses=addr):
            client = Client(addr)
            assert client.method1() == 'Hello, world!'

            with pytest.raises(RemoteException):
                client.method1('this', 'does not', 'accept arguments!')

            assert client.method2(u'WORLD') == u'Hello, WORLD!'
            assert client.method2(name=u'WORLD') == u'Hello, WORLD!'
            assert client.method2(name=u'Ẅøřłđ') == u'Hello, Ẅøřłđ!'

            assert client.method3('Hi', 'man') == 'Hi, man!'
            assert client.method3(greeting='Hi', name='man') == 'Hi, man!'
            assert client.method3(greeting='Hi') == 'Hi, world!'

            with pytest.raises(RemoteException):
                client.method3('HELLO', greeting='Hi')

            assert client.get_list() == ['this', 'is', 'a', 'list']
            assert client.get_dict() == {'this': 'is', 'a': 'dict'}

            with pytest.raises(RemoteException):
                client.raise_value_error()

            ## Ok, the method doesn't exist, no big deal..
            with pytest.raises(RemoteException):
                client.no_such_method()

            client.method1()  # The server must still be alive here..
Beispiel #2
0
    def test_call_with_multi_args(self):
        addr = get_random_ipc_socket()
        with utils.TestingServer(methods=self.get_methods(), addresses=addr):
            context = zmq.Context()
            socket = context.socket(zmq.REQ)
            socket.connect(addr)

            socket.send(msgpack.packb({
                'm': 'method3',
                'k': {'name': 'WORLD'},
            }, encoding='utf-8'))
            response = msgpack.unpackb(socket.recv(), encoding='utf-8')
            assert response == {'r': 'Hello, WORLD!'}

            socket.send(msgpack.packb({
                'm': 'method3',
                'k': {'greeting': 'HOWDY'},
            }, encoding='utf-8'))
            response = msgpack.unpackb(socket.recv(), encoding='utf-8')
            assert response == {'r': 'HOWDY, world!'}

            socket.send(msgpack.packb({
                'm': 'method3',
                'k': {'greeting': 'HOWDY', 'name': 'MAN'},
            }, encoding='utf-8'))
            response = msgpack.unpackb(socket.recv(), encoding='utf-8')
            assert response == {'r': 'HOWDY, MAN!'}

            socket.send(msgpack.packb({
                'm': 'method3',
                'a': ('hey', 'man'),
            }, encoding='utf-8'))
            response = msgpack.unpackb(socket.recv(), encoding='utf-8')
            assert response == {'r': 'hey, man!'}
Beispiel #3
0
    def test_server_unavailable(self):
        # retries 3, timeout 3
        addr = get_random_ipc_socket()
        c = pirate.Lazy(retries=1, timeout=0, address=addr)

        with pytest.raises(exceptions.ServerUnavailable):
            c.hello()
Beispiel #4
0
    def test_server_active(self):
        addr = get_random_ipc_socket()

        with utils.TestingServer(self.get_methods(), addr) as proc:
            proc.join(1)

            # retries 3, timeout 3
            c = pirate.Lazy(address=addr)
            assert 'world' == c.hello()
Beispiel #5
0
 def test_simple_call(self):
     addr = get_random_ipc_socket()
     with utils.TestingServer(methods=self.get_methods(), addresses=addr):
         context = zmq.Context()
         socket = context.socket(zmq.REQ)
         socket.connect(addr)
         socket.send(msgpack.packb({
             'm': 'method1',
         }, encoding='utf-8'))
         response = msgpack.unpackb(socket.recv(), encoding='utf-8')
         assert response == {'r': 'Hello, world!'}
Beispiel #6
0
 def test_call_with_kwargs(self):
     addr = get_random_ipc_socket()
     with utils.TestingServer(methods=self.get_methods(), addresses=addr):
         context = zmq.Context()
         socket = context.socket(zmq.REQ)
         socket.connect(addr)
         socket.send(msgpack.packb({
             'm': 'method2',
             'k': {'name': 'WORLD'},
         }, encoding='utf-8'))
         response = msgpack.unpackb(socket.recv(), encoding='utf-8')
         assert response == {'r': 'Hello, WORLD!'}
Beispiel #7
0
    def test_list_methods(self):
        addr = get_random_ipc_socket()
        with utils.TestingServer(
                self.get_methods(), addr,
                middleware=[IntrospectionMiddleware()]):
            client = IntrospectableClient(addr)
            exposed_methods = dir(client)
            for meth in ('method1', 'method2', 'method3'):
                assert meth in exposed_methods

            assert client.method1.__doc__ == \
                'Returns the string "Hello, world!".'
            assert client.method2.__doc__ == 'Docstring of method2()'
            assert client.method3.__doc__ == 'Docstring of method3()'
Beispiel #8
0
    def test_concurrent_calls(self):
        addr = get_random_ipc_socket()
        with utils.TestingServer(methods=self.get_methods(), addresses=addr):

            context = zmq.Context()
            sockets = {}
            socket_ids = range(10)

            random.shuffle(socket_ids)
            for i in socket_ids:
                sockets[i] = context.socket(zmq.REQ)
                sockets[i].connect(addr)

            random.shuffle(socket_ids)
            for i in socket_ids:
                sockets[i].send(msgpack.packb({
                    'm': 'method2',
                    'a': ['socket {0}'.format(i)],
                }, encoding='utf-8'))

            random.shuffle(socket_ids)
            for i in socket_ids:
                response = msgpack.unpackb(sockets[i].recv(), encoding='utf-8')
                assert response == {'r': 'Hello, socket {0}!'.format(i)}