Skip to content

Commit

Permalink
Make patched_subscriber_recv interruptible
Browse files Browse the repository at this point in the history
This makes it possible to catch cases when the subscriber is closed
before all expected messages have arrived.
  • Loading branch information
mraspaud committed Dec 4, 2023
1 parent 53398c7 commit 3e33ecf
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion posttroll/testing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
"""Testing utilities."""
from contextlib import contextmanager


@contextmanager
def patched_subscriber_recv(messages):
"""Patch the Subscriber object to return given messages."""
from unittest import mock
with mock.patch("posttroll.subscriber.Subscriber.recv", mock.Mock(return_value=messages)):

def interuptible_recv(self):
"""Yield message until the subscriber is closed."""
for msg in messages:
if self._loop is False:
break
yield msg

with mock.patch("posttroll.subscriber.Subscriber.recv", interuptible_recv):
yield

@contextmanager
Expand Down

0 comments on commit 3e33ecf

Please sign in to comment.