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