def test_direct_set_etc(): loop = asyncio.get_event_loop() done = set() def on_done(request): done.add(request.request_id) # Send 4 requests requests = [ RPCRequestOut('some_call', [1], on_done), RPCRequestOut('some_call', [2], on_done), RPCRequestOut('some_call', [3], on_done), RPCRequestOut('some_call', [4], on_done), ] with MyRPCProcessor() as rpc: for request in requests: rpc.send_request(request) # Interfere with all apart from one requests[0].set_result(6) response = RPCResponse(7, requests[1].request_id) rpc.message_received(rpc.protocol.response_message(response)) requests[2].cancel() requests[3].set_exception(ValueError()) rpc.yield_to_loop() # Process done functions assert done == {requests[n].request_id for n in range(len(requests))} assert not rpc.requests
def test_JSONRPCv2_and_JSONRPCLoosemessages(): tests = [ (RPCRequest('foo', [], 2), {"jsonrpc": "2.0", "method": "foo", "id": 2}), (RPCRequest('foo', (), 2), {"jsonrpc": "2.0", "method": "foo", "id": 2}), (RPCRequest('foo', [], None), {"jsonrpc": "2.0", "method": "foo"}), (RPCRequest('foo', {}, 2), {"jsonrpc": "2.0", "params": {}, "method": "foo", "id": 2}), (RPCRequest('foo', (1, 2), 2), {"jsonrpc": "2.0", "method": "foo", "params": [1, 2], "id": 2}), (RPCRequest('foo', [1, 2], 2), {"jsonrpc": "2.0", "method": "foo", "params": [1, 2], "id": 2}), (RPCRequest('foo', {"bar": 3, "baz": "bat"}, "it"), {"jsonrpc": "2.0", "method": "foo", "params": {"bar": 3, "baz": "bat"}, "id": "it"}), (RPCResponse('foo', "it"), {"jsonrpc": "2.0", "result": "foo", "id": "it"}), (RPCResponse(2, "it"), {"jsonrpc": "2.0", "result": 2, "id": "it"}), (RPCResponse(None, -2), {"jsonrpc": "2.0", "result": None, "id": -2}), (RPCResponse([1, 2], -1), {"jsonrpc": "2.0", "result": [1, 2], "id": -1}), (RPCResponse({"kind": 1}, 0), {"jsonrpc": "2.0", "result": {"kind": 1}, "id": 0}), (RPCResponse(RPCError(3, "j"), 1), {"jsonrpc": "2.0", "error": {"code": 3, "message": "j"}, "id": 1}), (RPCBatch([ RPCRequest('foo', [], 2), RPCRequest('bar', [2], None) ]), [ {"jsonrpc": "2.0", "method": "foo", "id": 2}, {"jsonrpc": "2.0", "method": "bar", "params": [2]} ]), ] for rpc in [JSONRPCv2, JSONRPCLoose]: for item, payload in tests: if isinstance(item, RPCRequest): binary = rpc.request_message(item) elif isinstance(item, RPCResponse): binary = rpc.response_message(item) else: binary = rpc.batch_message(item) test_payload = json.loads(binary.decode()) assert test_payload == payload
def test_JSONRPCv1_messages(): tests = [ (RPCRequest('foo', [], 2), {"method": "foo", "params": [], "id": 2}), (RPCRequest('foo', [], None), {"method": "foo", "params": [], "id": None}), (RPCRequest('foo', [1, 2], "s"), {"method": "foo", "params": [1, 2], "id": "s"}), (RPCRequest('foo', [1, 2], ["x"]), {"method": "foo", "params": [1, 2], "id": ["x"]}), (RPCResponse('foo', "it"), {"result": "foo", "error": None, "id": "it"}), (RPCResponse(2, "it"), {"result": 2, "error": None, "id": "it"}), (RPCResponse(None, -2), {"result": None, "error": None, "id": -2}), (RPCResponse([1, 2], -1), {"result": [1, 2], "error": None, "id": -1}), (RPCResponse({"kind": 1}, [1]), {"result": {"kind": 1}, "error": None, "id": [1]}), (RPCResponse(RPCError(3, "j"), 1), {"result": None, "error": {"code": 3, "message": "j"}, "id": 1}), ] rpc = JSONRPCv1 for item, payload in tests: if isinstance(item, RPCRequest): binary = rpc.request_message(item) else: binary = rpc.response_message(item) test_payload = json.loads(binary.decode()) assert test_payload == payload with pytest.raises(TypeError): rpc.request_message(RPCRequest('foo', {}, 2)) with pytest.raises(TypeError): rpc.request_message(RPCRequest('foo', {"x": 1}, 2))