Skip to content

Commit

Permalink
Merge pull request #58 from mraspaud/feature-patched-receiver-interup…
Browse files Browse the repository at this point in the history
…tible

Crash fake publisher when not started
  • Loading branch information
mraspaud authored Dec 5, 2023
2 parents 488aa17 + 46779ab commit 93833a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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 @@ -24,10 +24,15 @@ def patched_publisher():
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")

0 comments on commit 93833a2

Please sign in to comment.