http_server_persistence_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import imp
  17. import re
  18. import os
  19. import sys
  20. import string
  21. import random
  22. import socket
  23. # This environment variable is expected on the host machine
  24. test_fw_path = os.getenv("TEST_FW_PATH")
  25. if test_fw_path and test_fw_path not in sys.path:
  26. sys.path.insert(0, test_fw_path)
  27. # When running on local machine execute the following before running this script
  28. # > export TEST_FW_PATH='~/esp/esp-idf/tools/tiny-test-fw'
  29. # > make print_flash_cmd | tail -n 1 > build/download.config
  30. # > make app bootloader
  31. import TinyFW
  32. import IDF
  33. # Import client module
  34. expath = os.path.dirname(os.path.realpath(__file__))
  35. client = imp.load_source("client", expath + "/scripts/adder.py")
  36. @IDF.idf_example_test(env_tag="Example_WIFI")
  37. def test_examples_protocol_http_server_persistence(env, extra_data):
  38. # Acquire DUT
  39. dut1 = env.get_dut("http_server", "examples/protocols/http_server/persistent_sockets")
  40. # Get binary file
  41. binary_file = os.path.join(dut1.app.binary_path, "persistent_sockets.bin")
  42. bin_size = os.path.getsize(binary_file)
  43. IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size//1024))
  44. IDF.check_performance("http_server_bin_size", bin_size//1024)
  45. # Upload binary and start testing
  46. dut1.start_app()
  47. # Parse IP address of STA
  48. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  49. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: (\d+)"))[0]
  50. print "Got IP : " + got_ip
  51. print "Got Port : " + got_port
  52. # Expected Logs
  53. dut1.expect("Registering URI handlers")
  54. # Run test script
  55. conn = client.start_session(got_ip, got_port)
  56. visitor = 0
  57. adder = 0
  58. # Test PUT request and initialize session context
  59. num = random.randint(0,100)
  60. client.putreq(conn, "/adder", str(num))
  61. visitor += 1
  62. dut1.expect("/adder visitor count = " + str(visitor))
  63. dut1.expect("/adder PUT handler read " + str(num))
  64. dut1.expect("PUT allocating new session")
  65. # Retest PUT request and change session context value
  66. num = random.randint(0,100)
  67. print "Adding :", num
  68. client.putreq(conn, "/adder", str(num))
  69. visitor += 1
  70. adder += num
  71. dut1.expect("/adder visitor count = " + str(visitor))
  72. dut1.expect("/adder PUT handler read " + str(num))
  73. try:
  74. # Re allocation shouldn't happen
  75. dut1.expect("PUT allocating new session")
  76. # Not expected
  77. raise RuntimeError
  78. except:
  79. # As expected
  80. pass
  81. # Test POST request and session persistence
  82. random_nums = [random.randint(0,100) for _ in range(100)]
  83. for num in random_nums:
  84. print "Adding :", num
  85. client.postreq(conn, "/adder", str(num))
  86. visitor += 1
  87. adder += num
  88. dut1.expect("/adder visitor count = " + str(visitor))
  89. dut1.expect("/adder handler read " + str(num))
  90. # Test GET request and session persistence
  91. print "Matching final sum :", adder
  92. if client.getreq(conn, "/adder") != str(adder):
  93. raise RuntimeError
  94. visitor += 1
  95. dut1.expect("/adder visitor count = " + str(visitor))
  96. dut1.expect("/adder GET handler send " + str(adder))
  97. print "Ending session"
  98. # Close connection and check for invocation of context "Free" function
  99. client.end_session(conn)
  100. dut1.expect("/adder Free Context function called")
  101. print "Validating user context data"
  102. # Start another session to check user context data
  103. conn2 = client.start_session(got_ip, got_port)
  104. num = random.randint(0,100)
  105. client.putreq(conn, "/adder", str(num))
  106. visitor += 1
  107. dut1.expect("/adder visitor count = " + str(visitor))
  108. dut1.expect("/adder PUT handler read " + str(num))
  109. dut1.expect("PUT allocating new session")
  110. client.end_session(conn)
  111. dut1.expect("/adder Free Context function called")
  112. if __name__ == '__main__':
  113. test_examples_protocol_http_server_persistence()