timertest.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #pragma GCC diagnostic push
  31. #pragma GCC diagnostic ignored "-Wunused-function"
  32. #endif
  33. #define CIVETWEB_API static
  34. #define USE_TIMERS
  35. #include "../src/civetweb.c"
  36. #include <stdlib.h>
  37. #include <time.h>
  38. #include "timertest.h"
  39. static int
  40. action(void *arg)
  41. {
  42. int *p = (int *)arg;
  43. (*p)--;
  44. return 1;
  45. }
  46. START_TEST(test_timer1)
  47. {
  48. struct mg_context ctx;
  49. int c[10];
  50. memset(&ctx, 0, sizeof(ctx));
  51. memset(c, 0, sizeof(c));
  52. mark_point();
  53. timers_init(&ctx);
  54. mg_sleep(100);
  55. mark_point();
  56. c[0] = 10;
  57. timer_add(&ctx, 0, 0.1, 1, action, c + 0);
  58. c[2] = 2;
  59. timer_add(&ctx, 0, 0.5, 1, action, c + 2);
  60. c[1] = 5;
  61. timer_add(&ctx, 0, 0.2, 1, action, c + 1);
  62. mg_sleep(1000);
  63. ctx.stop_flag = 99;
  64. mg_sleep(100);
  65. timers_exit(&ctx);
  66. ck_assert_int_eq(c[0], 0);
  67. ck_assert_int_eq(c[1], 0);
  68. ck_assert_int_eq(c[2], 0);
  69. }
  70. END_TEST
  71. Suite *
  72. make_timertest_suite(void)
  73. {
  74. Suite *const suite = suite_create("Timer");
  75. TCase *const tcase_timer1 = tcase_create("Timer1");
  76. tcase_add_test(tcase_timer1, test_timer1);
  77. tcase_set_timeout(tcase_timer1, civetweb_min_test_timeout);
  78. suite_add_tcase(suite, tcase_timer1);
  79. return suite;
  80. }
  81. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  82. /* Used to debug test cases without using the check framework */
  83. void
  84. TIMER_PRIVATE(void)
  85. {
  86. #if defined(_WIN32)
  87. WSADATA data;
  88. WSAStartup(MAKEWORD(2, 2), &data);
  89. #endif
  90. test_timer1(0);
  91. #if defined(_WIN32)
  92. WSACleanup();
  93. #endif
  94. }
  95. #endif