01-con-discon-success.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # Test whether a client produces a correct connect and subsequent disconnect.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id 01-con-discon-success
  5. # The test will send a CONNACK message to the client with rc=0. Upon receiving
  6. # the CONNACK and verifying that rc=0, the client should send a DISCONNECT
  7. # message. If rc!=0, the client should exit with an error.
  8. from mosq_test_helper import *
  9. port = mosq_test.get_lib_port()
  10. rc = 1
  11. keepalive = 60
  12. connect_packet = mosq_test.gen_connect("01-con-discon-success", keepalive=keepalive)
  13. connack_packet = mosq_test.gen_connack(rc=0)
  14. disconnect_packet = mosq_test.gen_disconnect()
  15. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  17. sock.settimeout(10)
  18. sock.bind(('', port))
  19. sock.listen(5)
  20. client_args = sys.argv[1:]
  21. env = dict(os.environ)
  22. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  23. try:
  24. pp = env['PYTHONPATH']
  25. except KeyError:
  26. pp = ''
  27. env['PYTHONPATH'] = '../../lib/python:'+pp
  28. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  29. try:
  30. (conn, address) = sock.accept()
  31. conn.settimeout(10)
  32. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  33. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  34. rc = 0
  35. conn.close()
  36. except mosq_test.TestError:
  37. pass
  38. finally:
  39. #client.terminate()
  40. client.wait()
  41. sock.close()
  42. exit(rc)