From bb2a52aec015119a3f800976164cf93b76b3a540 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 17 Jun 2024 18:13:54 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20post-p?= =?UTF-8?q?rocessor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- samples/snippets/subscriber.py | 21 ++++++++++++++++----- samples/snippets/subscriber_test.py | 14 ++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/samples/snippets/subscriber.py b/samples/snippets/subscriber.py index 86359bd44..79cd0ebf1 100644 --- a/samples/snippets/subscriber.py +++ b/samples/snippets/subscriber.py @@ -95,7 +95,10 @@ def create_subscription(project_id: str, topic_id: str, subscription_id: str) -> def optimistic_subscribe( - project_id: str, topic_id: str, subscription_id: str, timeout: Optional[float] = None + project_id: str, + topic_id: str, + subscription_id: str, + timeout: Optional[float] = None, ) -> None: """Optimistically subscribe to messages instead of making calls to verify existence of a subscription first and then subscribing to messages from it. This avoids admin @@ -129,7 +132,9 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: with subscriber: try: # Optimistically subscribe to messages on the subscription. - streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) + streaming_pull_future = subscriber.subscribe( + subscription_path, callback=callback + ) streaming_pull_future.result(timeout=timeout) except TimeoutError: print("Successfully subscribed until the timeout passed.") @@ -153,13 +158,17 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: # Subscribe on the created subscription. try: - streaming_pull_future = subscriber.subscribe(subscription.name, callback=callback) + streaming_pull_future = subscriber.subscribe( + subscription.name, callback=callback + ) streaming_pull_future.result(timeout=timeout) except TimeoutError: streaming_pull_future.cancel() # Trigger the shutdown. streaming_pull_future.result() # Block until the shutdown is complete. except Exception as e: - print(f"Exception occurred when creating subscription and subscribing to it: {e}") + print( + f"Exception occurred when creating subscription and subscribing to it: {e}" + ) except Exception as e: print(f"Exception occurred when attempting optimistic subscribe: {e}") # [END pubsub_optimistic_subscribe] @@ -1384,7 +1393,9 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: elif args.command == "remove-dead-letter-policy": remove_dead_letter_policy(args.project_id, args.topic_id, args.subscription_id) elif args.command == "optimistic-subscribe": - optimistic_subscribe(args.project_id, args.topic_id, args.subscription_id, args.timeout) + optimistic_subscribe( + args.project_id, args.topic_id, args.subscription_id, args.timeout + ) elif args.command == "receive": receive_messages(args.project_id, args.subscription_id, args.timeout) elif args.command == "receive-custom-attributes": diff --git a/samples/snippets/subscriber_test.py b/samples/snippets/subscriber_test.py index 90754d9ce..c474eaf48 100644 --- a/samples/snippets/subscriber_test.py +++ b/samples/snippets/subscriber_test.py @@ -238,14 +238,10 @@ def test_optimistic_subscribe( subscriber_client: pubsub_v1.SubscriberClient, topic: str, publisher_client: pubsub_v1.PublisherClient, - capsys: CaptureFixture[str] + capsys: CaptureFixture[str], ) -> None: - subscription_id = ( - f"subscription_for_optimistic_subscribe-{PY_VERSION}-{UUID}" - ) - subscription_path = subscriber_client.subscription_path( - PROJECT_ID, subscription_id - ) + subscription_id = f"subscription_for_optimistic_subscribe-{PY_VERSION}-{UUID}" + subscription_path = subscriber_client.subscription_path(PROJECT_ID, subscription_id) # Ensure there is no pre-existing subscription. # So that we can test the case where optimistic subscribe fails. try: @@ -286,7 +282,9 @@ def test_optimistic_subscribe( # Test case where creation of subscription fails for reasons other than # a TimeoutError. - subscriber.optimistic_subscribe(PROJECT_ID, "not-existent-topic", subscription_id, 5) + subscriber.optimistic_subscribe( + PROJECT_ID, "not-existent-topic", subscription_id, 5 + ) assert "Exception occurred when creating subscription and subscribing to it" in out # Clean up resources created during test.