]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/test/deadline_timer.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / test / deadline_timer.cpp
CommitLineData
7c673cae
FG
1//
2// deadline_timer.cpp
3// ~~~~~~~~~~~~~~~~~~
4//
b32b8144 5// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7c673cae
FG
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"
b32b8144
FG
25#include <boost/asio/executor_work_guard.hpp>
26#include <boost/asio/io_context.hpp>
7c673cae
FG
27#include <boost/asio/placeholders.hpp>
28#include <boost/asio/detail/thread.hpp>
29
30using namespace boost::posix_time;
31
32void increment(int* count)
33{
34 ++(*count);
35}
36
37void 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
53void increment_if_not_cancelled(int* count,
54 const boost::system::error_code& ec)
55{
56 if (!ec)
57 ++(*count);
58}
59
60void 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
66void 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
72ptime 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
81void deadline_timer_test()
82{
b32b8144 83 boost::asio::io_context ioc;
7c673cae
FG
84 int count = 0;
85
86 ptime start = now();
87
b32b8144 88 boost::asio::deadline_timer t1(ioc, seconds(1));
7c673cae
FG
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
b32b8144 98 boost::asio::deadline_timer t2(ioc, seconds(1) + microseconds(500000));
7c673cae
FG
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
b32b8144 126 boost::asio::deadline_timer t3(ioc, seconds(5));
7c673cae
FG
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
b32b8144 132 ioc.run();
7c673cae
FG
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
b32b8144 144 boost::asio::deadline_timer t4(ioc, seconds(1));
7c673cae
FG
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
b32b8144
FG
150 ioc.restart();
151 ioc.run();
7c673cae
FG
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
b32b8144 163 boost::asio::deadline_timer t5(ioc, seconds(10));
7c673cae
FG
164 t5.async_wait(boost::bind(increment_if_not_cancelled, &count,
165 boost::asio::placeholders::error));
b32b8144 166 boost::asio::deadline_timer t6(ioc, seconds(1));
7c673cae
FG
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
b32b8144
FG
172 ioc.restart();
173 ioc.run();
7c673cae
FG
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
b32b8144
FG
188 ioc.restart();
189 ioc.run();
7c673cae
FG
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.
b32b8144 204 boost::asio::deadline_timer t7(ioc, seconds(3));
7c673cae
FG
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));
b32b8144 209 boost::asio::deadline_timer t8(ioc, seconds(1));
7c673cae
FG
210 t8.async_wait(boost::bind(cancel_one_timer, &t7));
211
b32b8144
FG
212 ioc.restart();
213 ioc.run();
7c673cae
FG
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
224void timer_handler(const boost::system::error_code&)
225{
226}
227
228void deadline_timer_cancel_test()
229{
b32b8144 230 static boost::asio::io_context io_context;
7c673cae
FG
231 struct timer
232 {
233 boost::asio::deadline_timer t;
b32b8144 234 timer() : t(io_context) { t.expires_at(boost::posix_time::pos_infin); }
7c673cae
FG
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
248struct 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
255void* asio_handler_allocate(std::size_t size,
256 custom_allocation_timer_handler* handler)
257{
258 ++(*handler->count_);
259 return ::operator new(size);
260}
261
262void asio_handler_deallocate(void* pointer, std::size_t,
263 custom_allocation_timer_handler* handler)
264{
265 --(*handler->count_);
266 ::operator delete(pointer);
267}
268
269void deadline_timer_custom_allocation_test()
270{
b32b8144 271 static boost::asio::io_context io_context;
7c673cae
FG
272 struct timer
273 {
274 boost::asio::deadline_timer t;
b32b8144 275 timer() : t(io_context) {}
7c673cae
FG
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
b32b8144 295 io_context.run();
7c673cae
FG
296
297 BOOST_ASIO_CHECK(allocation_count == 0);
298}
299
b32b8144 300void io_context_run(boost::asio::io_context* ioc)
7c673cae 301{
b32b8144 302 ioc->run();
7c673cae
FG
303}
304
305void deadline_timer_thread_test()
306{
b32b8144
FG
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);
7c673cae
FG
312 int count = 0;
313
b32b8144 314 boost::asio::detail::thread th(boost::bind(io_context_run, &ioc));
7c673cae
FG
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
b32b8144 325 ioc.stop();
7c673cae
FG
326 th.join();
327
328 BOOST_ASIO_CHECK(count == 1);
329}
330
331void deadline_timer_async_result_test()
332{
b32b8144
FG
333 boost::asio::io_context ioc;
334 boost::asio::deadline_timer t1(ioc);
7c673cae
FG
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
b32b8144
FG
340 ioc.run();
341}
342
343#if defined(BOOST_ASIO_HAS_MOVE)
344boost::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
353void 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)
7c673cae
FG
374}
375
376BOOST_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)
b32b8144 384 BOOST_ASIO_TEST_CASE(deadline_timer_move_test)
7c673cae
FG
385)
386#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
387BOOST_ASIO_TEST_SUITE
388(
389 "deadline_timer",
390 BOOST_ASIO_TEST_CASE(null_test)
391)
392#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)