コード例 #1
0
    def test_update_subscription(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.SubscriberStub)
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

        # Mock request
        subscription = pubsub_pb2.Subscription()
        update_mask = field_mask_pb2.FieldMask()

        # Mock response
        name = 'name3373707'
        topic = 'topic110546223'
        ack_deadline_seconds = 2135351438
        retain_acked_messages = False
        expected_response = pubsub_pb2.Subscription(name, topic,
                                                    ack_deadline_seconds,
                                                    retain_acked_messages)
        grpc_stub.UpdateSubscription.return_value = expected_response

        response = client.update_subscription(subscription, update_mask)
        self.assertEqual(expected_response, response)

        grpc_stub.UpdateSubscription.assert_called_once()
        request = grpc_stub.UpdateSubscription.call_args[0]

        self.assertEqual(subscription, request.subscription)
        self.assertEqual(update_mask, request.update_mask)
コード例 #2
0
    def test_create_subscription(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

        # Mock request
        name = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        # Mock response
        name_2 = 'name2-1052831874'
        topic_2 = 'topic2-1139259102'
        ack_deadline_seconds = 2135351438
        retain_acked_messages = False
        expected_response = pubsub_pb2.Subscription(
            name=name_2,
            topic=topic_2,
            ack_deadline_seconds=ack_deadline_seconds,
            retain_acked_messages=retain_acked_messages)
        grpc_stub.CreateSubscription.return_value = expected_response

        response = client.create_subscription(name, topic)
        self.assertEqual(expected_response, response)

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

        expected_request = pubsub_pb2.Subscription(name=name, topic=topic)
        self.assertEqual(expected_request, actual_request)
コード例 #3
0
    def test_get_subscription(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.SubscriberStub)
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

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

        # Mock response
        name = 'name3373707'
        topic = 'topic110546223'
        ack_deadline_seconds = 2135351438
        retain_acked_messages = False
        expected_response = pubsub_pb2.Subscription(name, topic,
                                                    ack_deadline_seconds,
                                                    retain_acked_messages)
        grpc_stub.GetSubscription.return_value = expected_response

        response = client.get_subscription(subscription)
        self.assertEqual(expected_response, response)

        grpc_stub.GetSubscription.assert_called_once()
        request = grpc_stub.GetSubscription.call_args[0]

        self.assertEqual(subscription, request.subscription)
コード例 #4
0
    def test_list_subscriptions(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.SubscriberStub)
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

        # Mock request
        project = client.project_path('[PROJECT]')

        # Mock response
        next_page_token = ''
        subscriptions_element = pubsub_pb2.Subscription()
        subscriptions = [subscriptions_element]
        expected_response = pubsub_pb2.ListSubscriptionsResponse(
            next_page_token, subscriptions)
        grpc_stub.ListSubscriptions.return_value = expected_response

        paged_list_response = client.list_subscriptions(project)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.subscriptions[0], resources[0])

        grpc_stub.ListSubscriptions.assert_called_once()
        request = grpc_stub.ListSubscriptions.call_args[0]

        self.assertEqual(project, request.project)
コード例 #5
0
    def test_get_subscription(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]')

        # Mock response
        name = 'name3373707'
        topic = 'topic110546223'
        ack_deadline_seconds = 2135351438
        retain_acked_messages = False
        expected_response = pubsub_pb2.Subscription(
            name=name,
            topic=topic,
            ack_deadline_seconds=ack_deadline_seconds,
            retain_acked_messages=retain_acked_messages)
        grpc_stub.GetSubscription.return_value = expected_response

        response = client.get_subscription(subscription)
        self.assertEqual(expected_response, response)

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

        expected_request = pubsub_pb2.GetSubscriptionRequest(
            subscription=subscription)
        self.assertEqual(expected_request, actual_request)
コード例 #6
0
    def test_update_subscription(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 = pubsub_pb2.Subscription()
        update_mask = field_mask_pb2.FieldMask()

        # Mock response
        name = 'name3373707'
        topic = 'topic110546223'
        ack_deadline_seconds = 2135351438
        retain_acked_messages = False
        expected_response = pubsub_pb2.Subscription(
            name=name,
            topic=topic,
            ack_deadline_seconds=ack_deadline_seconds,
            retain_acked_messages=retain_acked_messages)
        grpc_stub.UpdateSubscription.return_value = expected_response

        response = client.update_subscription(subscription, update_mask)
        self.assertEqual(expected_response, response)

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

        expected_request = pubsub_pb2.UpdateSubscriptionRequest(
            subscription=subscription, update_mask=update_mask)
        self.assertEqual(expected_request, actual_request)
コード例 #7
0
    def test_update_subscription_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.SubscriberStub)
        mock_create_stub.return_value = grpc_stub

        client = subscriber_client.SubscriberClient()

        # Mock request
        subscription = pubsub_pb2.Subscription()
        update_mask = field_mask_pb2.FieldMask()

        # Mock exception response
        grpc_stub.UpdateSubscription.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.update_subscription,
                          subscription, update_mask)
    def create_subscription(self,
                            name,
                            topic,
                            push_config=None,
                            ack_deadline_seconds=None,
                            retain_acked_messages=None,
                            message_retention_duration=None,
                            options=None):
        """
        Creates a subscription to a given topic.
        If the subscription already exists, returns ``ALREADY_EXISTS``.
        If the corresponding topic doesn't exist, returns ``NOT_FOUND``.

        If the name is not provided in the request, the server will assign a random
        name for this subscription on the same project as the topic, conforming
        to the
        `resource name format <https://cloud.google.com/pubsub/docs/overview#names>`_.
        The generated name is populated in the returned Subscription object.
        Note that for REST API requests, you must specify a name in the request.

        Example:
          >>> from google.cloud.gapic.pubsub.v1 import subscriber_client
          >>> client = subscriber_client.SubscriberClient()
          >>> name = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]')
          >>> topic = client.topic_path('[PROJECT]', '[TOPIC]')
          >>> response = client.create_subscription(name, topic)

        Args:
          name (string): The name of the subscription. It must have the format
            ``\"projects/{project}/subscriptions/{subscription}\"``. ``{subscription}`` must
            start with a letter, and contain only letters (``[A-Za-z]``), numbers
            (``[0-9]``), dashes (``-``), underscores (``_``), periods (``.``), tildes (``~``),
            plus (``+``) or percent signs (``%``). It must be between 3 and 255 characters
            in length, and it must not start with ``\"goog\"``.
          topic (string): The name of the topic from which this subscription is receiving messages.
            Format is ``projects/{project}/topics/{topic}``.
            The value of this field will be ``_deleted-topic_`` if the topic has been
            deleted.
          push_config (:class:`google.cloud.proto.pubsub.v1.pubsub_pb2.PushConfig`): If push delivery is used with this subscription, this field is
            used to configure it. An empty ``pushConfig`` signifies that the subscriber
            will pull and ack messages using API methods.
          ack_deadline_seconds (int): This value is the maximum time after a subscriber receives a message
            before the subscriber should acknowledge the message. After message
            delivery but before the ack deadline expires and before the message is
            acknowledged, it is an outstanding message and will not be delivered
            again during that time (on a best-effort basis).

            For pull subscriptions, this value is used as the initial value for the ack
            deadline. To override this value for a given message, call
            ``ModifyAckDeadline`` with the corresponding ``ack_id`` if using
            pull.
            The minimum custom deadline you can specify is 10 seconds.
            The maximum custom deadline you can specify is 600 seconds (10 minutes).
            If this parameter is 0, a default value of 10 seconds is used.

            For push delivery, this value is also used to set the request timeout for
            the call to the push endpoint.

            If the subscriber never acknowledges the message, the Pub/Sub
            system will eventually redeliver the message.
          retain_acked_messages (bool): Indicates whether to retain acknowledged messages. If true, then
            messages are not expunged from the subscription's backlog, even if they are
            acknowledged, until they fall out of the ``message_retention_duration``
            window.
          message_retention_duration (:class:`google.protobuf.duration_pb2.Duration`): How long to retain unacknowledged messages in the subscription's backlog,
            from the moment a message is published.
            If ``retain_acked_messages`` is true, then this also configures the retention
            of acknowledged messages, and thus configures how far back in time a ``Seek``
            can be done. Defaults to 7 days. Cannot be more than 7 days or less than 10
            minutes.
          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.Subscription` 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.Subscription(
            name=name,
            topic=topic,
            push_config=push_config,
            ack_deadline_seconds=ack_deadline_seconds,
            retain_acked_messages=retain_acked_messages,
            message_retention_duration=message_retention_duration)
        return self._create_subscription(request, options)