Skip to content

Commit

Permalink
Add test for user-callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreF committed Jan 14, 2024
1 parent c21388c commit a63b1dc
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 2 deletions.
124 changes: 124 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,127 @@ def test_change_error_code_to_enum(self):
# This might probably not be done: User might use rc as number in
# operation
assert rc_ok + 1 == 1

def test_callback_v1_mqtt3(self, fake_broker):
callback_called = []
mqttc = client.Client(
"client-id",
userdata=callback_called,
)

def on_connect(cl, userdata, flags, rc):
assert isinstance(cl, client.Client)
assert isinstance(flags, dict)
assert isinstance(flags["session present"], int)
assert isinstance(rc, int)
userdata.append("on_connect")
cl.subscribe([("topic", 0)])

def on_subscribe(cl, userdata, mid, granted_qos):
assert isinstance(cl, client.Client)
assert isinstance(mid, int)
assert isinstance(granted_qos, tuple)
assert isinstance(granted_qos[0], int)
userdata.append("on_subscribe")
cl.publish("topic", "payload", 2)

def on_publish(cl, userdata, mid):
assert isinstance(cl, client.Client)
assert isinstance(mid, int)
userdata.append("on_publish")

def on_message(cl, userdata, message):
assert isinstance(cl, client.Client)
assert isinstance(message, client.MQTTMessage)
userdata.append("on_message")
cl.unsubscribe("topic")

def on_unsubscribe(cl, userdata, mid):
assert isinstance(cl, client.Client)
assert isinstance(mid, int)
userdata.append("on_unsubscribe")
cl.disconnect()

def on_disconnect(cl, userdata, rc):
assert isinstance(cl, client.Client)
assert isinstance(rc, int)
userdata.append("on_disconnect")

mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_publish = on_publish
mqttc.on_message = on_message
mqttc.on_unsubscribe = on_unsubscribe
mqttc.on_disconnect = on_disconnect

mqttc.enable_logger()
mqttc.connect_async("localhost", fake_broker.port)
mqttc.loop_start()

try:
fake_broker.start()

connect_packet = paho_test.gen_connect(
"client-id", keepalive=60)
fake_broker.expect_packet("connect", connect_packet)

connack_packet = paho_test.gen_connack(rc=0)
count = fake_broker.send_packet(connack_packet)
assert count # Check connection was not closed
assert count == len(connack_packet)

subscribe_packet = paho_test.gen_subscribe(1, "topic", 0)
fake_broker.expect_packet("subscribe", subscribe_packet)

suback_packet = paho_test.gen_suback(1, 0)
count = fake_broker.send_packet(suback_packet)
assert count # Check connection was not closed
assert count == len(suback_packet)

publish_packet = paho_test.gen_publish("topic", 2, "payload", mid=2)
fake_broker.expect_packet("publish", publish_packet)

pubrec_packet = paho_test.gen_pubrec(mid=2)
count = fake_broker.send_packet(pubrec_packet)
assert count # Check connection was not closed
assert count == len(pubrec_packet)

pubrel_packet = paho_test.gen_pubrel(mid=2)
fake_broker.expect_packet("pubrel", pubrel_packet)

pubcomp_packet = paho_test.gen_pubcomp(mid=2)
count = fake_broker.send_packet(pubcomp_packet)
assert count # Check connection was not closed
assert count == len(pubcomp_packet)

publish_from_broker_packet = paho_test.gen_publish("topic", qos=0, payload="payload", mid=99)
count = fake_broker.send_packet(publish_from_broker_packet)
assert count # Check connection was not closed
assert count == len(publish_from_broker_packet)

unsubscribe_packet = paho_test.gen_unsubscribe(mid=3, topic="topic")
fake_broker.expect_packet("unsubscribe", unsubscribe_packet)

suback_packet = paho_test.gen_unsuback(mid=3)
count = fake_broker.send_packet(suback_packet)
assert count # Check connection was not closed
assert count == len(suback_packet)

disconnect_packet = paho_test.gen_disconnect()
fake_broker.expect_packet("disconnect", disconnect_packet)

assert callback_called == [
"on_connect",
"on_subscribe",
"on_publish",
"on_message",
"on_unsubscribe",
"on_disconnect",
]

finally:
mqttc.disconnect()
mqttc.loop_stop()

packet_in = fake_broker.receive_packet(1)
assert not packet_in # Check connection is closed
12 changes: 10 additions & 2 deletions tests/testsupport/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

import pytest

from tests import paho_test


class FakeBroker:
def __init__(self):
# Bind to "localhost" for maximum performance, as described in:
# http://docs.python.org/howto/sockets.html#ipc
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(30)
sock.settimeout(5)
sock.bind(("localhost", 0))
self.port = sock.getsockname()[1]
sock.listen(1)
Expand All @@ -25,7 +27,7 @@ def start(self):
raise ValueError('Socket is not open')

(conn, address) = self._sock.accept()
conn.settimeout(10)
conn.settimeout(5)
self._conn = conn

def finish(self):
Expand All @@ -51,6 +53,12 @@ def send_packet(self, packet_out):
count = self._conn.send(packet_out)
return count

def expect_packet(self, name, packet):
if self._conn is None:
raise ValueError('Connection is not open')

paho_test.expect_packet(self._conn, name, packet)


@pytest.fixture
def fake_broker():
Expand Down

0 comments on commit a63b1dc

Please sign in to comment.