timertest.c 9.0 KB

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