timertest.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* Copyright (c) 2016-2017 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] = 100;
  75. timer_add(&ctx, 0.05, 0.1, 1, action_dec, c + 0);
  76. c[2] = 20;
  77. timer_add(&ctx, 0.25, 0.5, 1, action_dec, c + 2);
  78. c[1] = 50;
  79. timer_add(&ctx, 0.1, 0.2, 1, action_dec, c + 1);
  80. mark_point();
  81. mg_sleep(10000); /* Sleep 10 second - timers will run */
  82. mark_point();
  83. ctx.stop_flag = 99; /* End timer thread */
  84. mark_point();
  85. mg_sleep(2000); /* Sleep 2 second - timers will not run */
  86. mark_point();
  87. timers_exit(&ctx);
  88. mark_point();
  89. /* If this test runs in a virtual environment, like the CI unit test
  90. * containers, there might be some timing deviations, so check the
  91. * counter with some tolerance. */
  92. #if defined(__MINGW32__)
  93. /* For unknown reasons, the MinGW build on AppVeyor will cause a SegFault
  94. * when calling ck_assert_int_ge / ck_assert_int_le, while it works for
  95. * other compilers. See
  96. https://ci.appveyor.com/project/civetweb/civetweb/build/job/epsqi8perbca1jd6
  97. https://github.com/civetweb/civetweb/issues/366#issuecomment-269383810
  98. */
  99. {
  100. /* The three conditions -1 <= c[n] <= +1 (n=0..2) are fulfilled,
  101. * if the sum(n=0..2) of (c[n])^2 is <= 3. */
  102. int d = (c[0] * c[0]) + (c[1] * c[1]) + (c[2] * c[2]);
  103. /* ck_assert_int_le(d, 3); <-- will crash */
  104. if (d > 3) {
  105. ck_abort_msg("Timer must be zero: (%i, %i, %i)", c[0], c[1], c[2]);
  106. }
  107. }
  108. #else
  109. ck_assert_int_ge(c[0], -1);
  110. ck_assert_int_le(c[0], +1);
  111. ck_assert_int_ge(c[1], -1);
  112. ck_assert_int_le(c[1], +1);
  113. ck_assert_int_ge(c[2], -1);
  114. ck_assert_int_le(c[2], +1);
  115. #endif
  116. }
  117. END_TEST
  118. START_TEST(test_timer_oneshot_by_callback_retval)
  119. {
  120. struct mg_context ctx;
  121. int c[10];
  122. memset(&ctx, 0, sizeof(ctx));
  123. memset(c, 0, sizeof(c));
  124. action_dec_ret = 0;
  125. mark_point();
  126. timers_init(&ctx);
  127. mg_sleep(100);
  128. mark_point();
  129. c[0] = 10;
  130. timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
  131. c[2] = 2;
  132. timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
  133. c[1] = 5;
  134. timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
  135. mark_point();
  136. mg_sleep(1000); /* Sleep 1 second - timer will run */
  137. mark_point();
  138. ctx.stop_flag = 99; /* End timer thread */
  139. mark_point();
  140. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  141. mark_point();
  142. timers_exit(&ctx);
  143. mark_point();
  144. mg_sleep(100);
  145. ck_assert_int_eq(c[0], 9);
  146. ck_assert_int_eq(c[1], 4);
  147. ck_assert_int_eq(c[2], 1);
  148. }
  149. END_TEST
  150. START_TEST(test_timer_oneshot_by_timer_add)
  151. {
  152. struct mg_context ctx;
  153. int c[10];
  154. memset(&ctx, 0, sizeof(ctx));
  155. memset(c, 0, sizeof(c));
  156. action_dec_ret = 1;
  157. mark_point();
  158. timers_init(&ctx);
  159. mg_sleep(100);
  160. mark_point();
  161. c[0] = 10;
  162. timer_add(&ctx, 0, 0, 1, action_dec, c + 0);
  163. c[2] = 2;
  164. timer_add(&ctx, 0, 0, 1, action_dec, c + 2);
  165. c[1] = 5;
  166. timer_add(&ctx, 0, 0, 1, action_dec, c + 1);
  167. mark_point();
  168. mg_sleep(1000); /* Sleep 1 second - timer will run */
  169. mark_point();
  170. ctx.stop_flag = 99; /* End timer thread */
  171. mark_point();
  172. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  173. mark_point();
  174. timers_exit(&ctx);
  175. mark_point();
  176. mg_sleep(100);
  177. ck_assert_int_eq(c[0], 9);
  178. ck_assert_int_eq(c[1], 4);
  179. ck_assert_int_eq(c[2], 1);
  180. }
  181. END_TEST
  182. START_TEST(test_timer_mixed)
  183. {
  184. struct mg_context ctx;
  185. int c[10];
  186. memset(&ctx, 0, sizeof(ctx));
  187. memset(c, 0, sizeof(c));
  188. mark_point();
  189. timers_init(&ctx);
  190. mg_sleep(100);
  191. mark_point();
  192. /* 3 --> 2, because it is a single shot timer */
  193. c[0] = 3;
  194. timer_add(&ctx, 0, 0, 1, action_dec_to_0, &c[0]);
  195. /* 3 --> 0, because it will run until c[1] = 0 and then stop */
  196. c[1] = 3;
  197. timer_add(&ctx, 0, 0.2, 1, action_dec_to_0, &c[1]);
  198. /* 3 --> 1, with 750 ms period, it will run once at start,
  199. * then once 750 ms later, but not 1500 ms later, since the
  200. * timer is already stopped then. */
  201. c[2] = 3;
  202. timer_add(&ctx, 0, 0.75, 1, action_dec_to_0, &c[2]);
  203. /* 3 --> 2, will run at start, but no cyclic in 1 second */
  204. c[3] = 3;
  205. timer_add(&ctx, 0, 2.5, 1, action_dec_to_0, &c[3]);
  206. /* 3 --> 3, will not run at start */
  207. c[4] = 3;
  208. timer_add(&ctx, 2.5, 0.1, 1, action_dec_to_0, &c[4]);
  209. /* 3 --> 2, an absolute timer in the past (-123.456) will still
  210. * run once at start, and then with the period */
  211. c[5] = 3;
  212. timer_add(&ctx, -123.456, 2.5, 0, action_dec_to_0, &c[5]);
  213. /* 3 --> 1, an absolute timer in the past (-123.456) will still
  214. * run once at start, and then with the period */
  215. c[6] = 3;
  216. timer_add(&ctx, -123.456, 0.75, 0, action_dec_to_0, &c[6]);
  217. mark_point();
  218. mg_sleep(1000); /* Sleep 1 second - timer will run */
  219. mark_point();
  220. ctx.stop_flag = 99; /* End timer thread */
  221. mark_point();
  222. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  223. mark_point();
  224. timers_exit(&ctx);
  225. mark_point();
  226. mg_sleep(100);
  227. #if defined(__MINGW32__)
  228. /* For unknown reasons, the MinGW build on AppVeyor will cause a SegFault
  229. * when calling ck_assert_int_ge / ck_assert_int_le, while it works for
  230. * other compilers. See
  231. https://ci.appveyor.com/project/civetweb/civetweb/build/job/epsqi8perbca1jd6
  232. https://github.com/civetweb/civetweb/issues/366#issuecomment-269383810
  233. */
  234. {
  235. char errbuf[256] = {0};
  236. if (c[0] != 2) {
  237. sprintf(errbuf + strlen(errbuf), "0: (%i != 2)\r\n", c[0]);
  238. }
  239. if (c[1] != 0) {
  240. sprintf(errbuf + strlen(errbuf), "1: (%i != 0)\r\n", c[1]);
  241. }
  242. if (c[2] != 1) {
  243. sprintf(errbuf + strlen(errbuf), "2: (%i != 1)\r\n", c[2]);
  244. }
  245. if (c[3] != 2) {
  246. sprintf(errbuf + strlen(errbuf), "3: (%i != 2)\r\n", c[3]);
  247. }
  248. if (c[4] != 3) {
  249. sprintf(errbuf + strlen(errbuf), "4: (%i != 3)\r\n", c[4]);
  250. }
  251. if (c[5] != 2) {
  252. sprintf(errbuf + strlen(errbuf), "5: (%i != 2)\r\n", c[5]);
  253. }
  254. if (c[6] != 1) {
  255. sprintf(errbuf + strlen(errbuf), "6: (%i != 1)\r\n", c[6]);
  256. }
  257. if (errbuf[0]) {
  258. ck_abort_msg("Timer error:\r\n%s", errbuf);
  259. }
  260. }
  261. #else
  262. ck_assert_int_eq(c[0], 2);
  263. ck_assert_int_eq(c[1], 0);
  264. ck_assert_int_eq(c[2], 1);
  265. ck_assert_int_eq(c[3], 2);
  266. ck_assert_int_eq(c[4], 3);
  267. ck_assert_int_eq(c[5], 2);
  268. ck_assert_int_eq(c[6], 1);
  269. #endif
  270. }
  271. END_TEST
  272. Suite *
  273. make_timertest_suite(void)
  274. {
  275. Suite *const suite = suite_create("Timer");
  276. TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
  277. TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
  278. TCase *const tcase_timer_mixed = tcase_create("Timer Mixed");
  279. tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
  280. tcase_set_timeout(tcase_timer_cyclic, 30);
  281. suite_add_tcase(suite, tcase_timer_cyclic);
  282. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_timer_add);
  283. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_callback_retval);
  284. tcase_set_timeout(tcase_timer_oneshot, 30);
  285. suite_add_tcase(suite, tcase_timer_oneshot);
  286. tcase_add_test(tcase_timer_mixed, test_timer_mixed);
  287. tcase_set_timeout(tcase_timer_mixed, 30);
  288. suite_add_tcase(suite, tcase_timer_mixed);
  289. return suite;
  290. }
  291. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  292. /* Used to debug test cases without using the check framework */
  293. void
  294. TIMER_PRIVATE(void)
  295. {
  296. unsigned f_avail;
  297. unsigned f_ret;
  298. #if defined(_WIN32)
  299. WSADATA data;
  300. WSAStartup(MAKEWORD(2, 2), &data);
  301. #endif
  302. f_avail = mg_check_feature(0xFF);
  303. f_ret = mg_init_library(f_avail);
  304. ck_assert_uint_eq(f_ret, f_avail);
  305. test_timer_cyclic(0);
  306. test_timer_oneshot_by_timer_add(0);
  307. test_timer_oneshot_by_callback_retval(0);
  308. test_timer_mixed(0);
  309. mg_exit_library();
  310. #if defined(_WIN32)
  311. WSACleanup();
  312. #endif
  313. }
  314. #endif