07-will-delay-session-expiry.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # Test whether a client that connects with a will delay that is longer than
  3. # their session expiry interval has their will published.
  4. # MQTT 5
  5. # https://github.com/eclipse/mosquitto/issues/1401
  6. from mosq_test_helper import *
  7. rc = 1
  8. keepalive = 60
  9. mid = 1
  10. connect1_packet = mosq_test.gen_connect("will-test", keepalive=keepalive, proto_ver=5)
  11. connack1_packet = mosq_test.gen_connack(rc=0, proto_ver=5)
  12. will_props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_WILL_DELAY_INTERVAL, 4)
  13. connect_props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 2)
  14. connect2_packet = mosq_test.gen_connect("will-helper", keepalive=keepalive, proto_ver=5, properties=connect_props, will_topic="will/test", will_payload=b"will delay", will_qos=2, will_properties=will_props)
  15. connack2_packet = mosq_test.gen_connack(rc=0, proto_ver=5)
  16. subscribe_packet = mosq_test.gen_subscribe(mid, "will/test", 0, proto_ver=5)
  17. suback_packet = mosq_test.gen_suback(mid, 0, proto_ver=5)
  18. publish_packet = mosq_test.gen_publish("will/test", qos=0, payload="will delay", proto_ver=5)
  19. port = mosq_test.get_port()
  20. broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
  21. try:
  22. sock1 = mosq_test.do_client_connect(connect1_packet, connack1_packet, timeout=30, port=port, connack_error="connack1")
  23. mosq_test.do_send_receive(sock1, subscribe_packet, suback_packet, "suback")
  24. sock2 = mosq_test.do_client_connect(connect2_packet, connack2_packet, timeout=30, port=port, connack_error="connack2")
  25. time.sleep(1)
  26. sock2.close()
  27. # Wait for session to expire
  28. time.sleep(3)
  29. mosq_test.expect_packet(sock1, "publish", publish_packet)
  30. rc = 0
  31. sock1.close()
  32. except mosq_test.TestError:
  33. pass
  34. finally:
  35. broker.terminate()
  36. broker.wait()
  37. (stdo, stde) = broker.communicate()
  38. if rc:
  39. print(stde.decode('utf-8'))
  40. exit(rc)