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

Cloud Pub/Sub Quickstart V2 #2004

Merged
merged 10 commits into from
Feb 12, 2019
Prev Previous commit
Next Next commit
proper resource deletion during teardown
  • Loading branch information
anguillanneuf committed Feb 11, 2019
commit cb03cd1f0c4026621acd89ffb15113ee5464e62a
18 changes: 11 additions & 7 deletions pubsub/cloud-client/quickstart/pub_test.py
Original file line number Diff line number Diff line change
@@ -43,15 +43,19 @@ def topic(publisher_client):
yield TOPIC


def test_pub(publisher_client, topic, capsys):
@pytest.fixture
def to_delete(publisher_client):
doomed = []
yield doomed
for item in doomed:
publisher_client.delete_topic(item)


def test_pub(publisher_client, topic, to_delete, capsys):
pub.pub(PROJECT, topic)

to_delete.append('projects/{}/topics/{}'.format(PROJECT, TOPIC))

out, _ = capsys.readouterr()

assert "Published message b'Hello, World!'" in out

# Clean up.
publisher_client.delete_topic('projects/{}/topics/{}'.format(
PROJECT,
TOPIC
))
25 changes: 18 additions & 7 deletions pubsub/cloud-client/quickstart/sub_test.py
Original file line number Diff line number Diff line change
@@ -65,6 +65,17 @@ def subscription(subscriber_client, topic_path):
yield SUBSCRIPTION


@pytest.fixture
def to_delete(publisher_client, subscriber_client):
doomed = []
yield doomed
for client, item in doomed:
if 'topics' in item:
publisher_client.delete_topic(item)
if 'subscriptions' in item:
subscriber_client.delete_subscription(item)


def _make_sleep_patch():
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer using the monkeypatch fixture for pytest. It can clean up after your tests so that the sleep function is restored after the test is done.

Copy link
Member Author

Choose a reason for hiding this comment

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

I need some help with this. In the meanwhile, since other tests are also written using _make_sleep_patch in the same repo, shall we start another PR just for this purpose?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the pattern I'm describing: https://stackoverflow.com/a/29110609/101923,

Now that I have a closer look, mock.patch does the same thing, but this still seems much more complicated than it needs to be.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried monkeypatch but it created an infinite loop.

def mock_sleep(sleep):
    time.sleep(10)
    raise RuntimeError('sigil')

monkeypatch.setattr(time, 'sleep', mock_sleep)

while True: time.sleep(60) would call time.sleep(10), which calls time.sleep(10) and never reaches raise RuntimeError('sigil').

real_sleep = time.sleep

@@ -82,21 +93,21 @@ def test_sub(publisher_client,
topic_path,
subscriber_client,
subscription,
to_delete,
capsys):

publisher_client.publish(topic_path, data=b'Hello, World!')

to_delete.append((publisher_client, topic_path))

with _make_sleep_patch():
with pytest.raises(RuntimeError, match='sigil'):
sub.sub(PROJECT, subscription)

out, _ = capsys.readouterr()
to_delete.append((subscriber_client,
'projects/{}/subscriptions/{}'.format(PROJECT,
SUBSCRIPTION)))

out, _ = capsys.readouterr()
assert "Received message" in out
assert "Acknowledged message" in out

# Clean up.
subscriber_client.delete_subscription(
'projects/{}/subscriptions/{}'.format(PROJECT, SUBSCRIPTION)
)
publisher_client.delete_topic(topic_path)