timertest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright (c) 2016 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. /**
  22. * We include the source file so that we have access to the internal private
  23. * static functions
  24. */
  25. #ifdef _MSC_VER
  26. #ifndef _CRT_SECURE_NO_WARNINGS
  27. #define _CRT_SECURE_NO_WARNINGS
  28. #endif
  29. #endif
  30. #if defined(__GNUC__)
  31. #pragma GCC diagnostic push
  32. #pragma GCC diagnostic ignored "-Wunused-function"
  33. #endif
  34. #define CIVETWEB_API static
  35. #define USE_TIMERS
  36. #include "../src/civetweb.c"
  37. #include <stdlib.h>
  38. #include <time.h>
  39. #include "timertest.h"
  40. static int action_dec_ret;
  41. static int
  42. action_dec(void *arg)
  43. {
  44. int *p = (int *)arg;
  45. (*p)--;
  46. if (*p < -1) {
  47. ck_abort_msg("Periodic timer called too often");
  48. return 0;
  49. }
  50. return action_dec_ret;
  51. }
  52. START_TEST(test_timer_cyclic)
  53. {
  54. struct mg_context ctx;
  55. int c[10];
  56. memset(&ctx, 0, sizeof(ctx));
  57. memset(c, 0, sizeof(c));
  58. action_dec_ret = 1;
  59. mark_point();
  60. timers_init(&ctx);
  61. mg_sleep(100);
  62. mark_point();
  63. c[0] = 10;
  64. timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
  65. c[2] = 2;
  66. timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
  67. c[1] = 5;
  68. timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
  69. mark_point();
  70. mg_sleep(1000); /* Sleep 1 second - timer will run */
  71. mark_point();
  72. ctx.stop_flag = 99; /* End timer thread */
  73. mark_point();
  74. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  75. mark_point();
  76. timers_exit(&ctx);
  77. mark_point();
  78. mg_sleep(100);
  79. #ifdef LOCAL_TEST
  80. /* If performed locally (on a physical machine that's not overloaded),
  81. * timing will be precise to the ~100 ms required here. */
  82. ck_assert_int_eq(c[0], 0);
  83. ck_assert_int_eq(c[1], 0);
  84. ck_assert_int_eq(c[2], 0);
  85. #else
  86. /* If this test runs in a virtual environment, like the CI unit test
  87. * containers, there might be some timing deviations, so check the
  88. * counter with some tolerance. */
  89. ck_assert_int_ge(c[0], -1);
  90. ck_assert_int_le(c[0], +1);
  91. ck_assert_int_ge(c[1], -1);
  92. ck_assert_int_le(c[1], +1);
  93. ck_assert_int_ge(c[2], -1);
  94. ck_assert_int_le(c[2], +1);
  95. #endif
  96. }
  97. END_TEST
  98. START_TEST(test_timer_oneshot_by_callback_retval)
  99. {
  100. struct mg_context ctx;
  101. int c[10];
  102. memset(&ctx, 0, sizeof(ctx));
  103. memset(c, 0, sizeof(c));
  104. action_dec_ret = 0;
  105. mark_point();
  106. timers_init(&ctx);
  107. mg_sleep(100);
  108. mark_point();
  109. c[0] = 10;
  110. timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
  111. c[2] = 2;
  112. timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
  113. c[1] = 5;
  114. timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
  115. mark_point();
  116. mg_sleep(1000); /* Sleep 1 second - timer will run */
  117. mark_point();
  118. ctx.stop_flag = 99; /* End timer thread */
  119. mark_point();
  120. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  121. mark_point();
  122. timers_exit(&ctx);
  123. mark_point();
  124. mg_sleep(100);
  125. ck_assert_int_eq(c[0], 9);
  126. ck_assert_int_eq(c[1], 4);
  127. ck_assert_int_eq(c[2], 1);
  128. }
  129. END_TEST
  130. START_TEST(test_timer_oneshot_by_timer_add)
  131. {
  132. struct mg_context ctx;
  133. int c[10];
  134. memset(&ctx, 0, sizeof(ctx));
  135. memset(c, 0, sizeof(c));
  136. action_dec_ret = 1;
  137. mark_point();
  138. timers_init(&ctx);
  139. mg_sleep(100);
  140. mark_point();
  141. c[0] = 10;
  142. timer_add(&ctx, 0, 0, 1, action_dec, c + 0);
  143. c[2] = 2;
  144. timer_add(&ctx, 0, 0, 1, action_dec, c + 2);
  145. c[1] = 5;
  146. timer_add(&ctx, 0, 0, 1, action_dec, c + 1);
  147. mark_point();
  148. mg_sleep(1000); /* Sleep 1 second - timer will run */
  149. mark_point();
  150. ctx.stop_flag = 99; /* End timer thread */
  151. mark_point();
  152. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  153. mark_point();
  154. timers_exit(&ctx);
  155. mark_point();
  156. mg_sleep(100);
  157. ck_assert_int_eq(c[0], 9);
  158. ck_assert_int_eq(c[1], 4);
  159. ck_assert_int_eq(c[2], 1);
  160. }
  161. END_TEST
  162. Suite *
  163. make_timertest_suite(void)
  164. {
  165. Suite *const suite = suite_create("Timer");
  166. TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
  167. TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
  168. tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
  169. tcase_set_timeout(tcase_timer_cyclic, 30);
  170. suite_add_tcase(suite, tcase_timer_cyclic);
  171. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_timer_add);
  172. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_callback_retval);
  173. tcase_set_timeout(tcase_timer_oneshot, 30);
  174. suite_add_tcase(suite, tcase_timer_oneshot);
  175. return suite;
  176. }
  177. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  178. /* Used to debug test cases without using the check framework */
  179. void
  180. TIMER_PRIVATE(void)
  181. {
  182. #if defined(_WIN32)
  183. WSADATA data;
  184. WSAStartup(MAKEWORD(2, 2), &data);
  185. #endif
  186. test_timer_cyclic(0);
  187. test_timer_oneshot_by_timer_add(0);
  188. test_timer_oneshot_by_callback_retval(0);
  189. #if defined(_WIN32)
  190. WSACleanup();
  191. #endif
  192. }
  193. #endif