test_eloop.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "string.h"
  7. #include "esp_system.h"
  8. #include "unity.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_wifi_types.h"
  12. #include "utils/common.h"
  13. #include "utils/eloop.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "../esp_supplicant/src/esp_wifi_driver.h"
  16. #include "esp_log.h"
  17. #include "test_utils.h"
  18. #include "memory_checks.h"
  19. #include <time.h>
  20. uint32_t timeouts_usec[6] = { 10000, 1000, 10000, 5000, 15000, 1000 };
  21. uint32_t timeouts_sec[6] = { 10, 1, 10, 5, 15, 1 };
  22. int executed_order[6];
  23. int t;
  24. struct os_reltime ts;
  25. /* there is only single instance of esp_timer so no need of protection */
  26. void callback(void *a, void *b)
  27. {
  28. int *i = a;
  29. struct os_time age, now;
  30. os_get_reltime(&now);
  31. os_time_sub(&now, &ts, &age);
  32. int32_t ms_diff = (age.sec - timeouts_sec[*i]) * 1000 +
  33. (age.usec - timeouts_usec[*i]) / 1000;
  34. /* let's give 50 ms offset for this small block */
  35. if (ms_diff > 50) {
  36. executed_order[t] = -1;
  37. } else {
  38. executed_order[t] = *i;
  39. }
  40. t++;
  41. ESP_LOGI("Eloop Test", "timer[%d] ran after %d msec of scheduled time",
  42. *i, ms_diff);
  43. }
  44. extern const wifi_osi_funcs_t *wifi_funcs;
  45. /* Check if eloop runs its timers correctly & in correct order */
  46. TEST_CASE("Test eloop timers run", "[eloop]")
  47. {
  48. int execution_order[6] = {1, 5, 3, 0, 2, 4};
  49. int index[6] = {0,1,2,3,4,5};
  50. wifi_funcs = WIFI_OSI_FUNCS_INITIALIZER();
  51. if (!wifi_funcs) {
  52. TEST_ASSERT(1);
  53. }
  54. eloop_init();
  55. os_get_reltime(&ts);
  56. for (int i = 0; i < 6; i++) {
  57. eloop_register_timeout(timeouts_sec[i], timeouts_usec[i],
  58. callback, &index[i], NULL);
  59. }
  60. /* wait for all timers to run */
  61. os_sleep(20, 0);
  62. t = 0;
  63. /* check the execution order, this will also check whether they were fired at correct time */
  64. TEST_ASSERT(memcmp(execution_order, executed_order, 6*sizeof(int)) == 0);
  65. eloop_destroy();
  66. }