timertest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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
  41. action1(void *arg)
  42. {
  43. int *p = (int *)arg;
  44. (*p)--;
  45. if (*p < -1) {
  46. ck_abort_msg("Periodic timer called too often");
  47. return 0;
  48. }
  49. return 1;
  50. }
  51. START_TEST(test_timer_cyclic)
  52. {
  53. struct mg_context ctx;
  54. int c[10];
  55. memset(&ctx, 0, sizeof(ctx));
  56. memset(c, 0, sizeof(c));
  57. mark_point();
  58. timers_init(&ctx);
  59. mg_sleep(100);
  60. mark_point();
  61. c[0] = 10;
  62. timer_add(&ctx, 0, 0.1, 1, action1, c + 0);
  63. c[2] = 2;
  64. timer_add(&ctx, 0, 0.5, 1, action1, c + 2);
  65. c[1] = 5;
  66. timer_add(&ctx, 0, 0.2, 1, action1, c + 1);
  67. mark_point();
  68. mg_sleep(1000); /* Sleep 1 second - timer will run */
  69. mark_point();
  70. ctx.stop_flag = 99; /* End timer thread */
  71. mark_point();
  72. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  73. mark_point();
  74. timers_exit(&ctx);
  75. mark_point();
  76. mg_sleep(100);
  77. #ifdef LOCAL_TEST
  78. /* If performed locally (on a physical machine that's not overloaded),
  79. * timing will be precise to the ~100 ms required here. */
  80. ck_assert_int_eq(c[0], 0);
  81. ck_assert_int_eq(c[1], 0);
  82. ck_assert_int_eq(c[2], 0);
  83. #else
  84. /* If this test runs in a virtual environment, like the CI unit test
  85. * containers, there might be some timing deviations, so check the
  86. * counter with some tolerance. */
  87. ck_assert_int_ge(c[0], -1);
  88. ck_assert_int_le(c[0], +1);
  89. ck_assert_int_ge(c[1], -1);
  90. ck_assert_int_le(c[1], +1);
  91. ck_assert_int_ge(c[2], -1);
  92. ck_assert_int_le(c[2], +1);
  93. #endif
  94. }
  95. END_TEST
  96. static int
  97. action2(void *arg)
  98. {
  99. int *p = (int *)arg;
  100. (*p)--;
  101. ck_assert_int_ge(*p, -1);
  102. return 0;
  103. }
  104. START_TEST(test_timer_oneshot)
  105. {
  106. struct mg_context ctx;
  107. int c[10];
  108. memset(&ctx, 0, sizeof(ctx));
  109. memset(c, 0, sizeof(c));
  110. mark_point();
  111. timers_init(&ctx);
  112. mg_sleep(100);
  113. mark_point();
  114. c[0] = 10;
  115. timer_add(&ctx, 0, 0.1, 1, action2, c + 0);
  116. c[2] = 2;
  117. timer_add(&ctx, 0, 0.5, 1, action2, c + 2);
  118. c[1] = 5;
  119. timer_add(&ctx, 0, 0.2, 1, action2, c + 1);
  120. mark_point();
  121. mg_sleep(1000); /* Sleep 1 second - timer will run */
  122. mark_point();
  123. ctx.stop_flag = 99; /* End timer thread */
  124. mark_point();
  125. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  126. mark_point();
  127. timers_exit(&ctx);
  128. mark_point();
  129. mg_sleep(100);
  130. ck_assert_int_eq(c[0], 9);
  131. ck_assert_int_eq(c[1], 4);
  132. ck_assert_int_eq(c[2], 1);
  133. }
  134. END_TEST
  135. Suite *
  136. make_timertest_suite(void)
  137. {
  138. Suite *const suite = suite_create("Timer");
  139. TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
  140. TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
  141. tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
  142. tcase_set_timeout(tcase_timer_cyclic, 30);
  143. suite_add_tcase(suite, tcase_timer_cyclic);
  144. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot);
  145. tcase_set_timeout(tcase_timer_oneshot, 30);
  146. suite_add_tcase(suite, tcase_timer_oneshot);
  147. return suite;
  148. }
  149. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  150. /* Used to debug test cases without using the check framework */
  151. void
  152. TIMER_PRIVATE(void)
  153. {
  154. #if defined(_WIN32)
  155. WSADATA data;
  156. WSAStartup(MAKEWORD(2, 2), &data);
  157. #endif
  158. test_timer_cyclic(0);
  159. test_timer_oneshot(0);
  160. #if defined(_WIN32)
  161. WSACleanup();
  162. #endif
  163. }
  164. #endif