Beispiel #1
0
    def test_pull(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

        # Mock request
        subscription = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
        max_messages = 496131527

        # Mock response
        expected_response = pubsub_pb2.PullResponse()
        grpc_stub.Pull.return_value = expected_response

        response = client.pull(subscription, max_messages)
        self.assertEqual(expected_response, response)

        grpc_stub.Pull.assert_called_once()
        args, kwargs = grpc_stub.Pull.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.PullRequest(subscription=subscription,
                                                  max_messages=max_messages)
        self.assertEqual(expected_request, actual_request)
Beispiel #2
0
    def pull(self,
             subscription,
             max_messages,
             return_immediately=None,
             options=None):
        """
        Pulls messages from the server. Returns an empty list if there are no
        messages available in the backlog. The server may return ``UNAVAILABLE`` if
        there are too many concurrent pull requests pending for the given
        subscription.

        Example:
          >>> from google.cloud.gapic.pubsub.v1 import subscriber_client
          >>> client = subscriber_client.SubscriberClient()
          >>> subscription = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
          >>> max_messages = 0
          >>> response = client.pull(subscription, max_messages)

        Args:
          subscription (string): The subscription from which messages should be pulled.
            Format is ``projects/{project}/subscriptions/{sub}``.
          max_messages (int): The maximum number of messages returned for this request. The Pub/Sub
            system may return fewer than the number specified.
          return_immediately (bool): If this field set to true, the system will respond immediately even if
            it there are no messages available to return in the ``Pull`` response.
            Otherwise, the system may wait (for a bounded amount of time) until at
            least one message is available, rather than returning no messages. The
            client may cancel the request if it does not wish to wait any longer for
            the response.
          options (:class:`google.gax.CallOptions`): Overrides the default
            settings for this call, e.g, timeout, retries etc.

        Returns:
          A :class:`google.cloud.proto.pubsub.v1.pubsub_pb2.PullResponse` instance.

        Raises:
          :exc:`google.gax.errors.GaxError` if the RPC is aborted.
          :exc:`ValueError` if the parameters are invalid.
        """
        # Create the request object.
        request = pubsub_pb2.PullRequest(
            subscription=subscription,
            max_messages=max_messages,
            return_immediately=return_immediately)
        return self._pull(request, options)