example_test.py 4.1 KB

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