example_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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: all_event_handler")
  32. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: timer_any_handler")
  33. dut.expect("TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_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_all("TIMER_EVENTS:TIMER_EVENT_EXPIRY: all_event_handler",
  40. "TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_any_handler",
  41. TIMER_EXPIRY_HANDLING.format(expiries))
  42. else:
  43. dut.expect_all("TIMER_EVENTS:TIMER_EVENT_STOPPED: posting to default loop",
  44. "TIMER_EVENTS:TIMER_EVENT_EXPIRY: all_event_handler",
  45. "TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_any_handler",
  46. TIMER_EXPIRY_HANDLING.format(expiries))
  47. print("Posted timer stopped event")
  48. print("Handled timer expiry event {} out of {}".format(expiries, TIMER_EVENT_LIMIT))
  49. dut.expect("TIMER_EVENTS:TIMER_EVENT_STOPPED: timer_stopped_handler")
  50. dut.expect("TIMER_EVENTS:TIMER_EVENT_STOPPED: deleted timer event source")
  51. print("Handled timer stopped event")
  52. def _test_iteration_events(dut):
  53. dut.start_app()
  54. print("Checking iteration events posting and handling")
  55. dut.expect("setting up")
  56. dut.expect("starting event sources")
  57. print("Finished setup")
  58. for iteration in range(1, TASK_ITERATION_LIMIT + 1):
  59. dut.expect(TASK_ITERATION_POST.format(iteration))
  60. print("Posted iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  61. if iteration < TASK_UNREGISTRATION_LIMIT:
  62. dut.expect_all("TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler", TASK_ITERATION_HANDLING.format(iteration))
  63. elif iteration == TASK_UNREGISTRATION_LIMIT:
  64. dut.expect_all("TASK_EVENTS:TASK_ITERATION_EVENT: unregistering task_iteration_handler", "TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler")
  65. print("Unregistered handler at iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  66. else:
  67. dut.expect_all("TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler")
  68. print("Handled iteration {} out of {}".format(iteration, TASK_ITERATION_LIMIT))
  69. dut.expect("TASK_EVENTS:TASK_ITERATION_EVENT: deleting task event source")
  70. print("Deleted task event source")
  71. @IDF.idf_example_test(env_tag='Example_WIFI')
  72. def test_default_event_loop_example(env, extra_data):
  73. dut = env.get_dut('default_event_loop', 'examples/system/event/default_event_loop')
  74. _test_iteration_events(dut)
  75. _test_timer_events(dut)
  76. if __name__ == '__main__':
  77. test_default_event_loop_example()