timertest.c 2.5 KB

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