]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/test/deadline_timer.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / test / deadline_timer.cpp
1 //
2 // deadline_timer.cpp
3 // ~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
15
16 // Test that header file is self-contained.
17 #include <boost/asio/deadline_timer.hpp>
18
19 #include "unit_test.hpp"
20
21 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
22
23 #include <boost/bind.hpp>
24 #include "archetypes/async_result.hpp"
25 #include <boost/asio/executor_work_guard.hpp>
26 #include <boost/asio/io_context.hpp>
27 #include <boost/asio/placeholders.hpp>
28 #include <boost/asio/detail/thread.hpp>
29
30 using namespace boost::posix_time;
31
32 void increment(int* count)
33 {
34 ++(*count);
35 }
36
37 void decrement_to_zero(boost::asio::deadline_timer* t, int* count)
38 {
39 if (*count > 0)
40 {
41 --(*count);
42
43 int before_value = *count;
44
45 t->expires_at(t->expires_at() + seconds(1));
46 t->async_wait(boost::bind(decrement_to_zero, t, count));
47
48 // Completion cannot nest, so count value should remain unchanged.
49 BOOST_ASIO_CHECK(*count == before_value);
50 }
51 }
52
53 void increment_if_not_cancelled(int* count,
54 const boost::system::error_code& ec)
55 {
56 if (!ec)
57 ++(*count);
58 }
59
60 void cancel_timer(boost::asio::deadline_timer* t)
61 {
62 std::size_t num_cancelled = t->cancel();
63 BOOST_ASIO_CHECK(num_cancelled == 1);
64 }
65
66 void cancel_one_timer(boost::asio::deadline_timer* t)
67 {
68 std::size_t num_cancelled = t->cancel_one();
69 BOOST_ASIO_CHECK(num_cancelled == 1);
70 }
71
72 ptime now()
73 {
74 #if defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
75 return microsec_clock::universal_time();
76 #else // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
77 return second_clock::universal_time();
78 #endif // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
79 }
80
81 void deadline_timer_test()
82 {
83 boost::asio::io_context ioc;
84 int count = 0;
85
86 ptime start = now();
87
88 boost::asio::deadline_timer t1(ioc, seconds(1));
89 t1.wait();
90
91 // The timer must block until after its expiry time.
92 ptime end = now();
93 ptime expected_end = start + seconds(1);
94 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
95
96 start = now();
97
98 boost::asio::deadline_timer t2(ioc, seconds(1) + microseconds(500000));
99 t2.wait();
100
101 // The timer must block until after its expiry time.
102 end = now();
103 expected_end = start + seconds(1) + microseconds(500000);
104 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
105
106 t2.expires_at(t2.expires_at() + seconds(1));
107 t2.wait();
108
109 // The timer must block until after its expiry time.
110 end = now();
111 expected_end += seconds(1);
112 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
113
114 start = now();
115
116 t2.expires_from_now(seconds(1) + microseconds(200000));
117 t2.wait();
118
119 // The timer must block until after its expiry time.
120 end = now();
121 expected_end = start + seconds(1) + microseconds(200000);
122 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
123
124 start = now();
125
126 boost::asio::deadline_timer t3(ioc, seconds(5));
127 t3.async_wait(boost::bind(increment, &count));
128
129 // No completions can be delivered until run() is called.
130 BOOST_ASIO_CHECK(count == 0);
131
132 ioc.run();
133
134 // The run() call will not return until all operations have finished, and
135 // this should not be until after the timer's expiry time.
136 BOOST_ASIO_CHECK(count == 1);
137 end = now();
138 expected_end = start + seconds(1);
139 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
140
141 count = 3;
142 start = now();
143
144 boost::asio::deadline_timer t4(ioc, seconds(1));
145 t4.async_wait(boost::bind(decrement_to_zero, &t4, &count));
146
147 // No completions can be delivered until run() is called.
148 BOOST_ASIO_CHECK(count == 3);
149
150 ioc.restart();
151 ioc.run();
152
153 // The run() call will not return until all operations have finished, and
154 // this should not be until after the timer's final expiry time.
155 BOOST_ASIO_CHECK(count == 0);
156 end = now();
157 expected_end = start + seconds(3);
158 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
159
160 count = 0;
161 start = now();
162
163 boost::asio::deadline_timer t5(ioc, seconds(10));
164 t5.async_wait(boost::bind(increment_if_not_cancelled, &count,
165 boost::asio::placeholders::error));
166 boost::asio::deadline_timer t6(ioc, seconds(1));
167 t6.async_wait(boost::bind(cancel_timer, &t5));
168
169 // No completions can be delivered until run() is called.
170 BOOST_ASIO_CHECK(count == 0);
171
172 ioc.restart();
173 ioc.run();
174
175 // The timer should have been cancelled, so count should not have changed.
176 // The total run time should not have been much more than 1 second (and
177 // certainly far less than 10 seconds).
178 BOOST_ASIO_CHECK(count == 0);
179 end = now();
180 expected_end = start + seconds(2);
181 BOOST_ASIO_CHECK(end < expected_end);
182
183 // Wait on the timer again without cancelling it. This time the asynchronous
184 // wait should run to completion and increment the counter.
185 t5.async_wait(boost::bind(increment_if_not_cancelled, &count,
186 boost::asio::placeholders::error));
187
188 ioc.restart();
189 ioc.run();
190
191 // The timer should not have been cancelled, so count should have changed.
192 // The total time since the timer was created should be more than 10 seconds.
193 BOOST_ASIO_CHECK(count == 1);
194 end = now();
195 expected_end = start + seconds(10);
196 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
197
198 count = 0;
199 start = now();
200
201 // Start two waits on a timer, one of which will be cancelled. The one
202 // which is not cancelled should still run to completion and increment the
203 // counter.
204 boost::asio::deadline_timer t7(ioc, seconds(3));
205 t7.async_wait(boost::bind(increment_if_not_cancelled, &count,
206 boost::asio::placeholders::error));
207 t7.async_wait(boost::bind(increment_if_not_cancelled, &count,
208 boost::asio::placeholders::error));
209 boost::asio::deadline_timer t8(ioc, seconds(1));
210 t8.async_wait(boost::bind(cancel_one_timer, &t7));
211
212 ioc.restart();
213 ioc.run();
214
215 // One of the waits should not have been cancelled, so count should have
216 // changed. The total time since the timer was created should be more than 3
217 // seconds.
218 BOOST_ASIO_CHECK(count == 1);
219 end = now();
220 expected_end = start + seconds(3);
221 BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
222 }
223
224 void timer_handler(const boost::system::error_code&)
225 {
226 }
227
228 void deadline_timer_cancel_test()
229 {
230 static boost::asio::io_context io_context;
231 struct timer
232 {
233 boost::asio::deadline_timer t;
234 timer() : t(io_context) { t.expires_at(boost::posix_time::pos_infin); }
235 } timers[50];
236
237 timers[2].t.async_wait(&timer_handler);
238 timers[41].t.async_wait(&timer_handler);
239 for (int i = 10; i < 20; ++i)
240 timers[i].t.async_wait(&timer_handler);
241
242 BOOST_ASIO_CHECK(timers[2].t.cancel() == 1);
243 BOOST_ASIO_CHECK(timers[41].t.cancel() == 1);
244 for (int i = 10; i < 20; ++i)
245 BOOST_ASIO_CHECK(timers[i].t.cancel() == 1);
246 }
247
248 struct custom_allocation_timer_handler
249 {
250 custom_allocation_timer_handler(int* count) : count_(count) {}
251 void operator()(const boost::system::error_code&) {}
252 int* count_;
253 };
254
255 void* asio_handler_allocate(std::size_t size,
256 custom_allocation_timer_handler* handler)
257 {
258 ++(*handler->count_);
259 return ::operator new(size);
260 }
261
262 void asio_handler_deallocate(void* pointer, std::size_t,
263 custom_allocation_timer_handler* handler)
264 {
265 --(*handler->count_);
266 ::operator delete(pointer);
267 }
268
269 void deadline_timer_custom_allocation_test()
270 {
271 static boost::asio::io_context io_context;
272 struct timer
273 {
274 boost::asio::deadline_timer t;
275 timer() : t(io_context) {}
276 } timers[100];
277
278 int allocation_count = 0;
279
280 for (int i = 0; i < 50; ++i)
281 {
282 timers[i].t.expires_at(boost::posix_time::pos_infin);
283 timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
284 }
285
286 for (int i = 50; i < 100; ++i)
287 {
288 timers[i].t.expires_at(boost::posix_time::neg_infin);
289 timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
290 }
291
292 for (int i = 0; i < 50; ++i)
293 timers[i].t.cancel();
294
295 io_context.run();
296
297 BOOST_ASIO_CHECK(allocation_count == 0);
298 }
299
300 void io_context_run(boost::asio::io_context* ioc)
301 {
302 ioc->run();
303 }
304
305 void deadline_timer_thread_test()
306 {
307 boost::asio::io_context ioc;
308 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work
309 = boost::asio::make_work_guard(ioc);
310 boost::asio::deadline_timer t1(ioc);
311 boost::asio::deadline_timer t2(ioc);
312 int count = 0;
313
314 boost::asio::detail::thread th(boost::bind(io_context_run, &ioc));
315
316 t2.expires_from_now(boost::posix_time::seconds(2));
317 t2.wait();
318
319 t1.expires_from_now(boost::posix_time::seconds(2));
320 t1.async_wait(boost::bind(increment, &count));
321
322 t2.expires_from_now(boost::posix_time::seconds(4));
323 t2.wait();
324
325 ioc.stop();
326 th.join();
327
328 BOOST_ASIO_CHECK(count == 1);
329 }
330
331 void deadline_timer_async_result_test()
332 {
333 boost::asio::io_context ioc;
334 boost::asio::deadline_timer t1(ioc);
335
336 t1.expires_from_now(boost::posix_time::seconds(1));
337 int i = t1.async_wait(archetypes::lazy_handler());
338 BOOST_ASIO_CHECK(i == 42);
339
340 ioc.run();
341 }
342
343 #if defined(BOOST_ASIO_HAS_MOVE)
344 boost::asio::deadline_timer make_timer(boost::asio::io_context& ioc, int* count)
345 {
346 boost::asio::deadline_timer t(ioc);
347 t.expires_from_now(boost::posix_time::seconds(1));
348 t.async_wait(boost::bind(increment, count));
349 return t;
350 }
351 #endif // defined(BOOST_ASIO_HAS_MOVE)
352
353 void deadline_timer_move_test()
354 {
355 #if defined(BOOST_ASIO_HAS_MOVE)
356 boost::asio::io_context io_context1;
357 boost::asio::io_context io_context2;
358 int count = 0;
359
360 boost::asio::deadline_timer t1 = make_timer(io_context1, &count);
361 boost::asio::deadline_timer t2 = make_timer(io_context2, &count);
362 boost::asio::deadline_timer t3 = std::move(t1);
363
364 t2 = std::move(t1);
365
366 io_context2.run();
367
368 BOOST_ASIO_CHECK(count == 1);
369
370 io_context1.run();
371
372 BOOST_ASIO_CHECK(count == 2);
373 #endif // defined(BOOST_ASIO_HAS_MOVE)
374 }
375
376 BOOST_ASIO_TEST_SUITE
377 (
378 "deadline_timer",
379 BOOST_ASIO_TEST_CASE(deadline_timer_test)
380 BOOST_ASIO_TEST_CASE(deadline_timer_cancel_test)
381 BOOST_ASIO_TEST_CASE(deadline_timer_custom_allocation_test)
382 BOOST_ASIO_TEST_CASE(deadline_timer_thread_test)
383 BOOST_ASIO_TEST_CASE(deadline_timer_async_result_test)
384 BOOST_ASIO_TEST_CASE(deadline_timer_move_test)
385 )
386 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
387 BOOST_ASIO_TEST_SUITE
388 (
389 "deadline_timer",
390 BOOST_ASIO_TEST_CASE(null_test)
391 )
392 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)