libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_timers.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 void sleep_ (long timeout_)
33 {
34 #if defined ZMQ_HAVE_WINDOWS
35  Sleep (timeout_ > 0 ? timeout_ : INFINITE);
36 #elif defined ZMQ_HAVE_ANDROID
37  usleep (timeout_ * 1000);
38 #else
39  usleep (timeout_ * 1000);
40 #endif
41 }
42 
43 void handler (int timer_id, void* arg)
44 {
45  (void) timer_id; // Stop 'unused' compiler warnings
46  *((bool *)arg) = true;
47 }
48 
49 int sleep_and_execute(void *timers_)
50 {
51  int timeout = zmq_timers_timeout (timers_);
52 
53  // Sleep methods are inaccurate, so we sleep in a loop until time arrived
54  while (timeout > 0) {
55  sleep_ (timeout);
56  timeout = zmq_timers_timeout(timers_);
57  }
58 
59  return zmq_timers_execute(timers_);
60 }
61 
62 int main (void)
63 {
65 
66  void* timers = zmq_timers_new ();
67  assert (timers);
68 
69  bool timer_invoked = false;
70 
71  int timer_id = zmq_timers_add (timers, 100, handler, &timer_invoked);
72  assert (timer_id);
73 
74  // Timer should be invoked yet
75  int rc = zmq_timers_execute (timers);
76  assert (rc == 0);
77  assert (!timer_invoked);
78 
79  // Wait half the time and check again
80  sleep_ (zmq_timers_timeout (timers) / 2);
81  rc = zmq_timers_execute (timers);
82  assert (rc == 0);
83  assert (!timer_invoked);
84 
85  // Wait until the end
86  rc = sleep_and_execute (timers);
87  assert (rc == 0);
88  assert (timer_invoked);
89  timer_invoked = false;
90 
91  // Wait half the time and check again
92  long timeout = zmq_timers_timeout (timers);
93  sleep_ (timeout / 2);
94  rc = zmq_timers_execute (timers);
95  assert (rc == 0);
96  assert (!timer_invoked);
97 
98  // Reset timer and wait half of the time left
99  rc = zmq_timers_reset (timers, timer_id);
100  sleep_ (timeout / 2);
101  rc = zmq_timers_execute (timers);
102  assert (rc == 0);
103  assert (!timer_invoked);
104 
105  // Wait until the end
106  rc = sleep_and_execute(timers);
107  assert (rc == 0);
108  assert (timer_invoked);
109  timer_invoked = false;
110 
111  // reschedule
112  zmq_timers_set_interval (timers, timer_id, 50);
113  rc = sleep_and_execute(timers);
114  assert (rc == 0);
115  assert (timer_invoked);
116  timer_invoked = false;
117 
118  // cancel timer
119  timeout = zmq_timers_timeout (timers);
120  zmq_timers_cancel (timers, timer_id);
121  sleep_ (timeout * 2);
122  rc = zmq_timers_execute (timers);
123  assert (rc == 0);
124  assert (!timer_invoked);
125 
126  rc = zmq_timers_destroy (&timers);
127  assert (rc == 0);
128 
129  return 0;
130 }
int main(void)
Definition: test_timers.cpp:62
int zmq_timers_execute(void *timers)
Definition: zmq.cpp:1311
void setup_test_environment(void)
Definition: testutil.hpp:285
void handler(int timer_id, void *arg)
Definition: test_timers.cpp:43
int zmq_timers_cancel(void *timers, int timer_id)
Definition: zmq.cpp:1271
int zmq_timers_destroy(void **timers_p)
Definition: zmq.cpp:1249
int zmq_timers_add(void *timers, size_t interval, zmq_timer_fn handler, void *arg)
Definition: zmq.cpp:1261
long zmq_timers_timeout(void *timers)
Definition: zmq.cpp:1301
int zmq_timers_reset(void *timers, int timer_id)
Definition: zmq.cpp:1291
void * zmq_timers_new(void)
Definition: zmq.cpp:1242
void sleep_(long timeout_)
Definition: test_timers.cpp:32
int sleep_and_execute(void *timers_)
Definition: test_timers.cpp:49
int zmq_timers_set_interval(void *timers, int timer_id, size_t interval)
Definition: zmq.cpp:1281