Esempio n. 1
0
def test_outgoing_request_cancellation_and_setting():
    '''Tests cancelling requests.'''
    called = 0

    def on_done(req):
        nonlocal called
        called += 1

    with MyRPCProcessor() as rpc:
        request = RPCRequestOut('add_async', [1], on_done)
        rpc.send_request(request)
        assert request.request_id in rpc.requests
        request.cancel()
        rpc.yield_to_loop()
        assert called == 1
        assert request.cancelled()
        assert not rpc.requests
        with pytest.raises(asyncio.CancelledError):
            request.result()

        request = RPCRequestOut('add_async', [1], on_done)
        rpc.send_request(request)
        assert request.request_id in rpc.requests
        request.set_result(4)
        rpc.yield_to_loop()
        assert called == 2
        assert request.result() == 4
        assert not rpc.requests

        request = RPCRequestOut('add_async', [1], on_done)
        rpc.send_request(request)
        assert request.request_id in rpc.requests
        request.set_exception(ValueError())
        rpc.yield_to_loop()
        assert called == 3
        assert isinstance(request.exception(), ValueError)
        with pytest.raises(ValueError):
            request.result()
        assert not rpc.requests
Esempio n. 2
0
def test_RPCRequest():
    request = RPCRequestOut('method', [1], None)
    assert request.method == 'method'
    assert request.args == [1]
    assert request.request_id == 0
    assert not request.is_notification()
    assert repr(request) == "RPCRequest('method', [1], 0)"
    request = RPCRequestOut('method', (1, ), None)
    assert request.method == 'method'
    assert request.args == (1, )
    assert request.request_id == 1
    assert not request.is_notification()
    assert repr(request) == "RPCRequest('method', (1,), 1)"
    request = RPCRequestOut('foo', {"bar": 1}, None)
    assert request.method == 'foo'
    assert request.args == {"bar": 1}
    assert request.request_id == 2
    assert not request.is_notification()
    assert repr(request) == "RPCRequest('foo', {'bar': 1}, 2)"
    request = RPCRequest('foo', [], None)
    assert request.method == 'foo'
    assert request.args == []
    assert request.request_id is None
    assert request.is_notification()
    assert repr(request) == "RPCRequest('foo', [], None)"
    request = RPCRequest('foo', (), None)
    assert request.method == 'foo'
    assert request.args == ()
    assert request.request_id is None
    assert request.is_notification()
    assert repr(request) == "RPCRequest('foo', (), None)"
    # Check {} is preserved (different call semantics)
    for request in [RPCRequest('add', {}, 0), RPCRequestOut('add', {}, None)]:
        assert request.method == 'add'
        assert request.args == {}
        request = RPCRequest('add', {}, 0)
    # Check None gives []
    for request in [
            RPCRequest('add', None, 0),
            RPCRequestOut('add', None, None)
    ]:
        request = RPCRequest('add', None, 0)
        assert request.method == 'add'
        assert request.args == []

    loop = asyncio.get_event_loop()
    # Result setting
    valid = False

    def on_done(req):
        nonlocal valid
        valid = req is request

    # Test args good type
    with pytest.raises(ValueError):
        RPCRequestOut('method', 0, on_done)

    request = RPCRequestOut('method', [0], on_done)
    assert not request.done()
    with pytest.raises(asyncio.InvalidStateError):
        request.result()
    assert not request.done()
    request.set_result(42)

    assert not valid  # Not scheduled yet
    loop.run_until_complete(asyncio.sleep(0))
    assert valid

    assert request.result() == 42
    with pytest.raises(asyncio.InvalidStateError):
        request.set_result(35)
    assert request.result() == 42

    # Result setting
    request = RPCRequestOut('method', None, on_done)
    loop.call_later(0.001, request.set_result, 45)
    valid = False
    assert loop.run_until_complete(request) == 45
    assert valid
    assert request.result() == 45

    request = RPCRequestOut('method', None, on_done)
    loop = asyncio.get_event_loop()
    request.set_result(46)
    assert loop.run_until_complete(request) == 46