01-will-set.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # Test whether a client produces a correct connect with a will.
  3. # Will QoS=1, will retain=1.
  4. # The client should connect to port 1888 with keepalive=60, clean session set,
  5. # client id 01-will-set will topic set to topic/on/unexpected/disconnect , will
  6. # payload set to "will message", will qos set to 1 and will retain set.
  7. from mosq_test_helper import *
  8. port = mosq_test.get_lib_port()
  9. rc = 1
  10. keepalive = 60
  11. connect_packet = mosq_test.gen_connect("01-will-set", keepalive=keepalive, will_topic="topic/on/unexpected/disconnect", will_qos=1, will_retain=True, will_payload=b"will message")
  12. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  14. sock.settimeout(10)
  15. sock.bind(('', port))
  16. sock.listen(5)
  17. client_args = sys.argv[1:]
  18. env = dict(os.environ)
  19. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  20. try:
  21. pp = env['PYTHONPATH']
  22. except KeyError:
  23. pp = ''
  24. env['PYTHONPATH'] = '../../lib/python:'+pp
  25. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  26. try:
  27. (conn, address) = sock.accept()
  28. conn.settimeout(10)
  29. mosq_test.expect_packet(conn, "connect", connect_packet)
  30. rc = 0
  31. conn.close()
  32. except mosq_test.TestError:
  33. pass
  34. finally:
  35. client.terminate()
  36. client.wait()
  37. sock.close()
  38. exit(rc)