http_server_persistence_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import division
  17. from __future__ import print_function
  18. from __future__ import unicode_literals
  19. from builtins import str
  20. from builtins import range
  21. import re
  22. import os
  23. import sys
  24. import random
  25. try:
  26. import IDF
  27. except ImportError:
  28. # This environment variable is expected on the host machine
  29. test_fw_path = os.getenv("TEST_FW_PATH")
  30. if test_fw_path and test_fw_path not in sys.path:
  31. sys.path.insert(0, test_fw_path)
  32. import IDF
  33. import Utility
  34. # When running on local machine execute the following before running this script
  35. # > make app bootloader
  36. # > make print_flash_cmd | tail -n 1 > build/download.config
  37. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  38. # Import client module
  39. expath = os.path.dirname(os.path.realpath(__file__))
  40. client = Utility.load_source("client", expath + "/scripts/adder.py")
  41. @IDF.idf_example_test(env_tag="Example_WIFI")
  42. def test_examples_protocol_http_server_persistence(env, extra_data):
  43. # Acquire DUT
  44. dut1 = env.get_dut("http_server", "examples/protocols/http_server/persistent_sockets")
  45. # Get binary file
  46. binary_file = os.path.join(dut1.app.binary_path, "persistent_sockets.bin")
  47. bin_size = os.path.getsize(binary_file)
  48. IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size // 1024))
  49. IDF.check_performance("http_server_bin_size", bin_size // 1024)
  50. # Upload binary and start testing
  51. Utility.console_log("Starting http_server persistance test app")
  52. dut1.start_app()
  53. # Parse IP address of STA
  54. Utility.console_log("Waiting to connect with AP")
  55. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
  56. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  57. Utility.console_log("Got IP : " + got_ip)
  58. Utility.console_log("Got Port : " + got_port)
  59. # Expected Logs
  60. dut1.expect("Registering URI handlers", timeout=30)
  61. # Run test script
  62. conn = client.start_session(got_ip, got_port)
  63. visitor = 0
  64. adder = 0
  65. # Test PUT request and initialize session context
  66. num = random.randint(0,100)
  67. client.putreq(conn, "/adder", str(num))
  68. visitor += 1
  69. dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
  70. dut1.expect("/adder PUT handler read " + str(num), timeout=30)
  71. dut1.expect("PUT allocating new session", timeout=30)
  72. # Retest PUT request and change session context value
  73. num = random.randint(0,100)
  74. Utility.console_log("Adding: " + str(num))
  75. client.putreq(conn, "/adder", str(num))
  76. visitor += 1
  77. adder += num
  78. dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
  79. dut1.expect("/adder PUT handler read " + str(num), timeout=30)
  80. try:
  81. # Re allocation shouldn't happen
  82. dut1.expect("PUT allocating new session", timeout=30)
  83. # Not expected
  84. raise RuntimeError
  85. except Exception:
  86. # As expected
  87. pass
  88. # Test POST request and session persistence
  89. random_nums = [random.randint(0,100) for _ in range(100)]
  90. for num in random_nums:
  91. Utility.console_log("Adding: " + str(num))
  92. client.postreq(conn, "/adder", str(num))
  93. visitor += 1
  94. adder += num
  95. dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
  96. dut1.expect("/adder handler read " + str(num), timeout=30)
  97. # Test GET request and session persistence
  98. Utility.console_log("Matching final sum: " + str(adder))
  99. if client.getreq(conn, "/adder").decode() != str(adder):
  100. raise RuntimeError
  101. visitor += 1
  102. dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
  103. dut1.expect("/adder GET handler send " + str(adder), timeout=30)
  104. Utility.console_log("Ending session")
  105. # Close connection and check for invocation of context "Free" function
  106. client.end_session(conn)
  107. dut1.expect("/adder Free Context function called", timeout=30)
  108. Utility.console_log("Validating user context data")
  109. # Start another session to check user context data
  110. client.start_session(got_ip, got_port)
  111. num = random.randint(0,100)
  112. client.putreq(conn, "/adder", str(num))
  113. visitor += 1
  114. dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
  115. dut1.expect("/adder PUT handler read " + str(num), timeout=30)
  116. dut1.expect("PUT allocating new session", timeout=30)
  117. client.end_session(conn)
  118. dut1.expect("/adder Free Context function called", timeout=30)
  119. if __name__ == '__main__':
  120. test_examples_protocol_http_server_persistence()