def test_publish(self): # Setup Expected Response message_ids_element = 'messageIdsElement-744837059' message_ids = [message_ids_element] expected_response = {'message_ids': message_ids} expected_response = pubsub_pb2.PublishResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = publisher_client.PublisherClient(channel=channel) # Setup Request topic = client.topic_path('[PROJECT]', '[TOPIC]') data = b'-86' messages_element = {'data': data} messages = [messages_element] response = client.publish(topic, messages) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.PublishRequest(topic=topic, messages=messages) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_publish(self): # Setup Expected Response message_ids_element = "messageIdsElement-744837059" message_ids = [message_ids_element] expected_response = {"message_ids": message_ids} expected_response = pubsub_pb2.PublishResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request topic = client.topic_path("[PROJECT]", "[TOPIC]") data = b"-86" messages_element = {"data": data} messages = [messages_element] response = client.publish(topic, messages) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.PublishRequest(topic=topic, messages=messages) actual_request = channel.requests[0][1] assert expected_request == actual_request
def Publish( self, request: pubsub_pb2.PublishRequest, context: grpc.ServicerContext ): """Publish implementation.""" self.logger.debug("Publish(%.100s)", LazyFormat(request)) if request.topic in self.status_codes: context.abort(self.status_codes[request.topic], "Override") message_ids: List[str] = [] try: subscriptions = self.topics[request.topic] except KeyError: context.abort(grpc.StatusCode.NOT_FOUND, "Topic not found") message_ids = [uuid.uuid4().hex for _ in request.messages] if self.sleep is not None: time.sleep(self.sleep) # return a valid response without recording messages return pubsub_pb2.PublishResponse(message_ids=message_ids) for _id, message in zip(message_ids, request.messages): message.message_id = _id for subscription in subscriptions: subscription.published.extend(request.messages) return pubsub_pb2.PublishResponse(message_ids=message_ids)