timertest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 here would be unreachable code */
  49. }
  50. return (*p >= -3) ? action_dec_ret : 0;
  51. }
  52. static int
  53. action_dec_to_0(void *arg)
  54. {
  55. int *p = (int *)arg;
  56. (*p)--;
  57. if (*p <= -1) {
  58. ck_abort_msg("Periodic timer called too often");
  59. /* return 0 here would be unreachable code */
  60. }
  61. return (*p > 0);
  62. }
  63. START_TEST(test_timer_cyclic)
  64. {
  65. struct mg_context ctx;
  66. int c[10];
  67. memset(&ctx, 0, sizeof(ctx));
  68. memset(c, 0, sizeof(c));
  69. action_dec_ret = 1;
  70. mark_point();
  71. timers_init(&ctx);
  72. mg_sleep(100);
  73. mark_point();
  74. c[0] = 10;
  75. timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
  76. c[2] = 2;
  77. timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
  78. c[1] = 5;
  79. timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
  80. mark_point();
  81. mg_sleep(1000); /* Sleep 1 second - timer will run */
  82. mark_point();
  83. ctx.stop_flag = 99; /* End timer thread */
  84. mark_point();
  85. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  86. mark_point();
  87. timers_exit(&ctx);
  88. mark_point();
  89. mg_sleep(100);
  90. #ifdef LOCAL_TEST
  91. /* If performed locally (on a physical machine that's not overloaded),
  92. * timing will be precise to the ~100 ms required here. */
  93. ck_assert_int_eq(c[0], 0);
  94. ck_assert_int_eq(c[1], 0);
  95. ck_assert_int_eq(c[2], 0);
  96. #else
  97. /* If this test runs in a virtual environment, like the CI unit test
  98. * containers, there might be some timing deviations, so check the
  99. * counter with some tolerance. */
  100. ck_assert_int_ge(c[0], -1);
  101. ck_assert_int_le(c[0], +1);
  102. ck_assert_int_ge(c[1], -1);
  103. ck_assert_int_le(c[1], +1);
  104. ck_assert_int_ge(c[2], -1);
  105. ck_assert_int_le(c[2], +1);
  106. #endif
  107. }
  108. END_TEST
  109. START_TEST(test_timer_oneshot_by_callback_retval)
  110. {
  111. struct mg_context ctx;
  112. int c[10];
  113. memset(&ctx, 0, sizeof(ctx));
  114. memset(c, 0, sizeof(c));
  115. action_dec_ret = 0;
  116. mark_point();
  117. timers_init(&ctx);
  118. mg_sleep(100);
  119. mark_point();
  120. c[0] = 10;
  121. timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
  122. c[2] = 2;
  123. timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
  124. c[1] = 5;
  125. timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
  126. mark_point();
  127. mg_sleep(1000); /* Sleep 1 second - timer will run */
  128. mark_point();
  129. ctx.stop_flag = 99; /* End timer thread */
  130. mark_point();
  131. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  132. mark_point();
  133. timers_exit(&ctx);
  134. mark_point();
  135. mg_sleep(100);
  136. ck_assert_int_eq(c[0], 9);
  137. ck_assert_int_eq(c[1], 4);
  138. ck_assert_int_eq(c[2], 1);
  139. }
  140. END_TEST
  141. START_TEST(test_timer_oneshot_by_timer_add)
  142. {
  143. struct mg_context ctx;
  144. int c[10];
  145. memset(&ctx, 0, sizeof(ctx));
  146. memset(c, 0, sizeof(c));
  147. action_dec_ret = 1;
  148. mark_point();
  149. timers_init(&ctx);
  150. mg_sleep(100);
  151. mark_point();
  152. c[0] = 10;
  153. timer_add(&ctx, 0, 0, 1, action_dec, c + 0);
  154. c[2] = 2;
  155. timer_add(&ctx, 0, 0, 1, action_dec, c + 2);
  156. c[1] = 5;
  157. timer_add(&ctx, 0, 0, 1, action_dec, c + 1);
  158. mark_point();
  159. mg_sleep(1000); /* Sleep 1 second - timer will run */
  160. mark_point();
  161. ctx.stop_flag = 99; /* End timer thread */
  162. mark_point();
  163. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  164. mark_point();
  165. timers_exit(&ctx);
  166. mark_point();
  167. mg_sleep(100);
  168. ck_assert_int_eq(c[0], 9);
  169. ck_assert_int_eq(c[1], 4);
  170. ck_assert_int_eq(c[2], 1);
  171. }
  172. END_TEST
  173. START_TEST(test_timer_mixed)
  174. {
  175. struct mg_context ctx;
  176. int c[10];
  177. memset(&ctx, 0, sizeof(ctx));
  178. memset(c, 0, sizeof(c));
  179. mark_point();
  180. timers_init(&ctx);
  181. mg_sleep(100);
  182. mark_point();
  183. /* 3 --> 2, because it is a single shot timer */
  184. c[0] = 3;
  185. timer_add(&ctx, 0, 0, 1, action_dec_to_0, c + 0);
  186. /* 3 --> 0, because it will run until c[1] = 0 and then stop */
  187. c[1] = 3;
  188. timer_add(&ctx, 0, 0.2, 1, action_dec_to_0, c + 1);
  189. /* 3 --> 1, with 750 ms period, it will run once at start,
  190. * then once 750 ms later, but not 1500 ms later, since the
  191. * timer is already stopped then. */
  192. c[2] = 3;
  193. timer_add(&ctx, 0, 0.75, 1, action_dec_to_0, c + 2);
  194. /* 3 --> 2, will run at start, but no cyclic in 1 second */
  195. c[3] = 3;
  196. timer_add(&ctx, 0, 2.5, 1, action_dec_to_0, c + 3);
  197. /* 3 --> 3, will not run at start */
  198. c[4] = 3;
  199. timer_add(&ctx, 2.5, 0.1, 1, action_dec_to_0, c + 4);
  200. /* 3 --> 2, an absolute timer in the past (-123.456) will still
  201. * run once at start, and then with the period */
  202. c[5] = 3;
  203. timer_add(&ctx, -123.456, 2.5, 0, action_dec_to_0, c + 5);
  204. /* 3 --> 1, an absolute timer in the past (-123.456) will still
  205. * run once at start, and then with the period */
  206. c[6] = 3;
  207. timer_add(&ctx, -123.456, 0.75, 0, action_dec_to_0, c + 6);
  208. mark_point();
  209. mg_sleep(1000); /* Sleep 1 second - timer will run */
  210. mark_point();
  211. ctx.stop_flag = 99; /* End timer thread */
  212. mark_point();
  213. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  214. mark_point();
  215. timers_exit(&ctx);
  216. mark_point();
  217. mg_sleep(100);
  218. ck_assert_int_eq(c[0], 2);
  219. ck_assert_int_eq(c[1], 0);
  220. ck_assert_int_eq(c[2], 1);
  221. ck_assert_int_eq(c[3], 2);
  222. ck_assert_int_eq(c[4], 3);
  223. ck_assert_int_eq(c[5], 2);
  224. ck_assert_int_eq(c[6], 1);
  225. }
  226. END_TEST
  227. Suite *
  228. make_timertest_suite(void)
  229. {
  230. Suite *const suite = suite_create("Timer");
  231. TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
  232. TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
  233. TCase *const tcase_timer_mixed = tcase_create("Timer Mixed");
  234. tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
  235. tcase_set_timeout(tcase_timer_cyclic, 30);
  236. suite_add_tcase(suite, tcase_timer_cyclic);
  237. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_timer_add);
  238. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_callback_retval);
  239. tcase_set_timeout(tcase_timer_oneshot, 30);
  240. suite_add_tcase(suite, tcase_timer_oneshot);
  241. tcase_add_test(tcase_timer_mixed, test_timer_mixed);
  242. tcase_set_timeout(tcase_timer_mixed, 30);
  243. suite_add_tcase(suite, tcase_timer_mixed);
  244. return suite;
  245. }
  246. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  247. /* Used to debug test cases without using the check framework */
  248. void
  249. TIMER_PRIVATE(void)
  250. {
  251. unsigned f_avail;
  252. unsigned f_ret;
  253. #if defined(_WIN32)
  254. WSADATA data;
  255. WSAStartup(MAKEWORD(2, 2), &data);
  256. #endif
  257. f_avail = mg_check_feature(0xFF);
  258. f_ret = mg_init_library(f_avail);
  259. ck_assert_uint_eq(f_ret, f_avail);
  260. test_timer_cyclic(0);
  261. test_timer_oneshot_by_timer_add(0);
  262. test_timer_oneshot_by_callback_retval(0);
  263. test_timer_mixed(0);
  264. mg_exit_library();
  265. #if defined(_WIN32)
  266. WSACleanup();
  267. #endif
  268. }
  269. #endif