timertest.c 2.6 KB

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