03-publish-b2c-qos1.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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-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.
  7. # The test will send the client a PUBLISH message with topic
  8. # "pub/qos1/receive", payload of "message", QoS=1 and mid=123. The client
  9. # should handle this as per the spec by sending a PUBACK message.
  10. # The client should then exit with return code==0.
  11. from mosq_test_helper import *
  12. port = mosq_test.get_lib_port()
  13. rc = 1
  14. keepalive = 60
  15. connect_packet = mosq_test.gen_connect("publish-qos1-test", keepalive=keepalive)
  16. connack_packet = mosq_test.gen_connack(rc=0)
  17. disconnect_packet = mosq_test.gen_disconnect()
  18. mid = 123
  19. publish_packet = mosq_test.gen_publish("pub/qos1/receive", qos=1, mid=mid, payload="message")
  20. puback_packet = mosq_test.gen_puback(mid)
  21. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  22. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  23. sock.settimeout(10)
  24. sock.bind(('', port))
  25. sock.listen(5)
  26. client_args = sys.argv[1:]
  27. env = dict(os.environ)
  28. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  29. try:
  30. pp = env['PYTHONPATH']
  31. except KeyError:
  32. pp = ''
  33. env['PYTHONPATH'] = '../../lib/python:'+pp
  34. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  35. try:
  36. (conn, address) = sock.accept()
  37. conn.settimeout(10)
  38. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  39. mosq_test.do_send_receive(conn, publish_packet, puback_packet, "puback")
  40. rc = 0
  41. conn.close()
  42. except mosq_test.TestError:
  43. pass
  44. finally:
  45. for i in range(0, 5):
  46. if client.returncode != None:
  47. break
  48. time.sleep(0.1)
  49. try:
  50. client.terminate()
  51. except OSError:
  52. pass
  53. client.wait()
  54. sock.close()
  55. if client.returncode != 0:
  56. exit(1)
  57. exit(rc)