def test_modify_push_config_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 = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]') push_config = pubsub_pb2.PushConfig() # Mock exception response grpc_stub.ModifyPushConfig.side_effect = CustomException() self.assertRaises(errors.GaxError, client.modify_push_config, subscription, push_config)
def test_modify_push_config(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]') push_config = pubsub_pb2.PushConfig() client.modify_push_config(subscription, push_config) grpc_stub.ModifyPushConfig.assert_called_once() request = grpc_stub.ModifyPushConfig.call_args[0] self.assertEqual(subscription, request.subscription) self.assertEqual(push_config, request.push_config)
def test_modify_push_config(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]') push_config = pubsub_pb2.PushConfig() client.modify_push_config(subscription, push_config) grpc_stub.ModifyPushConfig.assert_called_once() args, kwargs = grpc_stub.ModifyPushConfig.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = pubsub_pb2.ModifyPushConfigRequest( subscription=subscription, push_config=push_config) self.assertEqual(expected_request, actual_request)
def create_subscription(self, name, topic, push_config=None, ack_deadline_seconds=0, retain_acked_messages=False, 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 >>> api = subscriber_client.SubscriberClient() >>> name = api.subscription_path('[PROJECT]', '[SUBSCRIPTION]') >>> topic = api.topic_path('[PROJECT]', '[TOPIC]') >>> response = api.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. """ if push_config is None: push_config = pubsub_pb2.PushConfig() if message_retention_duration is None: message_retention_duration = duration_pb2.Duration() # 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)