http_server_persistence_test.py 4.6 KB

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