Skip to content

Commit

Permalink
samples: clarify comments in samples (#223)
Browse files Browse the repository at this point in the history
* samples: clarify comments in samples

* fix: create a dlq sub instead
  • Loading branch information
anguillanneuf authored Oct 13, 2020
1 parent b64e218 commit 7de13b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
5 changes: 3 additions & 2 deletions samples/snippets/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@ def publish_messages_with_retry_settings(project_id, topic_id):
# project_id = "your-project-id"
# topic_id = "your-topic-id"

# Configure the retry settings.
# Configure the retry settings. Defaults shown in comments are values applied
# by the library by default, instead of default values in the Retry object.
custom_retry = api_core.retry.Retry(
initial=0.250, # seconds (default: 0.1)
maximum=90.0, # seconds (default: 60.0)
multiplier=1.45, # default: 1.3
deadline=300.0, # seconds (default: 600.0)
deadline=300.0, # seconds (default: 60.0)
predicate=api_core.retry.if_exception_type(
api_core.exceptions.Aborted,
api_core.exceptions.DeadlineExceeded,
Expand Down
4 changes: 3 additions & 1 deletion samples/snippets/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ def callback(message):
streaming_pull_future.result(timeout=timeout)
except Exception as e:
streaming_pull_future.cancel()
print(f"Listening for messages on {subscription_path} threw an exception: {e}.")
print(
f"Listening for messages on {subscription_path} threw an exception: {e}."
)
# [END pubsub_subscriber_error_listener]


Expand Down
27 changes: 17 additions & 10 deletions samples/snippets/subscriber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,26 @@ def subscription_async(subscriber_client, topic):


@pytest.fixture(scope="module")
def subscription_dlq(subscriber_client, topic):
def subscription_dlq(subscriber_client, topic, dead_letter_topic):
from google.cloud.pubsub_v1.types import DeadLetterPolicy

subscription_path = subscriber_client.subscription_path(
PROJECT_ID, SUBSCRIPTION_DLQ
)

try:
subscription = subscriber_client.get_subscription(
subscription = subscriber_client.delete_subscription(
request={"subscription": subscription_path}
)
except NotFound:
subscription = subscriber_client.create_subscription(
request={"name": subscription_path, "topic": topic}
)
request = {
"name": subscription_path,
"topic": topic,
"dead_letter_policy": DeadLetterPolicy(
dead_letter_topic=dead_letter_topic, max_delivery_attempts=10
),
}
subscription = subscriber_client.create_subscription(request)

yield subscription.name

Expand Down Expand Up @@ -210,12 +217,14 @@ def test_create_subscription_with_dead_letter_policy(
assert "After 10 delivery attempts." in out


def test_update_dead_letter_policy(capsys):
def test_update_dead_letter_policy(subscription_dlq, dead_letter_topic, capsys):
_ = subscriber.update_subscription_with_dead_letter_policy(
PROJECT_ID, TOPIC, SUBSCRIPTION_DLQ, DEAD_LETTER_TOPIC
)

out, _ = capsys.readouterr()
assert dead_letter_topic in out
assert subscription_dlq in out
assert "max_delivery_attempts: 20" in out


Expand Down Expand Up @@ -366,12 +375,10 @@ def test_receive_with_delivery_attempts(
):
_publish_messages(publisher_client, topic)

subscriber.receive_messages_with_delivery_attempts(PROJECT_ID, SUBSCRIPTION_DLQ, 10)
subscriber.receive_messages_with_delivery_attempts(PROJECT_ID, SUBSCRIPTION_DLQ, 15)

out, _ = capsys.readouterr()
assert subscription_dlq in out
assert "Received" in out
assert "message 4" in out
assert f"Listening for messages on {subscription_dlq}.." in out
assert "With delivery attempts: " in out


Expand Down

0 comments on commit 7de13b8

Please sign in to comment.