test_timers.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #define __STDC_LIMIT_MACROS // to define SIZE_MAX with older compilers
  25. #include "testutil.hpp"
  26. #include "testutil_unity.hpp"
  27. void setUp ()
  28. {
  29. }
  30. void tearDown ()
  31. {
  32. }
  33. void handler (int timer_id_, void *arg_)
  34. {
  35. (void) timer_id_; // Stop 'unused' compiler warnings
  36. *(static_cast<bool *> (arg_)) = true;
  37. }
  38. int sleep_and_execute (void *timers_)
  39. {
  40. int timeout = zmq_timers_timeout (timers_);
  41. // Sleep methods are inaccurate, so we sleep in a loop until time arrived
  42. while (timeout > 0) {
  43. msleep (timeout);
  44. timeout = zmq_timers_timeout (timers_);
  45. }
  46. return zmq_timers_execute (timers_);
  47. }
  48. void test_null_timer_pointers ()
  49. {
  50. void *timers = NULL;
  51. TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_destroy (&timers));
  52. // TODO this currently triggers an access violation
  53. #if 0
  54. TEST_ASSERT_FAILURE_ERRNO(EFAULT, zmq_timers_destroy (NULL));
  55. #endif
  56. const size_t dummy_interval = 100;
  57. const int dummy_timer_id = 1;
  58. TEST_ASSERT_FAILURE_ERRNO (
  59. EFAULT, zmq_timers_add (timers, dummy_interval, &handler, NULL));
  60. TEST_ASSERT_FAILURE_ERRNO (
  61. EFAULT, zmq_timers_add (&timers, dummy_interval, &handler, NULL));
  62. TEST_ASSERT_FAILURE_ERRNO (EFAULT,
  63. zmq_timers_cancel (timers, dummy_timer_id));
  64. TEST_ASSERT_FAILURE_ERRNO (EFAULT,
  65. zmq_timers_cancel (&timers, dummy_timer_id));
  66. TEST_ASSERT_FAILURE_ERRNO (
  67. EFAULT, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
  68. TEST_ASSERT_FAILURE_ERRNO (
  69. EFAULT,
  70. zmq_timers_set_interval (&timers, dummy_timer_id, dummy_interval));
  71. TEST_ASSERT_FAILURE_ERRNO (EFAULT,
  72. zmq_timers_reset (timers, dummy_timer_id));
  73. TEST_ASSERT_FAILURE_ERRNO (EFAULT,
  74. zmq_timers_reset (&timers, dummy_timer_id));
  75. TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_timeout (timers));
  76. TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_timeout (&timers));
  77. TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_execute (timers));
  78. TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_execute (&timers));
  79. }
  80. void test_corner_cases ()
  81. {
  82. void *timers = zmq_timers_new ();
  83. TEST_ASSERT_NOT_NULL (timers);
  84. const size_t dummy_interval = SIZE_MAX;
  85. const int dummy_timer_id = 1;
  86. // attempt to cancel non-existent timer
  87. TEST_ASSERT_FAILURE_ERRNO (EINVAL,
  88. zmq_timers_cancel (timers, dummy_timer_id));
  89. // attempt to set interval of non-existent timer
  90. TEST_ASSERT_FAILURE_ERRNO (
  91. EINVAL, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
  92. // attempt to reset non-existent timer
  93. TEST_ASSERT_FAILURE_ERRNO (EINVAL,
  94. zmq_timers_reset (timers, dummy_timer_id));
  95. // attempt to add NULL handler
  96. TEST_ASSERT_FAILURE_ERRNO (
  97. EFAULT, zmq_timers_add (timers, dummy_interval, NULL, NULL));
  98. const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
  99. zmq_timers_add (timers, dummy_interval, handler, NULL));
  100. // attempt to cancel timer twice
  101. // TODO should this case really be an error? canceling twice could be allowed
  102. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
  103. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_timers_cancel (timers, timer_id));
  104. // timeout without any timers active
  105. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_timers_timeout (timers));
  106. // cleanup
  107. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_destroy (&timers));
  108. }
  109. void test_timers ()
  110. {
  111. void *timers = zmq_timers_new ();
  112. TEST_ASSERT_NOT_NULL (timers);
  113. bool timer_invoked = false;
  114. const unsigned long full_timeout = 100;
  115. void *const stopwatch = zmq_stopwatch_start ();
  116. const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
  117. zmq_timers_add (timers, full_timeout, handler, &timer_invoked));
  118. // Timer should not have been invoked yet
  119. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
  120. if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
  121. TEST_ASSERT_FALSE (timer_invoked);
  122. }
  123. // Wait half the time and check again
  124. long timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
  125. msleep (timeout / 2);
  126. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
  127. if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
  128. TEST_ASSERT_FALSE (timer_invoked);
  129. }
  130. // Wait until the end
  131. TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
  132. TEST_ASSERT_TRUE (timer_invoked);
  133. timer_invoked = false;
  134. // Wait half the time and check again
  135. timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
  136. msleep (timeout / 2);
  137. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
  138. if (zmq_stopwatch_intermediate (stopwatch) < 2 * full_timeout) {
  139. TEST_ASSERT_FALSE (timer_invoked);
  140. }
  141. // Reset timer and wait half of the time left
  142. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_reset (timers, timer_id));
  143. msleep (timeout / 2);
  144. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
  145. if (zmq_stopwatch_stop (stopwatch) < 2 * full_timeout) {
  146. TEST_ASSERT_FALSE (timer_invoked);
  147. }
  148. // Wait until the end
  149. TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
  150. TEST_ASSERT_TRUE (timer_invoked);
  151. timer_invoked = false;
  152. // reschedule
  153. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_set_interval (timers, timer_id, 50));
  154. TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
  155. TEST_ASSERT_TRUE (timer_invoked);
  156. timer_invoked = false;
  157. // cancel timer
  158. timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
  159. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
  160. msleep (timeout * 2);
  161. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
  162. TEST_ASSERT_FALSE (timer_invoked);
  163. TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_destroy (&timers));
  164. }
  165. int main ()
  166. {
  167. setup_test_environment ();
  168. UNITY_BEGIN ();
  169. RUN_TEST (test_timers);
  170. RUN_TEST (test_null_timer_pointers);
  171. RUN_TEST (test_corner_cases);
  172. return UNITY_END ();
  173. }