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

Crash fake publisher when not started #58

Merged
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
3 changes: 1 addition & 2 deletions posttroll/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def __init__(self, address, name="", min_port=None, max_port=None):


def start(self):
"""Start the publisher.
"""
"""Start the publisher."""
self.publish_socket = get_context().socket(zmq.PUB)
_set_tcp_keepalive(self.publish_socket)

Expand Down
7 changes: 6 additions & 1 deletion posttroll/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"""Patch the Subscriber object to return given messages."""
from unittest import mock

def interuptible_recv(self):

Check warning on line 10 in posttroll/testing.py

View check run for this annotation

Codecov / codecov/patch

posttroll/testing.py#L10

Added line #L10 was not covered by tests
"""Yield message until the subscriber is closed."""
for msg in messages:
if self._loop is False:
break
yield msg

Check warning on line 15 in posttroll/testing.py

View check run for this annotation

Codecov / codecov/patch

posttroll/testing.py#L12-L15

Added lines #L12 - L15 were not covered by tests

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

Check warning on line 17 in posttroll/testing.py

View check run for this annotation

Codecov / codecov/patch

posttroll/testing.py#L17

Added line #L17 was not covered by tests
yield

@contextmanager
Expand All @@ -24,10 +24,15 @@
published = []

def fake_send(self, message):
if getattr(self, "test_pub_started", None) is None:
raise RuntimeError("Cannot 'send' before the publisher is started.")
published.append(message)

def fake_start(self, *args, **kwargs):
self.test_pub_started = True

def noop(self, *args, **kwargs):
pass

with mock.patch.multiple("posttroll.publisher.Publisher", send=fake_send, start=noop, stop=noop):
with mock.patch.multiple("posttroll.publisher.Publisher", send=fake_send, start=fake_start, stop=noop):
yield published
13 changes: 13 additions & 0 deletions posttroll/tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest
from posttroll.testing import patched_publisher


def test_fake_publisher():
"""Test running a fake publisher."""
from posttroll.publisher import create_publisher_from_dict_config

with patched_publisher() as messages:
Expand All @@ -9,3 +12,13 @@ def test_fake_publisher():
pub.send("bla")
pub.stop()
assert "bla" in messages


def test_fake_publisher_crashes_when_not_started():
"""Test fake publisher needs to be started."""
from posttroll.publisher import create_publisher_from_dict_config

with patched_publisher():
pub = create_publisher_from_dict_config(dict(port=1979, nameservers=False))
with pytest.raises(RuntimeError):
pub.send("bla")