04-retain-qos0-clear.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. # Test whether a retained PUBLISH is cleared when a zero length retained
  3. # message is published to a topic.
  4. from mosq_test_helper import *
  5. def do_test(proto_ver):
  6. rc = 1
  7. keepalive = 60
  8. connect_packet = mosq_test.gen_connect("retain-clear-test", keepalive=keepalive, proto_ver=proto_ver)
  9. connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver)
  10. publish_packet = mosq_test.gen_publish("retain/clear/test", qos=0, payload="retained message", retain=True, proto_ver=proto_ver)
  11. retain_clear_packet = mosq_test.gen_publish("retain/clear/test", qos=0, payload=None, retain=True, proto_ver=proto_ver)
  12. mid_sub = 592
  13. subscribe_packet = mosq_test.gen_subscribe(mid_sub, "retain/clear/test", 0, proto_ver=proto_ver)
  14. suback_packet = mosq_test.gen_suback(mid_sub, 0, proto_ver=proto_ver)
  15. mid_unsub = 593
  16. unsubscribe_packet = mosq_test.gen_unsubscribe(mid_unsub, "retain/clear/test", proto_ver=proto_ver)
  17. unsuback_packet = mosq_test.gen_unsuback(mid_unsub, proto_ver=proto_ver)
  18. port = mosq_test.get_port()
  19. broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
  20. try:
  21. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=4, port=port)
  22. # Send retained message
  23. sock.send(publish_packet)
  24. # Subscribe to topic, we should get the retained message back.
  25. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  26. mosq_test.expect_packet(sock, "publish", publish_packet)
  27. # Now unsubscribe from the topic before we clear the retained
  28. # message.
  29. mosq_test.do_send_receive(sock, unsubscribe_packet, unsuback_packet, "unsuback")
  30. # Now clear the retained message.
  31. sock.send(retain_clear_packet)
  32. # Subscribe to topic, we shouldn't get anything back apart
  33. # from the SUBACK.
  34. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  35. time.sleep(1)
  36. # If we do get something back, it should be before this ping, so if
  37. # this succeeds then we're ok.
  38. mosq_test.do_ping(sock)
  39. # This is the expected event
  40. rc = 0
  41. sock.close()
  42. except mosq_test.TestError:
  43. pass
  44. finally:
  45. broker.terminate()
  46. broker.wait()
  47. (stdo, stde) = broker.communicate()
  48. if rc:
  49. print(stde.decode('utf-8'))
  50. print("proto_ver=%d" % (proto_ver))
  51. exit(rc)
  52. do_test(proto_ver=4)
  53. do_test(proto_ver=5)
  54. exit(0)