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

tests: mitigate flaky snippets tests #432

Merged
merged 4 commits into from
Jun 23, 2021
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
2 changes: 2 additions & 0 deletions samples/snippets/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import uuid

from flaky import flaky
from google.api_core.exceptions import NotFound
from google.cloud.pubsub import PublisherClient, SchemaServiceClient, SubscriberClient
from google.pubsub_v1.types import Encoding
Expand Down Expand Up @@ -251,6 +252,7 @@ def test_subscribe_with_proto_schema(
assert "Received a binary-encoded message" in out


@flaky(max_runs=3, min_passes=1)
def test_delete_schema(proto_schema, capsys):
schema.delete_schema(PROJECT_ID, PROTO_SCHEMA_ID)
out, _ = capsys.readouterr()
Expand Down
2 changes: 1 addition & 1 deletion samples/snippets/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def receive_messages_with_blocking_shutdown(project_id, subscription_id, timeout

def callback(message):
print(f"Received {message.data}.")
time.sleep(timeout + 5.0) # Pocess longer than streaming pull future timeout.
time.sleep(timeout + 3.0) # Pocess longer than streaming pull future timeout.
message.ack()
print(f"Done processing the message {message.data}.")

Expand Down
30 changes: 18 additions & 12 deletions samples/snippets/subscriber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,21 +425,26 @@ def test_receive_with_blocking_shutdown(
if re.search(r".*done waiting.*stream shutdown.*", line, flags=re.IGNORECASE)
]

assert "Listening" in out
assert subscription_async in out
try:
assert "Listening" in out
assert subscription_async in out

assert len(stream_canceled_lines) == 1
assert len(shutdown_done_waiting_lines) == 1
assert len(msg_received_lines) == 3
assert len(msg_done_lines) == 3
assert len(stream_canceled_lines) == 1
assert len(shutdown_done_waiting_lines) == 1
assert len(msg_received_lines) == 3
assert len(msg_done_lines) == 3

# The stream should have been canceled *after* receiving messages, but before
# message processing was done.
assert msg_received_lines[-1] < stream_canceled_lines[0] < msg_done_lines[0]
# The stream should have been canceled *after* receiving messages, but before
# message processing was done.
assert msg_received_lines[-1] < stream_canceled_lines[0] < msg_done_lines[0]

# Yet, waiting on the stream shutdown should have completed *after* the processing
# of received messages has ended.
assert msg_done_lines[-1] < shutdown_done_waiting_lines[0]
# Yet, waiting on the stream shutdown should have completed *after*
# the processing of received messages has ended.
assert msg_done_lines[-1] < shutdown_done_waiting_lines[0]
except AssertionError: # pragma: NO COVER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about "pragma: NO COVER". Nice.

Copy link
Contributor Author

@plamut plamut Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it just shouldn't be overused. :)
This is one of the cases where it's the right tool, though, it makes the coverage check happy, since a passing test does never hit the "unhappy" path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely :)

from pprint import pprint
pprint(out_lines) # To make possible flakiness debugging easier.
raise


def test_listen_for_errors(publisher_client, topic, subscription_async, capsys):
Expand All @@ -464,6 +469,7 @@ def test_receive_synchronously(publisher_client, topic, subscription_sync, capsy
assert f"{subscription_sync}" in out


@flaky(max_runs=3, min_passes=1)
def test_receive_synchronously_with_lease(
publisher_client, topic, subscription_sync, capsys
):
Expand Down