timertest.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. mg_sleep(1000); /* Sleep 1 second - timer will run */
  65. ctx.stop_flag = 99; /* End timer thread */
  66. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  67. timers_exit(&ctx);
  68. mg_sleep(100);
  69. #ifdef LOCAL_TEST
  70. ck_assert_int_eq(c[0], 0);
  71. ck_assert_int_eq(c[1], 0);
  72. ck_assert_int_eq(c[2], 0);
  73. #else
  74. ck_assert_int_ge(c[0], -1);
  75. ck_assert_int_le(c[0], +1);
  76. ck_assert_int_ge(c[1], -1);
  77. ck_assert_int_le(c[1], +1);
  78. ck_assert_int_ge(c[2], -1);
  79. ck_assert_int_le(c[2], +1);
  80. #endif
  81. }
  82. END_TEST
  83. static int
  84. action2(void *arg)
  85. {
  86. int *p = (int *)arg;
  87. (*p)--;
  88. ck_assert_int_ge(*p, -1);
  89. return 0;
  90. }
  91. START_TEST(test_timer_oneshot)
  92. {
  93. struct mg_context ctx;
  94. int c[10];
  95. memset(&ctx, 0, sizeof(ctx));
  96. memset(c, 0, sizeof(c));
  97. mark_point();
  98. timers_init(&ctx);
  99. mg_sleep(100);
  100. mark_point();
  101. c[0] = 10;
  102. timer_add(&ctx, 0, 0.1, 1, action2, c + 0);
  103. c[2] = 2;
  104. timer_add(&ctx, 0, 0.5, 1, action2, c + 2);
  105. c[1] = 5;
  106. timer_add(&ctx, 0, 0.2, 1, action2, c + 1);
  107. mg_sleep(1000); /* Sleep 1 second - timer will run */
  108. ctx.stop_flag = 99; /* End timer thread */
  109. mg_sleep(1000); /* Sleep 1 second - timer will not run */
  110. timers_exit(&ctx);
  111. mg_sleep(100);
  112. ck_assert_int_eq(c[0], 9);
  113. ck_assert_int_eq(c[1], 4);
  114. ck_assert_int_eq(c[2], 1);
  115. }
  116. END_TEST
  117. Suite *
  118. make_timertest_suite(void)
  119. {
  120. Suite *const suite = suite_create("Timer");
  121. TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
  122. TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
  123. tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
  124. tcase_set_timeout(tcase_timer_cyclic, 30);
  125. suite_add_tcase(suite, tcase_timer_cyclic);
  126. tcase_add_test(tcase_timer_oneshot, test_timer_oneshot);
  127. tcase_set_timeout(tcase_timer_oneshot, 30);
  128. suite_add_tcase(suite, tcase_timer_oneshot);
  129. return suite;
  130. }
  131. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  132. /* Used to debug test cases without using the check framework */
  133. void
  134. TIMER_PRIVATE(void)
  135. {
  136. #if defined(_WIN32)
  137. WSADATA data;
  138. WSAStartup(MAKEWORD(2, 2), &data);
  139. #endif
  140. test_timer_cyclic(0);
  141. test_timer_oneshot(0);
  142. #if defined(_WIN32)
  143. WSACleanup();
  144. #endif
  145. }
  146. #endif