Skip to content

Commit

Permalink
Fix tests with msg.retain value check
Browse files Browse the repository at this point in the history
Those tests send a message without retain and expected
`msg.retain is not False`. Before adding typing, `msg.retain` was
an integer, meaning the check was already true regardless of message
being retained or not.
After typing was added, the tests was broken and marked at xfail.

Also re-enable test_03_publish_b2c_qos2
  • Loading branch information
PierreF committed Apr 28, 2024
1 parent 7b24e06 commit d4d0715
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tests/lib/clients/03-publish-b2c-qos1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def on_message(mqttc, obj, msg):
assert msg.topic == "pub/qos1/receive", f"Invalid topic: ({msg.topic})"
assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})"
assert msg.qos == 1, f"Invalid qos: ({msg.qos})"
assert msg.retain is not False, f"Invalid retain: ({msg.retain})"
assert not msg.retain, f"Invalid retain: ({msg.retain})"


def on_connect(mqttc, obj, flags, rc):
Expand Down
15 changes: 7 additions & 8 deletions tests/lib/clients/03-publish-b2c-qos2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

import paho.mqtt.client as mqtt

from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt
from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt

expected_payload = b"message"

Expand All @@ -10,20 +12,17 @@ def on_message(mqttc, obj, msg):
assert msg.topic == "pub/qos2/receive", f"Invalid topic: ({msg.topic})"
assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})"
assert msg.qos == 2, f"Invalid qos: ({msg.qos})"
assert msg.retain is not False, f"Invalid retain: ({msg.retain})"
assert not msg.retain, f"Invalid retain: ({msg.retain})"


def on_connect(mqttc, obj, flags, rc):
assert rc == 0, f"Connect failed ({rc})"


logging.basicConfig(level=logging.DEBUG)
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos2-test", clean_session=True)
mqttc.enable_logger()
mqttc.on_connect = on_connect
mqttc.on_message = on_message

mqttc.connect("localhost", get_test_server_port())

with wait_for_keyboard_interrupt():
while True:
if mqttc.loop(0.3):
break
loop_until_keyboard_interrupt(mqttc)
4 changes: 0 additions & 4 deletions tests/lib/test_03_publish_b2c_qos1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# "pub/qos1/receive", payload of "message", QoS=1 and mid=123. The client
# should handle this as per the spec by sending a PUBACK message.
# The client should then exit with return code==0.
import pytest

import tests.paho_test as paho_test

connect_packet = paho_test.gen_connect("publish-qos1-test", keepalive=60)
Expand All @@ -23,8 +21,6 @@
puback_packet = paho_test.gen_puback(mid)


# msg.retain changed when typing was added
@pytest.mark.xfail
def test_03_publish_b2c_qos1(server_socket, start_client):
start_client("03-publish-b2c-qos1.py")

Expand Down
41 changes: 41 additions & 0 deletions tests/lib/test_03_publish_b2c_qos2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Test whether a client responds correctly to a PUBLISH with QoS 1.

# The client should connect with keepalive=60, clean session set,
# and client id publish-qos1-test
# The test will send a CONNACK message to the client with rc=0. Upon receiving
# the CONNACK the client should verify that rc==0.
# The test will send the client a PUBLISH message with topic
# "pub/qos1/receive", payload of "message", QoS=1 and mid=123. The client
# should handle this as per the spec by sending a PUBACK message.
# The client should then exit with return code==0.
import tests.paho_test as paho_test

connect_packet = paho_test.gen_connect("publish-qos2-test", keepalive=60)
connack_packet = paho_test.gen_connack(rc=0)

disconnect_packet = paho_test.gen_disconnect()

mid = 13423
publish_packet = paho_test.gen_publish(
"pub/qos2/receive", qos=2, mid=mid, payload="message")
pubrec_packet = paho_test.gen_pubrec(mid=mid)
pubrel_packet = paho_test.gen_pubrel(mid=mid)
pubcomp_packet = paho_test.gen_pubcomp(mid)


def test_03_publish_b2c_qos2(server_socket, start_client):
start_client("03-publish-b2c-qos2.py")

(conn, address) = server_socket.accept()
conn.settimeout(10)

paho_test.expect_packet(conn, "connect", connect_packet)
conn.send(connack_packet)
conn.send(publish_packet)

paho_test.expect_packet(conn, "pubrec", pubrec_packet)
conn.send(pubrel_packet)

paho_test.expect_packet(conn, "pubcomp", pubcomp_packet)

conn.close()

0 comments on commit d4d0715

Please sign in to comment.