timertest.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. action(void *arg)
  42. {
  43. int *p = (int *)arg;
  44. (*p)--;
  45. return 1;
  46. }
  47. START_TEST(test_timer1)
  48. {
  49. struct mg_context ctx;
  50. int c[10];
  51. memset(&ctx, 0, sizeof(ctx));
  52. memset(c, 0, sizeof(c));
  53. mark_point();
  54. timers_init(&ctx);
  55. mg_sleep(100);
  56. mark_point();
  57. c[0] = 10;
  58. timer_add(&ctx, 0, 0.1, 1, action, c + 0);
  59. c[2] = 2;
  60. timer_add(&ctx, 0, 0.5, 1, action, c + 2);
  61. c[1] = 5;
  62. timer_add(&ctx, 0, 0.2, 1, action, c + 1);
  63. mg_sleep(1000);
  64. ctx.stop_flag = 99;
  65. mg_sleep(100);
  66. timers_exit(&ctx);
  67. #ifdef LOCAL_TEST
  68. ck_assert_int_eq(c[0], 0);
  69. ck_assert_int_eq(c[1], 0);
  70. ck_assert_int_eq(c[2], 0);
  71. #else
  72. ck_assert_int_ge(c[0], -1);
  73. ck_assert_int_le(c[0], +1);
  74. ck_assert_int_ge(c[1], -1);
  75. ck_assert_int_le(c[1], +1);
  76. ck_assert_int_ge(c[2], -1);
  77. ck_assert_int_le(c[2], +1);
  78. #endif
  79. }
  80. END_TEST
  81. Suite *
  82. make_timertest_suite(void)
  83. {
  84. Suite *const suite = suite_create("Timer");
  85. TCase *const tcase_timer1 = tcase_create("Timer1");
  86. tcase_add_test(tcase_timer1, test_timer1);
  87. tcase_set_timeout(tcase_timer1, civetweb_min_test_timeout);
  88. suite_add_tcase(suite, tcase_timer1);
  89. return suite;
  90. }
  91. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  92. /* Used to debug test cases without using the check framework */
  93. void
  94. TIMER_PRIVATE(void)
  95. {
  96. #if defined(_WIN32)
  97. WSADATA data;
  98. WSAStartup(MAKEWORD(2, 2), &data);
  99. #endif
  100. test_timer1(0);
  101. #if defined(_WIN32)
  102. WSACleanup();
  103. #endif
  104. }
  105. #endif