03-publish-b2c-qos2.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # Test whether a client responds correctly to a PUBLISH with QoS 1.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id publish-qos2-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.
  7. # The test will send the client a PUBLISH message with topic
  8. # "pub/qos2/receive", payload of "message", QoS=2 and mid=13423. The client
  9. # should handle this as per the spec by sending a PUBREC message.
  10. # The test will not respond to the first PUBREC message, so the client must
  11. # resend the PUBREC 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 PUBREC with dup==1, the test will send the correct
  14. # PUBREL message. The client should respond to this with the correct PUBCOMP
  15. # message and then exit with return code=0.
  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-qos2-test", keepalive=keepalive)
  21. connack_packet = mosq_test.gen_connack(rc=0)
  22. disconnect_packet = mosq_test.gen_disconnect()
  23. mid = 13423
  24. publish_packet = mosq_test.gen_publish("pub/qos2/receive", qos=2, mid=mid, payload="message")
  25. pubrec_packet = mosq_test.gen_pubrec(mid)
  26. pubrel_packet = mosq_test.gen_pubrel(mid)
  27. pubcomp_packet = mosq_test.gen_pubcomp(mid)
  28. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  30. sock.settimeout(10)
  31. sock.bind(('', port))
  32. sock.listen(5)
  33. client_args = sys.argv[1:]
  34. env = dict(os.environ)
  35. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  36. try:
  37. pp = env['PYTHONPATH']
  38. except KeyError:
  39. pp = ''
  40. env['PYTHONPATH'] = '../../lib/python:'+pp
  41. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  42. try:
  43. (conn, address) = sock.accept()
  44. conn.settimeout(10)
  45. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  46. mosq_test.do_send_receive(conn, publish_packet, pubrec_packet, "pubrec")
  47. mosq_test.do_send_receive(conn, pubrel_packet, pubcomp_packet, "pubcomp")
  48. rc = 0
  49. conn.close()
  50. except mosq_test.TestError:
  51. pass
  52. finally:
  53. for i in range(0, 5):
  54. if client.returncode != None:
  55. break
  56. time.sleep(0.1)
  57. try:
  58. client.terminate()
  59. except OSError:
  60. pass
  61. client.wait()
  62. sock.close()
  63. if client.returncode != 0:
  64. exit(1)
  65. exit(rc)