Skip to content

Commit

Permalink
Fix test that try to loop_stop() when not using loop_start()
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreF committed Jan 13, 2024
1 parent d449351 commit c52f872
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,44 +210,40 @@ def on_disconnect(*args):
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect

mqttc.connect("localhost", fake_broker.port)

try:
mqttc.connect("localhost", fake_broker.port)
fake_broker.start()

fake_broker.start()
# not yet connected, packet are not yet processed by loop()
assert not mqttc.is_connected()

# not yet connected, packet are not yet processed by loop()
assert not mqttc.is_connected()

# connect packet is sent during connect() call
connect_packet = paho_test.gen_connect(
"test_with_loop", keepalive=60,
proto_ver=MQTTProtocolVersion.MQTTv311)
packet_in = fake_broker.receive_packet(1000)
assert packet_in # Check connection was not closed
assert packet_in == connect_packet
# connect packet is sent during connect() call
connect_packet = paho_test.gen_connect(
"test_with_loop", keepalive=60,
proto_ver=MQTTProtocolVersion.MQTTv311)
packet_in = fake_broker.receive_packet(1000)
assert packet_in # Check connection was not closed
assert packet_in == 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)
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)

# call loop() to process the connack packet
assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_SUCCESS
# call loop() to process the connack packet
assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_SUCCESS

assert on_connect_reached.wait(1)
assert mqttc.is_connected()
assert on_connect_reached.wait(1)
assert mqttc.is_connected()

fake_broker.finish()
fake_broker.finish()

# call loop() to detect the connection lost
assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_CONN_LOST
# call loop() to detect the connection lost
assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_CONN_LOST

assert on_disconnect_reached.wait(1)
assert not mqttc.is_connected()
assert on_disconnect_reached.wait(1)
assert not mqttc.is_connected()

finally:
mqttc.loop_stop()

class TestPublishBroker2Client:

Expand Down

0 comments on commit c52f872

Please sign in to comment.