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

Use is_published correctly #912

Merged
merged 1 commit into from
Jan 23, 2024
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
13 changes: 11 additions & 2 deletions tavern/_plugins/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ def message_received(self, topic: str, timeout: int = 1):

return msg

def publish(self, topic, payload=None, qos=None, retain=None):
def publish(
self, topic: str, payload=None, qos=None, retain=None
) -> paho.MQTTMessageInfo:
"""publish message using paho library"""
self._wait_for_subscriptions()

Expand All @@ -389,7 +391,14 @@ def publish(self, topic, payload=None, qos=None, retain=None):
kwargs["retain"] = retain
msg = self._client.publish(topic, payload, **kwargs)

if not msg.is_published:
# Wait for 2*connect timeout which should be plenty to publish the message even with qos 2
# TODO: configurable
try:
msg.wait_for_publish(self._connect_timeout * 2)
except (RuntimeError, ValueError) as e:
raise exceptions.MQTTError("could not publish message") from e

if not msg.is_published():
raise exceptions.MQTTError(
"err {:s}: {:s}".format(
_err_vals.get(msg.rc, "unknown"), paho.error_string(msg.rc)
Expand Down
56 changes: 48 additions & 8 deletions tests/unit/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Dict
from unittest.mock import MagicMock, Mock, patch

Expand Down Expand Up @@ -71,28 +72,67 @@ def test_context_connection_success(self, fake_client):
with fake_client as x:
assert fake_client == x

def test_assert_message_published(self, fake_client):
"""If it couldn't immediately publish the message, error out"""
def test_assert_message_published_error(self, fake_client):
"""Error waiting for it to publish"""

class FakeMessage(paho.MQTTMessageInfo):
def wait_for_publish(self, timeout=None):
raise RuntimeError

rc = 1

with patch.object(fake_client._client, "subscribe"), patch.object(
fake_client._client, "publish", return_value=FakeMessage(10)
):
with pytest.raises(exceptions.MQTTError):
fake_client.publish("abc", "123")

def test_assert_message_published_failure(self, fake_client):
"""If it couldn't publish the message, error out"""

class FakeMessage(paho.MQTTMessageInfo):
def wait_for_publish(self, timeout=None):
return

def is_published(self):
return False

class FakeMessage:
is_published = False
rc = 1

with patch.object(fake_client._client, "subscribe"), patch.object(
fake_client._client, "publish", return_value=FakeMessage()
fake_client._client, "publish", return_value=FakeMessage(10)
):
with pytest.raises(exceptions.MQTTError):
fake_client.publish("abc", "123")

def test_assert_message_published_delay(self, fake_client):
"""Published but only after a small delay"""

class FakeMessage(paho.MQTTMessageInfo):
def wait_for_publish(self, timeout=None):
time.sleep(0.5)

def is_published(self):
return True

rc = 1

with patch.object(fake_client._client, "subscribe"), patch.object(
fake_client._client, "publish", return_value=FakeMessage(10)
):
fake_client.publish("abc", "123")

def test_assert_message_published_unknown_err(self, fake_client):
"""Same, but with an unknown error code"""

class FakeMessage:
is_published = False
class FakeMessage(paho.MQTTMessageInfo):
def is_published(self):
return False

rc = 2342423

with patch.object(fake_client._client, "subscribe"), patch.object(
fake_client._client, "publish", return_value=FakeMessage()
fake_client._client, "publish", return_value=FakeMessage(10)
):
with pytest.raises(exceptions.MQTTError):
fake_client.publish("abc", "123")
Expand Down