def test_pull(self): # Setup Expected Response expected_response = {} expected_response = pubsub_pb2.PullResponse(**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 = subscriber_client.SubscriberClient() # Setup Request subscription = client.subscription_path("[PROJECT]", "[SUBSCRIPTION]") max_messages = 496131527 response = client.pull(subscription, max_messages) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.PullRequest( subscription=subscription, max_messages=max_messages ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_pull(self): # Setup Expected Response expected_response = {} expected_response = pubsub_pb2.PullResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = subscriber_client.SubscriberClient(channel=channel) # Setup Request subscription = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]') max_messages = 496131527 response = client.pull(subscription, max_messages) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.PullRequest(subscription=subscription, max_messages=max_messages) actual_request = channel.requests[0][1] assert expected_request == actual_request
def Pull(self, request: pubsub_pb2.PullRequest, context: grpc.ServicerContext): """Pull implementation.""" self.logger.debug("Pull(%.100s)", LazyFormat(request)) received_messages: List[pubsub_pb2.ReceivedMessage] = [] try: subscription = self.subscriptions[request.subscription] except KeyError: context.abort(grpc.StatusCode.NOT_FOUND, "Subscription not found") messages = subscription.published[: request.max_messages or 100] subscription.pulled.update( {message.message_id: message for message in messages} ) for message in messages: try: subscription.published.remove(message) except ValueError: pass received_messages = [ pubsub_pb2.ReceivedMessage(ack_id=message.message_id, message=message) for message in messages ] return pubsub_pb2.PullResponse(received_messages=received_messages)