test_timers.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #define __STDC_LIMIT_MACROS // to define SIZE_MAX with older compilers
4 #include "testutil.hpp"
5 #include "testutil_unity.hpp"
6 
7 void setUp ()
8 {
9 }
10 
11 void tearDown ()
12 {
13 }
14 
15 void handler (int timer_id_, void *arg_)
16 {
17  (void) timer_id_; // Stop 'unused' compiler warnings
18  *(static_cast<bool *> (arg_)) = true;
19 }
20 
21 int sleep_and_execute (void *timers_)
22 {
23  int timeout = zmq_timers_timeout (timers_);
24 
25  // Sleep methods are inaccurate, so we sleep in a loop until time arrived
26  while (timeout > 0) {
27  msleep (timeout);
28  timeout = zmq_timers_timeout (timers_);
29  }
30 
31  return zmq_timers_execute (timers_);
32 }
33 
35 {
36  void *timers = NULL;
37 
39 
40 // TODO this currently triggers an access violation
41 #if 0
43 #endif
44 
45  const size_t dummy_interval = 100;
46  const int dummy_timer_id = 1;
47 
49  EFAULT, zmq_timers_add (timers, dummy_interval, &handler, NULL));
51  EFAULT, zmq_timers_add (&timers, dummy_interval, &handler, NULL));
52 
54  zmq_timers_cancel (timers, dummy_timer_id));
56  zmq_timers_cancel (&timers, dummy_timer_id));
57 
59  EFAULT, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
61  EFAULT,
62  zmq_timers_set_interval (&timers, dummy_timer_id, dummy_interval));
63 
65  zmq_timers_reset (timers, dummy_timer_id));
67  zmq_timers_reset (&timers, dummy_timer_id));
68 
71 
74 }
75 
77 {
78  void *timers = zmq_timers_new ();
79  TEST_ASSERT_NOT_NULL (timers);
80 
81  const size_t dummy_interval = SIZE_MAX;
82  const int dummy_timer_id = 1;
83 
84  // attempt to cancel non-existent timer
86  zmq_timers_cancel (timers, dummy_timer_id));
87 
88  // attempt to set interval of non-existent timer
90  EINVAL, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
91 
92  // attempt to reset non-existent timer
94  zmq_timers_reset (timers, dummy_timer_id));
95 
96  // attempt to add NULL handler
98  EFAULT, zmq_timers_add (timers, dummy_interval, NULL, NULL));
99 
100  const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
101  zmq_timers_add (timers, dummy_interval, handler, NULL));
102 
103  // attempt to cancel timer twice
104  // TODO should this case really be an error? canceling twice could be allowed
105  TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
106 
107  TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_timers_cancel (timers, timer_id));
108 
109  // timeout without any timers active
111 
112  // cleanup
114 }
115 
116 void test_timers ()
117 {
118  void *timers = zmq_timers_new ();
119  TEST_ASSERT_NOT_NULL (timers);
120 
121  bool timer_invoked = false;
122 
123  const unsigned long full_timeout = 100;
124  void *const stopwatch = zmq_stopwatch_start ();
125 
126  const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
127  zmq_timers_add (timers, full_timeout, handler, &timer_invoked));
128 
129  // Timer should not have been invoked yet
131 
132  if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
133  TEST_ASSERT_FALSE (timer_invoked);
134  }
135 
136  // Wait half the time and check again
138  msleep (timeout / 2);
140  if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
141  TEST_ASSERT_FALSE (timer_invoked);
142  }
143 
144  // Wait until the end
146  TEST_ASSERT_TRUE (timer_invoked);
147  timer_invoked = false;
148 
149  // Wait half the time and check again
151  msleep (timeout / 2);
153  if (zmq_stopwatch_intermediate (stopwatch) < 2 * full_timeout) {
154  TEST_ASSERT_FALSE (timer_invoked);
155  }
156 
157  // Reset timer and wait half of the time left
158  TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_reset (timers, timer_id));
159  msleep (timeout / 2);
161  if (zmq_stopwatch_stop (stopwatch) < 2 * full_timeout) {
162  TEST_ASSERT_FALSE (timer_invoked);
163  }
164 
165  // Wait until the end
167  TEST_ASSERT_TRUE (timer_invoked);
168  timer_invoked = false;
169 
170  // reschedule
171  TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_set_interval (timers, timer_id, 50));
173  TEST_ASSERT_TRUE (timer_invoked);
174  timer_invoked = false;
175 
176  // cancel timer
178  TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
179  msleep (timeout * 2);
181  TEST_ASSERT_FALSE (timer_invoked);
182 
184 }
185 
186 int main ()
187 {
189 
190  UNITY_BEGIN ();
194  return UNITY_END ();
195 }
zmq_timers_add
ZMQ_EXPORT int zmq_timers_add(void *timers, size_t interval, zmq_timer_fn handler, void *arg)
Definition: zmq.cpp:1659
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
EINVAL
#define EINVAL
Definition: errno.hpp:25
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
zmq_timers_cancel
ZMQ_EXPORT int zmq_timers_cancel(void *timers, int timer_id)
Definition: zmq.cpp:1673
zmq_timers_execute
ZMQ_EXPORT int zmq_timers_execute(void *timers)
Definition: zmq.cpp:1714
zmq_timers_destroy
ZMQ_EXPORT int zmq_timers_destroy(void **timers_p)
Definition: zmq.cpp:1647
zmq_timers_new
ZMQ_EXPORT void * zmq_timers_new(void)
Definition: zmq.cpp:1640
TEST_ASSERT_FALSE
#define TEST_ASSERT_FALSE(condition)
Definition: unity.h:123
tearDown
void tearDown()
Definition: test_timers.cpp:11
zmq_stopwatch_intermediate
ZMQ_EXPORT unsigned long zmq_stopwatch_intermediate(void *watch_)
Definition: zmq_utils.cpp:40
testutil_unity.hpp
testutil.hpp
zmq_stopwatch_start
ZMQ_EXPORT void * zmq_stopwatch_start(void)
Definition: zmq_utils.cpp:32
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
sleep_and_execute
int sleep_and_execute(void *timers_)
Definition: test_timers.cpp:21
zmq_timers_reset
ZMQ_EXPORT int zmq_timers_reset(void *timers, int timer_id)
Definition: zmq.cpp:1694
test_null_timer_pointers
void test_null_timer_pointers()
Definition: test_timers.cpp:34
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
test_timers
void test_timers()
Definition: test_timers.cpp:116
test_corner_cases
void test_corner_cases()
Definition: test_timers.cpp:76
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
zmq_timers_set_interval
ZMQ_EXPORT int zmq_timers_set_interval(void *timers, int timer_id, size_t interval)
Definition: zmq.cpp:1683
zmq_timers_timeout
ZMQ_EXPORT long zmq_timers_timeout(void *timers)
Definition: zmq.cpp:1704
EFAULT
#define EFAULT
Definition: errno.hpp:17
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
setUp
void setUp()
Definition: test_timers.cpp:7
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
main
int main()
Definition: test_timers.cpp:186
zmq_stopwatch_stop
ZMQ_EXPORT unsigned long zmq_stopwatch_stop(void *watch_)
Definition: zmq_utils.cpp:47
handler
void handler(int timer_id_, void *arg_)
Definition: test_timers.cpp:15


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59