Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: create subscription with exactly once delivery #592

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ def default(session):
session.install("-e", ".", "-c", constraints_path)

# Run py.test against the unit tests.
# THe following flags are useful during development:
# "-s" -> show print() statement output
# "-k <test case prefix>" -> filter test cases
session.run(
"py.test",
"--quiet",
Expand All @@ -159,7 +156,6 @@ def default(session):
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=0",
"-s",
os.path.join("tests", "unit"),
*session.posargs,
)
Expand Down Expand Up @@ -204,9 +200,6 @@ def system(session):
session.install("-e", ".", "-c", constraints_path)

# Run py.test against the system tests.
# THe following flags are useful during development:
# "-s" -> show print() statement output
# "-k <test case prefix>" -> filter test cases
if system_test_exists:
session.run(
"py.test",
Expand All @@ -233,8 +226,7 @@ def cover(session):
test runs (not system test runs), and then erases coverage data.
"""
session.install("coverage", "pytest-cov")
# Tip: The "-i" flag lets you ignore errors with specific files.
session.run("coverage", "report", "-i", "--show-missing", "--fail-under=100")
session.run("coverage", "report", "--show-missing", "--fail-under=100")

session.run("coverage", "erase")

Expand Down
44 changes: 44 additions & 0 deletions samples/snippets/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,37 @@ def create_subscription_with_filtering(
# [END pubsub_create_subscription_with_filter]


def create_subscription_with_exactly_once_delivery(
project_id: str, topic_id: str, subscription_id: str
) -> None:
"""Create a subscription with exactly once delivery enabled."""
# [START pubsub_create_subscription_with_exactly_once_delivery]
from google.cloud import pubsub_v1

# TODO(developer): Choose an existing topic.
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"

publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)

with subscriber:
subscription = subscriber.create_subscription(
request={
"name": subscription_path,
"topic": topic_path,
"enable_exactly_once_delivery": True,
}
)
print(
f"Created subscription with exactly once delivery enabled: {subscription}"
)
# [END pubsub_create_subscription_with_exactly_once_delivery]


def delete_subscription(project_id: str, subscription_id: str) -> None:
"""Deletes an existing Pub/Sub topic."""
# [START pubsub_delete_subscription]
Expand Down Expand Up @@ -879,6 +910,15 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None:
create_subscription_with_filtering_parser.add_argument("subscription_id")
create_subscription_with_filtering_parser.add_argument("filter")

create_subscription_with_exactly_once_delivery_parser = subparsers.add_parser(
"create-with-exactly-once",
help=create_subscription_with_exactly_once_delivery.__doc__,
)
create_subscription_with_exactly_once_delivery_parser.add_argument("topic_id")
create_subscription_with_exactly_once_delivery_parser.add_argument(
"subscription_id"
)

delete_parser = subparsers.add_parser("delete", help=delete_subscription.__doc__)
delete_parser.add_argument("subscription_id")

Expand Down Expand Up @@ -1003,6 +1043,10 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None:
create_subscription_with_filtering(
args.project_id, args.topic_id, args.subscription_id, args.filter
)
elif args.command == "create-with-exactly-once":
create_subscription_with_exactly_once_delivery(
args.project_id, args.topic_id, args.subscription_id
)
elif args.command == "delete":
delete_subscription(args.project_id, args.subscription_id)
elif args.command == "update-push":
Expand Down
25 changes: 25 additions & 0 deletions samples/snippets/subscriber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,31 @@ def test_create_subscription_with_filtering(
assert '"attributes.author=\\"unknown\\""' in out


def test_create_subscription_with_exactly_once_delivery(
subscriber_client: pubsub_v1.SubscriberClient,
subscription_admin: str,
capsys: CaptureFixture[str],
) -> None:
subscription_path = subscriber_client.subscription_path(
PROJECT_ID, SUBSCRIPTION_ADMIN
)
try:
subscriber_client.delete_subscription(
request={"subscription": subscription_path}
)
except NotFound:
pass

subscriber.create_subscription_with_exactly_once_delivery(
PROJECT_ID, TOPIC, SUBSCRIPTION_ADMIN
)

out, _ = capsys.readouterr()
assert "Created subscription with exactly once delivery enabled" in out
assert f"{subscription_admin}" in out
assert "enable_exactly_once_delivery: true" in out


def test_create_push_subscription(
subscriber_client: pubsub_v1.SubscriberClient,
subscription_admin: str,
Expand Down