mqtt_ws_example_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import print_function
  2. from __future__ import unicode_literals
  3. from builtins import str
  4. import re
  5. import os
  6. import sys
  7. import time
  8. import paho.mqtt.client as mqtt
  9. try:
  10. import IDF
  11. except Exception:
  12. # this is a test case write with tiny-test-fw.
  13. # to run test cases outside tiny-test-fw,
  14. # we need to set environment variable `TEST_FW_PATH`,
  15. # then get and insert `TEST_FW_PATH` to sys path before import FW module
  16. test_fw_path = os.getenv("TEST_FW_PATH")
  17. if test_fw_path and test_fw_path not in sys.path:
  18. sys.path.insert(0, test_fw_path)
  19. import IDF
  20. import DUT
  21. g_recv_data = ""
  22. g_recv_topic = ""
  23. g_broker_connected = 0
  24. # The callback for when the client receives a CONNACK response from the server.
  25. def on_connect(client, userdata, flags, rc):
  26. global g_broker_connected
  27. print("Connected with result code " + str(rc))
  28. g_broker_connected = 1
  29. client.subscribe("/topic/qos0")
  30. # The callback for when a PUBLISH message is received from the server.
  31. def on_message(client, userdata, msg):
  32. global g_recv_topic
  33. global g_recv_data
  34. payload = msg.payload.decode()
  35. if g_recv_data == "" and payload == "data":
  36. client.publish("/topic/qos0", "data_to_esp32")
  37. g_recv_topic = msg.topic
  38. g_recv_data = payload
  39. print(msg.topic + " " + payload)
  40. @IDF.idf_example_test(env_tag="Example_WIFI")
  41. def test_examples_protocol_mqtt_ws(env, extra_data):
  42. global g_recv_topic
  43. global g_recv_data
  44. global g_broker_connected
  45. broker_url = "iot.eclipse.org"
  46. """
  47. steps: |
  48. 1. join AP and connects to ws broker
  49. 2. Test connects a client to the same broker
  50. 3. Test evaluates it received correct qos0 message
  51. 4. Test ESP32 client received correct qos0 message
  52. """
  53. dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws")
  54. # check and log bin size
  55. binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin")
  56. bin_size = os.path.getsize(binary_file)
  57. IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024))
  58. IDF.check_performance("mqtt_websocket_size", bin_size // 1024)
  59. # 1. start test (and check the environment is healthy)
  60. dut1.start_app()
  61. client = None
  62. # 2. Test connects to a broker
  63. try:
  64. ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
  65. print("Connected to AP with IP: {}".format(ip_address))
  66. client = mqtt.Client(transport="websockets")
  67. client.on_connect = on_connect
  68. client.on_message = on_message
  69. client.ws_set_options(path="/ws", headers=None)
  70. print("Connecting...")
  71. client.connect(broker_url, 80, 60)
  72. print("...done")
  73. except DUT.ExpectTimeout:
  74. raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
  75. except Exception:
  76. print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
  77. raise
  78. print("Start Looping...")
  79. start = time.time()
  80. while (time.time() - start) <= 20:
  81. client.loop()
  82. print("...done")
  83. if g_broker_connected == 0:
  84. raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
  85. # 3. check the message received back from the server
  86. if g_recv_topic == "/topic/qos0" and g_recv_data == "data":
  87. print("PASS: Received correct message")
  88. else:
  89. print("Failure!")
  90. raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data))
  91. # 4. check that the esp32 client received data sent by this python client
  92. dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30)
  93. if __name__ == '__main__':
  94. test_examples_protocol_mqtt_ws()