示例#1
0
    def test_job_raise(self):
        from pseud.interfaces import ERROR, VERSION, WORK
        from pseud.packer import Packer
        from pseud.utils import register_rpc

        user_id = b'echo'
        endpoint = 'inproc://{}'.format(self.__class__.__name__).encode()

        @register_rpc
        def job_buggy(*args, **kw):
            raise ValueError('too bad')

        server = self.make_one_server(user_id, endpoint)
        socket = self.make_one_client_socket(endpoint)
        stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop)
        work = Packer().packb(('job_buggy', (), {}))
        yield server.start()
        yield tornado.gen.Task(stream.send_multipart,
                               [user_id, b'', VERSION, b'', WORK, work])
        response = yield tornado.gen.Task(stream.on_recv)
        assert response[:-1] == [user_id, b'', VERSION, b'', ERROR]
        klass, message, traceback = Packer().unpackb(response[-1])
        assert klass == 'ValueError'
        assert message == 'too bad'
        assert __file__ in traceback
        server.stop()
示例#2
0
def test_job_raise():
    from pseud.interfaces import ERROR, VERSION, WORK
    from pseud.packer import Packer
    from pseud.utils import register_rpc

    @register_rpc
    def job_buggy(*args, **kw):
        raise ValueError('too bad')

    user_id = 'echo'
    endpoint = 'inproc://{}'.format(__name__)
    server = make_one_server(user_id, endpoint)
    socket = make_one_client_socket(endpoint)
    work = Packer().packb((job_buggy.func_name, (), {}))
    server.start()
    gevent.spawn(socket.send_multipart, [user_id, '', VERSION, '', WORK, work])
    result = gevent.event.AsyncResult()
    gevent.spawn(read_once, socket).link(result)
    response = result.get()
    assert response[:-1] == [user_id, '', VERSION, '', ERROR]
    klass, message, traceback = Packer().unpackb(response[-1])
    assert klass == 'ValueError'
    assert message == 'too bad'
    assert __file__ in traceback
    server.stop()
示例#3
0
async def test_job_executed(loop, unused_tcp_port):
    from pseud.interfaces import OK, VERSION, WORK
    from pseud.packer import Packer
    peer_routing_id = b'echo'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'
    socket = make_one_server_socket(peer_routing_id, endpoint)
    client = make_one_client(peer_routing_id, loop=loop)
    client.connect(endpoint)

    async with client:
        probing = await socket.recv_multipart()
        assert len(probing) == 2
        future = asyncio.ensure_future(client.please.do_that_job(1, 2, 3, b=4))
        request = await socket.recv_multipart()
        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)
        ]
        await socket.send_multipart(reply)
        result = await future
        assert result is True
        assert not client.future_pool
示例#4
0
    def test_job_running(self):
        from pseud.interfaces import EMPTY_DELIMITER, OK, VERSION, WORK
        from pseud.packer import Packer
        from pseud.utils import register_rpc

        user_id = b'echo'
        endpoint = 'inproc://{}'.format(self.__class__.__name__).encode()

        @register_rpc
        def job_success(a, b, c, d=None):
            time.sleep(.2)
            return True

        server = self.make_one_server(user_id, endpoint)
        socket = self.make_one_client_socket(endpoint)
        stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop)
        work = Packer().packb(('job_success', (1, 2, 3), {'d': False}))
        yield tornado.gen.Task(
            stream.send_multipart,
            [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work])
        yield server.start()
        response = yield tornado.gen.Task(stream.on_recv)
        assert response == [
            user_id, EMPTY_DELIMITER, VERSION, b'', OK,
            Packer().packb(True)
        ]
        server.stop()
示例#5
0
def test_job_executed():
    from pseud.interfaces import OK, VERSION, WORK
    from pseud.packer import Packer
    peer_identity = 'echo'
    endpoint = 'tcp://127.0.0.1'
    port, socket = make_one_server_socket(peer_identity, endpoint)

    client = make_one_client(peer_identity)
    client.connect(endpoint + ':{}'.format(port))

    future = client.please.do_that_job(1, 2, 3, b=4)
    request = gevent.spawn(socket.recv_multipart).get()
    routing_id, delimiter, version, uid, message_type, message = request
    assert delimiter == ''
    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 = [routing_id, '', version, uid, OK, Packer().packb(True)]
    gevent.spawn(socket.send_multipart, reply)
    assert future.get() is True
    assert not client.future_pool
    client.stop()
    socket.close()
示例#6
0
 def server_callback(socket, request):
     peer_id, _, version, uid, message_type, message = request
     assert _ == 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 = [peer_id, _, version, uid, OK, Packer().packb(True)]
     socket.send_multipart(reply)
示例#7
0
async def test_job_server_never_reply(loop):
    from pseud.interfaces import VERSION, WORK
    from pseud.packer import Packer
    peer_routing_id = b'echo'
    endpoint = 'ipc://test_job_server_never_reply'
    socket = make_one_server_socket(peer_routing_id, endpoint)
    client = make_one_client(peer_routing_id, timeout=1, loop=loop)
    client.connect(endpoint)

    async with client:
        probing = await socket.recv_multipart()
        assert len(probing) == 2
        future = asyncio.ensure_future(client.please.do_that_job(1, 2, 3, b=4))
        await asyncio.sleep(.1)
        request = await socket.recv_multipart()
        _, 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(asyncio.TimeoutError):
            await future
        assert not client.future_pool
示例#8
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()
示例#9
0
async def test_job_not_found(loop):
    import pseud
    from pseud.interfaces import EMPTY_DELIMITER, ERROR, VERSION, WORK
    from pseud.packer import Packer
    user_id = b'echo'
    endpoint = 'inproc://test_job_not_found'
    server = make_one_server(user_id, endpoint, loop)
    socket = make_one_client_socket(endpoint)
    work = Packer().packb(('thisIsNotAFunction', (), {}))
    async with server:
        await socket.send_multipart(
            [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work])
        response = await socket.recv_multipart()
        assert response[:-1] == [user_id, EMPTY_DELIMITER, VERSION, b'', ERROR]
        klass, message, traceback = Packer().unpackb(response[-1])
        assert klass == 'ServiceNotFoundError'
        assert message == 'thisIsNotAFunction'
        # pseud.__file__ might ends with .pyc
        assert os.path.dirname(pseud.__file__) in traceback
示例#10
0
def test_job_running():
    from pseud.interfaces import OK, VERSION, WORK
    from pseud.utils import register_rpc
    from pseud.packer import Packer

    @register_rpc
    def job_success(a, b, c, d=None):
        time.sleep(.2)
        return True

    user_id = 'echo'
    endpoint = 'inproc://{}'.format(__name__)
    server = make_one_server(user_id, endpoint)
    server.start()
    socket = make_one_client_socket(endpoint)
    work = Packer().packb((job_success.func_name, (1, 2, 3), {'d': False}))
    gevent.spawn(socket.send_multipart, [user_id, '', VERSION, '', WORK, work])
    response = gevent.spawn(read_once, socket).get()
    assert response == [user_id, '', VERSION, '', OK, Packer().packb(True)]
    server.stop()
示例#11
0
def test_job_not_found():
    from pseud.packer import Packer
    import pseud
    from pseud.interfaces import ERROR, VERSION, WORK
    user_id = 'echo'
    endpoint = 'inproc://{}'.format(__name__)
    server = make_one_server(user_id, endpoint)
    socket = make_one_client_socket(endpoint)
    work = Packer().packb(('thisIsNotAFunction', (), {}))
    server.start()
    gevent.spawn(socket.send_multipart, [user_id, '', VERSION, '', WORK, work])
    result = gevent.event.AsyncResult()
    gevent.spawn(read_once, socket).link(result)
    response = result.get()
    assert response[:-1] == [user_id, '', VERSION, '', ERROR]
    klass, message, traceback = Packer().unpackb(response[-1])
    assert klass == 'ServiceNotFoundError'
    assert message == 'thisIsNotAFunction'
    # pseud.__file__ might ends with .pyc
    assert os.path.dirname(pseud.__file__) in traceback
    server.stop()
示例#12
0
 def server_callback(socket, request):
     peer_id, _, version, uid, message_type, message = request
     assert _ == ''
     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 == b'please.do_that_job'
     assert args == (1, 2)
     assert kw == {'b': 5}
示例#13
0
 def test_job_not_found(self):
     import pseud
     from pseud.interfaces import EMPTY_DELIMITER, ERROR, VERSION, WORK
     from pseud.packer import Packer
     user_id = b'echo'
     endpoint = 'inproc://{}'.format(self.__class__.__name__).encode()
     server = self.make_one_server(user_id, endpoint)
     socket = self.make_one_client_socket(endpoint)
     stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop)
     work = Packer().packb(('thisIsNotAFunction', (), {}))
     yield server.start()
     yield tornado.gen.Task(
         stream.send_multipart,
         [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work])
     response = yield tornado.gen.Task(stream.on_recv)
     assert response[:-1] == [user_id, EMPTY_DELIMITER, VERSION, b'', ERROR]
     klass, message, traceback = Packer().unpackb(response[-1])
     assert klass == 'ServiceNotFoundError'
     assert message == 'thisIsNotAFunction'
     # pseud.__file__ might ends with .pyc
     assert os.path.dirname(pseud.__file__) in traceback
     server.stop()
示例#14
0
async def test_job_raise(loop):
    from pseud.interfaces import ERROR, VERSION, WORK
    from pseud.packer import Packer
    from pseud.utils import register_rpc

    user_id = b'echo'
    endpoint = 'inproc://test_job_raise'

    @register_rpc
    def job_buggy(*args, **kw):
        raise ValueError('too bad')

    server = make_one_server(user_id, endpoint, loop)
    socket = make_one_client_socket(endpoint)
    work = Packer().packb(('job_buggy', (), {}))
    async with server:
        await socket.send_multipart([user_id, b'', VERSION, b'', WORK, work])
        response = await socket.recv_multipart()
        assert response[:-1] == [user_id, b'', VERSION, b'', ERROR]
        klass, message, traceback = Packer().unpackb(response[-1])
        assert klass == 'ValueError'
        assert message == 'too bad'
        assert __file__ in traceback
示例#15
0
async def test_job_running(loop):
    from pseud.interfaces import EMPTY_DELIMITER, OK, VERSION, WORK
    from pseud.packer import Packer
    from pseud.utils import register_rpc

    user_id = b'echo'
    endpoint = 'inproc://test_job_running'

    @register_rpc
    def job_success(a, b, c, d=None):
        time.sleep(.2)
        return True

    server = make_one_server(user_id, endpoint, loop)
    socket = make_one_client_socket(endpoint)
    work = Packer().packb(('job_success', (1, 2, 3), {'d': False}))
    await socket.send_multipart(
        [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work])
    async with server:
        response = await socket.recv_multipart()
        assert response == [
            user_id, EMPTY_DELIMITER, VERSION, b'', OK,
            Packer().packb(True)
        ]
示例#16
0
def test_packer_normal():
    from pseud.packer import Packer
    packer = Packer()
    assert packer.unpackb(packer.packb('data')) == 'data'
示例#17
0
def test_packer_failure():
    import msgpack
    from pseud.packer import Packer
    packer = Packer()
    with pytest.raises(msgpack.ExtraData):
        packer.unpackb(b'--')
示例#18
0
def test_datetime_with_timezone():
    from pseud.packer import Packer

    date = datetime.datetime(2003, 9, 27, 9, 40, 1, 521290, tzinfo=tzlocal())
    assert Packer().unpackb(Packer().packb(date)) == date
示例#19
0
def test_packer_translation():
    import msgpack
    from pseud.packer import Packer

    class A(object):
        def __init__(self, arg):
            self.arg = arg

        def __eq__(self, other):
            return self.arg == other.arg

    table = {5: (A,
                 lambda obj: msgpack.packb(obj.arg),
                 lambda data: A(msgpack.unpackb(data)))}
    packer = Packer(translation_table=table)
    assert packer.packb({b'key': A(b'--')}) == (
        b'\x81\xc4\x03key\xc7\x03\x05\xa2--')
    assert packer.unpackb(
        packer.packb({'key': A(b'arg')})) == {'key': A(b'arg')}

    packer.register_ext_handler(
        0, A, lambda obj: b'overidden', lambda data: A(b'arbitrary'))
    # Checks pack_cache is valid
    assert packer.unpackb(
        packer.packb({'key': A(b'arg')})) != {'key': A(b'arg')}

    # Mostly for coverage of error paths.
    class B(object):
        pass

    # Two different error paths.
    with pytest.raises(TypeError):
        packer.packb(B())
    with pytest.raises(TypeError):
        packer.packb(B())

    dumb_packer = Packer()
    dumb_packer.unpackb(packer.packb(A('')))
示例#20
0
def test_packer_failure():
    import msgpack
    from pseud.packer import Packer
    packer = Packer()
    with pytest.raises(msgpack.ExtraData):
        packer.unpackb(b'--')
示例#21
0
def test_packer_normal():
    from pseud.packer import Packer
    packer = Packer()
    assert packer.unpackb(packer.packb('data')) == 'data'
示例#22
0
def test_datetime_without_timezone():
    from pseud.packer import Packer

    date = datetime.datetime(2003, 9, 27, 9, 40, 1, 521290)
    assert Packer().unpackb(Packer().packb(date)) == date
示例#23
0
def test_packer_translation():
    import msgpack
    from pseud.packer import Packer

    class A(object):
        def __init__(self, arg):
            self.arg = arg

        def __eq__(self, other):
            return self.arg == other.arg

    table = {
        5: (A, lambda obj: msgpack.packb(obj.arg),
            lambda data: A(msgpack.unpackb(data)))
    }
    packer = Packer(translation_table=table)
    assert packer.packb({b'key':
                         A(b'--')}) == (b'\x81\xc4\x03key\xc7\x03\x05\xa2--')
    assert packer.unpackb(packer.packb({'key': A(b'arg')})) == {
        'key': A(b'arg')
    }

    packer.register_ext_handler(0, A, lambda obj: b'overidden',
                                lambda data: A(b'arbitrary'))
    # Checks pack_cache is valid
    assert packer.unpackb(packer.packb({'key': A(b'arg')})) != {
        'key': A(b'arg')
    }

    # Mostly for coverage of error paths.
    class B(object):
        pass

    # Two different error paths.
    with pytest.raises(TypeError):
        packer.packb(B())
    with pytest.raises(TypeError):
        packer.packb(B())

    dumb_packer = Packer()
    dumb_packer.unpackb(packer.packb(A('')))