03-publish-c2b-qos1-timeout.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. # Test whether a client sends a correct PUBLISH to a topic with QoS 1 and responds to a delay.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id publish-qos1-test
  5. # The test will send a CONNACK message to the client with rc=0. Upon receiving
  6. # the CONNACK the client should verify that rc==0. If not, it should exit with
  7. # return code=1.
  8. # On a successful CONNACK, the client should send a PUBLISH message with topic
  9. # "pub/qos1/test", payload "message" and QoS=1.
  10. # The test will not respond to the first PUBLISH message, so the client must
  11. # resend the PUBLISH message with dup=1. Note that to keep test durations low, a
  12. # message retry timeout of less than 10 seconds is required for this test.
  13. # On receiving the second PUBLISH message, the test will send the correct
  14. # PUBACK response. On receiving the correct PUBACK response, the client should
  15. # send a DISCONNECT message.
  16. from mosq_test_helper import *
  17. port = mosq_test.get_lib_port()
  18. rc = 1
  19. keepalive = 60
  20. connect_packet = mosq_test.gen_connect("publish-qos1-test", keepalive=keepalive)
  21. connack_packet = mosq_test.gen_connack(rc=0)
  22. disconnect_packet = mosq_test.gen_disconnect()
  23. mid = 1
  24. publish_packet = mosq_test.gen_publish("pub/qos1/test", qos=1, mid=mid, payload="message")
  25. publish_packet_dup = mosq_test.gen_publish("pub/qos1/test", qos=1, mid=mid, payload="message", dup=True)
  26. puback_packet = mosq_test.gen_puback(mid)
  27. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  28. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  29. sock.settimeout(10)
  30. sock.bind(('', port))
  31. sock.listen(5)
  32. client_args = sys.argv[1:]
  33. env = dict(os.environ)
  34. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  35. try:
  36. pp = env['PYTHONPATH']
  37. except KeyError:
  38. pp = ''
  39. env['PYTHONPATH'] = '../../lib/python:'+pp
  40. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  41. try:
  42. (conn, address) = sock.accept()
  43. conn.settimeout(10)
  44. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  45. mosq_test.expect_packet(conn, "publish", publish_packet)
  46. # Delay for > 3 seconds (message retry time)
  47. mosq_test.do_receive_send(conn, publish_packet_dup, puback_packet, "dup publish")
  48. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  49. rc = 0
  50. conn.close()
  51. except mosq_test.TestError:
  52. pass
  53. finally:
  54. client.terminate()
  55. client.wait()
  56. sock.close()
  57. exit(rc)