def test_batch(self): multicall = MultiCall(self.client) multicall.sum(1, 2, 4) multicall._notify.notify_hello(7) multicall.subtract(42, 23) multicall.foo.get(name='myself') multicall.get_data() job_requests = [j.request() for j in multicall._job_list] job_requests.insert(3, '{"foo": "boo"}') json_requests = '[%s]' % ','.join(job_requests) requests = json.loads(json_requests) responses = self.client._run_request(json_requests) verify_requests = json.loads("""[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]""") # Thankfully, these are in order so testing is pretty simple. verify_responses = json.loads("""[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]""") self.assertTrue(len(requests) == len(verify_requests)) self.assertTrue(len(responses) == len(verify_responses)) responses_by_id = {} response_i = 0 for i in range(len(requests)): verify_request = verify_requests[i] request = requests[i] response = None if request.get('method') != 'notify_hello': req_id = request.get('id') if verify_request.has_key('id'): verify_request['id'] = req_id verify_response = verify_responses[response_i] verify_response['id'] = req_id responses_by_id[req_id] = verify_response response_i += 1 response = verify_response self.assertTrue(request == verify_request) for response in responses: verify_response = responses_by_id.get(response.get('id')) if verify_response.has_key('error'): verify_response['error']['message'] = \ response['error']['message'] self.assertTrue(response == verify_response)
def test_batch(self): multicall = MultiCall(self.client) multicall.sum(1, 2, 4) multicall._notify.notify_hello(7) multicall.subtract(42, 23) multicall.foo.get(name='myself') multicall.get_data() job_requests = [j.request() for j in multicall._job_list] job_requests.insert(3, '{"foo": "boo"}') json_requests = '[%s]' % ','.join(job_requests) requests = json.loads(json_requests) responses = self.client._run_request(json_requests) verify_requests = json.loads("""[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]""") # Thankfully, these are in order so testing is pretty simple. verify_responses = json.loads("""[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]""") self.assertTrue(len(requests) == len(verify_requests)) self.assertTrue(len(responses) == len(verify_responses)) responses_by_id = {} response_i = 0 for i in range(len(requests)): verify_request = verify_requests[i] request = requests[i] response = None if request.get('method') != 'notify_hello': req_id = request.get('id') if 'id' in verify_request: verify_request['id'] = req_id verify_response = verify_responses[response_i] verify_response['id'] = req_id responses_by_id[req_id] = verify_response response_i += 1 response = verify_response self.assertTrue(request == verify_request) for response in responses: verify_response = responses_by_id.get(response.get('id')) if 'error' in verify_response: verify_response['error']['message'] = \ response['error']['message'] self.assertTrue(response == verify_response)
def test_batch_notifications(self): multicall = MultiCall(self.client) multicall._notify.notify_sum(1, 2, 4) multicall._notify.notify_hello(7) result = multicall() self.assertTrue(len(result) == 0) valid_request = json.loads( '[{"jsonrpc": "2.0", "method": "notify_sum", ' + '"params": [1, 2, 4]},{"jsonrpc": "2.0", ' + '"method": "notify_hello", "params": [7]}]') request = json.loads(history.request) self.assertTrue(len(request) == len(valid_request)) for i in range(len(request)): req = request[i] valid_req = valid_request[i] self.assertTrue(req == valid_req) self.assertTrue(history.response == '')
def get_multicall_client(self): server = self.get_client() return MultiCall(server)