diff --git a/noxfile.py b/noxfile.py index ba16d5a46..e9fea8af8 100644 --- a/noxfile.py +++ b/noxfile.py @@ -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 " -> filter test cases session.run( "py.test", "--quiet", @@ -159,7 +156,6 @@ def default(session): "--cov-config=.coveragerc", "--cov-report=", "--cov-fail-under=0", - "-s", os.path.join("tests", "unit"), *session.posargs, ) @@ -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 " -> filter test cases if system_test_exists: session.run( "py.test", @@ -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") diff --git a/samples/snippets/subscriber.py b/samples/snippets/subscriber.py index ada70a02d..5a9d0a7a5 100644 --- a/samples/snippets/subscriber.py +++ b/samples/snippets/subscriber.py @@ -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] @@ -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") @@ -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": diff --git a/samples/snippets/subscriber_test.py b/samples/snippets/subscriber_test.py index 614633664..34a42cac4 100644 --- a/samples/snippets/subscriber_test.py +++ b/samples/snippets/subscriber_test.py @@ -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,