timertest.c 8.6 KB

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