timertest.c 4.6 KB

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