def test_client_handle_message_with_response_calls_response_callback(self): callback = ResponseCallback() callback.on_response = Mock() self.client.add_response_callback(self.test_channel, callback) # Create and process Response msg = Response(request=None)._to_bytes() self.client._handle_message(self.test_channel, msg) # Check that callback was called self.assertEqual(callback.on_response.call_count, 1)
def test_execute_async_callback_timeout(self): # TODO: Set SYSPROP_ASYNC_CALLBACK_CHECK_INTERVAL = 10000 when it is available def resp_callback(): pass cb = ResponseCallback() cb.on_response = resp_callback with self.create_client() as client: client.connect() req_topic = UuidGenerator.generate_id_as_string() missing_topic = UuidGenerator.generate_id_as_string() test_service = TestService(client, 1) def empty_on_request(request): pass test_service.on_request = empty_on_request reg_info = ServiceRegistrationInfo(client, "async_callback_test_service") reg_info.add_topic(req_topic, test_service) # Register the service client.register_service_sync(reg_info, self.DEFAULT_TIMEOUT) async_req = Request(destination_topic=req_topic) client.async_request( async_req, cb) # TODO: Use the method with timeout when is will available for i in range(0, 10): req = Request(destination_topic=req_topic) client.async_request( req, cb ) # TODO: Use the updated method with timeout when it is available req_for_error = Request(destination_topic=missing_topic) client.async_request(req_for_error) async_callback_count = client._get_async_callback_count() self.assertEquals(11, async_callback_count) for i in range(0, 20): print "asyncCallbackCount = " + str( client._get_async_callback_count()) time.sleep(1) req = Request(destination_topic=req_topic) client.async_request(req, cb) self.assertEquals(1, async_callback_count)
def test_wildcard_services(self): max_wait = 10 with self.create_client() as client: # The request message that the service receives service_request_message = [] # The request message corresponding to the response received by the client client_response_message_request = [] # The event that we received client_event_message = [] client_event_message_condition = Condition() # The payload that the service receives service_request_message_receive_payload = [] client.connect() info = ServiceRegistrationInfo(client, "myWildcardService") meta = {} # Transform events mapped to "test/#/" to "request/test/..." meta["EventToRequestTopic"] = "/test/#" meta["EventToRequestPrefix"] = "/request" info.metadata = meta rcb = RequestCallback() def on_request(request): print("## Request in service: " + request.destination_topic + ", " + str(request.message_id)) print("## Request in service - payload: " + request.payload) service_request_message.append(request.message_id) service_request_message_receive_payload.append(request.payload) response = Response(request) response.payload = "Request response - Event payload: " + request.payload client.send_response(response) rcb.on_request = on_request info.add_topic("/request/test/#", rcb) client.register_service_sync(info, 10) evt = Event("/test/bar") rcb = ResponseCallback() def on_response(response): # Only handle the response corresponding to the event we sent if response.request_message_id == evt.message_id: print("## received_response: " + response.request_message_id + ", " + response.__class__.__name__) print("## received_response_payload: " + response.payload) client_response_message_request[0] = response.request_message_id rcb.on_response = on_response client.add_response_callback("", rcb) ecb = EventCallback() def on_event(event): print("## received event: " + event.destination_topic + ", " + event.message_id) with client_event_message_condition: client_event_message.append(event.message_id) client_event_message_condition.notify_all() ecb.on_event = on_event client.add_event_callback("/test/#", ecb) # Send our event print("## Sending event: " + evt.destination_topic + ", " + evt.message_id) evt.payload = "Unit test payload" client.send_event(evt) start = time.time() with client_event_message_condition: while (time.time() - start < max_wait) and \ not client_event_message: client_event_message_condition.wait(max_wait) # # Make sure the service received the request properly # self.assertEqual(evt.message_id, service_request_message[0]) # # Make sure the service received the request payload from the event properly # self.assertEqual(evt.payload, service_request_message_receive_payload[0]) # Make sure the response we received was for the request message # self.assertEqual(evt.message_id, client_response_message_request[0]) # Make sure we received the correct event self.assertGreater(len(client_event_message), 0) self.assertEqual(evt.message_id, client_event_message[0])
def test_execute_async_request(self): # Create and register a response callback. Once the request has been processed # by the service, the requesting client will receive a notification via this # callback. At that point, we note that the request has been responded to (remove # it from the set of outstanding requests), and increment the count of responses # we have received via the callbacks. def my_response(response): with self.response_condition: # Increment count of responses received self.response_count += 1 # Remove from outstanding requests self.remove_outstanding_request(response.request_message_id) # Notify that a response has been received (are we done yet?) self.response_condition.notify_all() rc = ResponseCallback() rc.on_response = my_response with self.create_client(max_retries=0) as client: try: client.connect() # Create a test service that responds to requests on a particular topic. test_service = TestService(client, 1) topic = UuidGenerator.generate_id_as_string() reg_info = ServiceRegistrationInfo( client, "async_request_runner_service") reg_info.add_topic(topic, test_service) # Register the service client.register_service_sync(reg_info, self.DEFAULT_TIMEOUT) # Add a response callback (not channel-specific) client.add_response_callback("", rc) for i in range(0, self.REQ_COUNT): # Send a request without specifying a callback for the current request req = Request(topic) self.append_outstanding_request(req.message_id) client.async_request(req) # Send a request with a specific callback that is to receive the response. # The response will be received by two callbacks. req = Request(topic) self.append_outstanding_request(req.message_id) client.async_request(req, response_callback=rc) # Wait until all the responses are received via the response callbacks. # The "times 3" is due to the fact that 20000 requests were sent in total. # 20000 were handled via the global callback, an additional 10000 were handled # via the callback explicitly passed in the second set of requests. with self.response_condition: while self.response_count != self.REQ_COUNT * 3: current_count = self.response_count self.response_condition.wait(self.MAX_RESPONSE_WAIT) if current_count == self.response_count: self.fail("Response wait timeout") # Make sure there are no outstanding requests self.assertEqual(0, len(self.outstanding_requests)) print "Async request test: PASSED" except Exception, ex: print ex.message raise ex
def test_async_flood(self): channel = UuidGenerator.generate_id_as_string() with self.create_client() as client: self.m_info = ServiceRegistrationInfo(client, channel) client.connect() client.subscribe(channel) def my_request_callback(request): try: time.sleep(0.05) resp = Response(request) resp.payload = request.payload client.send_response(resp) except Exception as ex: # pylint: disable=broad-except print(ex) req_callback = RequestCallback() req_callback.on_request = my_request_callback self.m_info.add_topic(channel, req_callback) client.register_service_sync(self.m_info, 10) with self.create_client() as client2: client2.connect() def my_response_callback(response): if response.message_type == Message.MESSAGE_TYPE_ERROR: print("Received error response: " + response._error_response) with self.resp_condition: self.error_count += 1 self.resp_condition.notify_all() else: with self.resp_condition: if self.response_count % 10 == 0: print("Received request " + str(self.response_count)) self.response_count += 1 self.resp_condition.notify_all() callback = ResponseCallback() callback.on_response = my_response_callback client2.add_response_callback("", callback) for i in range(0, self.REQUEST_COUNT): if i % 100 == 0: print("Sent: " + str(i)) request = Request(channel) request.payload = str(i) client2.async_request(request) if self.error_count > 0: break # Wait for all responses, an error to occur, or we timeout start_time = time.time() with self.resp_condition: while (self.response_count != self.REQUEST_COUNT) and ( self.error_count == 0) and ( time.time() - start_time < self.WAIT_TIME): self.resp_condition.wait(5) if self.error_count != 0: raise Exception("Received an error response!") self.assertEqual(self.REQUEST_COUNT, self.response_count, "Did not receive all messages!")