example_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. try:
  5. import IDF
  6. except ImportError:
  7. # this is a test case write with tiny-test-fw.
  8. # to run test cases outside tiny-test-fw,
  9. # we need to set environment variable `TEST_FW_PATH`,
  10. # then get and insert `TEST_FW_PATH` to sys path before import FW module
  11. test_fw_path = os.getenv('TEST_FW_PATH')
  12. if test_fw_path and test_fw_path not in sys.path:
  13. sys.path.insert(0, test_fw_path)
  14. import IDF
  15. # Timer events
  16. TIMER_EVENT_LIMIT = 3
  17. TIMER_EXPIRY_HANDLING = "TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_expiry_handler, executed {} out of " + str(TIMER_EVENT_LIMIT) + " times"
  18. # Task events
  19. TASK_ITERATION_LIMIT = 5
  20. TASK_UNREGISTRATION_LIMIT = 3
  21. TASK_ITERATION_POST = "TASK_EVENTS:TASK_ITERATION_EVENT: posting to default loop, {} out of " + str(TASK_ITERATION_LIMIT)
  22. TASK_ITERATION_HANDLING = "TASK_EVENTS:TASK_ITERATION_EVENT: task_iteration_handler, executed {} times"
  23. def _test_timer_events(dut):
  24. dut.start_app()
  25. print("Checking timer events posting and handling")
  26. dut.expect("setting up")
  27. dut.expect("starting event sources")
  28. print("Finished setup")
  29. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: posting to default loop")
  30. print("Posted timer started event")
  31. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_handler")
  32. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: timer_any_handler")
  33. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: all_event_handler")
  34. print("Handled timer started event")
  35. for expiries in range(1, TIMER_EVENT_LIMIT + 1):
  36. dut.expect("TIMER_EVENTS:TIMER_EVENT_EXPIRY: posting to default loop")
  37. print("Posted timer expiry event {} out of {}".format(expiries, TIMER_EVENT_LIMIT))
  38. if expiries >= TIMER_EVENT_LIMIT:
  39. dut.expect("TIMER_EVENTS:TIMER_EVENT_STOPPED: posting to default loop")
  40. print("Posted timer stopped event")
  41. dut.expect(TIMER_EXPIRY_HANDLING.format(expiries))
  42. dut.expect("TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_any_handler")
  43. dut.expect("TIMER_EVENTS:TIMER_EVENT_EXPIRY: all_event_handler")
  44. print("Handled timer expiry event {} out of {}".format(expiries, TIMER_EVENT_LIMIT))
  45. dut.expect("TIMER_EVENTS:TIMER_EVENT_STOPPED: timer_stopped_handler")
  46. dut.expect("TIMER_EVENTS:TIMER_EVENT_STOPPED: deleted timer event source")
  47. print("Handled timer stopped event")
  48. def _test_iteration_events(dut):
  49. dut.start_app()
  50. print("Checking iteration events posting and handling")
  51. dut.expect("setting up")
  52. dut.expect("starting event sources")
  53. print("Finished setup")
  54. for iteration in range(1, TASK_ITERATION_LIMIT + 1):
  55. dut.expect(TASK_ITERATION_POST.format(iteration))
  56. print("Posted iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  57. if iteration < TASK_UNREGISTRATION_LIMIT:
  58. dut.expect(TASK_ITERATION_HANDLING.format(iteration))
  59. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler")
  60. elif iteration == TASK_UNREGISTRATION_LIMIT:
  61. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: unregistering task_iteration_handler")
  62. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler")
  63. print("Unregistered handler at iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  64. else:
  65. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler")
  66. print("Handled iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  67. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: deleting task event source")
  68. print("Deleted task event source")
  69. @IDF.idf_example_test(env_tag='Example_WIFI')
  70. def test_default_event_loop_example(env, extra_data):
  71. dut = env.get_dut('default_event_loop', 'examples/system/esp_event/default_event_loop')
  72. _test_iteration_events(dut)
  73. _test_timer_events(dut)
  74. if __name__ == '__main__':
  75. test_default_event_loop_example()