]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/test_condition_timed_wait_times_out.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / test / test_condition_timed_wait_times_out.cpp
1 // Copyright (C) 2007-8 Anthony Williams
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #define BOOST_THREAD_VERSION 2
7
8 #define BOOST_TEST_MODULE Boost.Threads: condition_variable test suite
9
10 #include <boost/thread/detail/config.hpp>
11
12 #include <boost/thread/condition.hpp>
13 #include <boost/thread/thread_only.hpp>
14
15 #include <boost/test/unit_test.hpp>
16 #include "./util.inl"
17
18 bool fake_predicate()
19 {
20 return false;
21 }
22
23 unsigned const timeout_seconds=2;
24 unsigned const timeout_grace=1;
25 boost::posix_time::milliseconds const timeout_resolution(100);
26
27
28 void do_test_timed_wait_times_out()
29 {
30 boost::condition_variable cond;
31 boost::mutex m;
32
33 boost::posix_time::seconds const delay(timeout_seconds);
34 boost::unique_lock<boost::mutex> lock(m);
35 boost::system_time const start=boost::get_system_time();
36 boost::system_time const timeout=start+delay;
37
38 while(cond.timed_wait(lock,timeout)) {}
39
40 boost::system_time const end=boost::get_system_time();
41 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
42 }
43
44 void do_test_timed_wait_with_predicate_times_out()
45 {
46 boost::condition_variable cond;
47 boost::mutex m;
48
49 boost::posix_time::seconds const delay(timeout_seconds);
50 boost::unique_lock<boost::mutex> lock(m);
51 boost::system_time const start=boost::get_system_time();
52 boost::system_time const timeout=start+delay;
53
54 bool const res=cond.timed_wait(lock,timeout,fake_predicate);
55
56 boost::system_time const end=boost::get_system_time();
57 BOOST_CHECK(!res);
58 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
59 }
60
61 void do_test_relative_timed_wait_with_predicate_times_out()
62 {
63 boost::condition_variable cond;
64 boost::mutex m;
65
66 boost::posix_time::seconds const delay(timeout_seconds);
67 boost::unique_lock<boost::mutex> lock(m);
68 boost::system_time const start=boost::get_system_time();
69
70 bool const res=cond.timed_wait(lock,delay,fake_predicate);
71
72 boost::system_time const end=boost::get_system_time();
73 BOOST_CHECK(!res);
74 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
75 }
76
77 void do_test_timed_wait_relative_times_out()
78 {
79 boost::condition_variable cond;
80 boost::mutex m;
81
82 boost::posix_time::seconds const delay(timeout_seconds);
83 boost::unique_lock<boost::mutex> lock(m);
84 boost::system_time const start=boost::get_system_time();
85
86 while(cond.timed_wait(lock,delay)) {}
87
88 boost::system_time const end=boost::get_system_time();
89 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
90 }
91
92 void do_test_cv_any_timed_wait_times_out()
93 {
94 boost::condition_variable_any cond;
95 boost::mutex m;
96
97 boost::posix_time::seconds const delay(timeout_seconds);
98 boost::unique_lock<boost::mutex> lock(m);
99 boost::system_time const start=boost::get_system_time();
100 boost::system_time const timeout=start+delay;
101
102 while(cond.timed_wait(lock,timeout)) {}
103
104 boost::system_time const end=boost::get_system_time();
105 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
106 }
107
108 void do_test_cv_any_timed_wait_with_predicate_times_out()
109 {
110 boost::condition_variable_any cond;
111 boost::mutex m;
112
113 boost::posix_time::seconds const delay(timeout_seconds);
114 boost::unique_lock<boost::mutex> lock(m);
115 boost::system_time const start=boost::get_system_time();
116 boost::system_time const timeout=start+delay;
117
118 bool const res=cond.timed_wait(lock,timeout,fake_predicate);
119
120 boost::system_time const end=boost::get_system_time();
121 BOOST_CHECK(!res);
122 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
123 }
124
125 void do_test_cv_any_relative_timed_wait_with_predicate_times_out()
126 {
127 boost::condition_variable_any cond;
128 boost::mutex m;
129
130 boost::posix_time::seconds const delay(timeout_seconds);
131 boost::unique_lock<boost::mutex> lock(m);
132 boost::system_time const start=boost::get_system_time();
133
134 bool const res=cond.timed_wait(lock,delay,fake_predicate);
135
136 boost::system_time const end=boost::get_system_time();
137 BOOST_CHECK(!res);
138 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
139 }
140
141 void do_test_cv_any_timed_wait_relative_times_out()
142 {
143 boost::condition_variable_any cond;
144 boost::mutex m;
145
146 boost::posix_time::seconds const delay(timeout_seconds);
147 boost::unique_lock<boost::mutex> lock(m);
148 boost::system_time const start=boost::get_system_time();
149
150 while(cond.timed_wait(lock,delay)) {}
151
152 boost::system_time const end=boost::get_system_time();
153 BOOST_CHECK((delay-timeout_resolution)<=(end-start));
154 }
155
156
157 BOOST_AUTO_TEST_CASE(test_timed_wait_times_out)
158 {
159 timed_test(&do_test_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
160 timed_test(&do_test_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
161 timed_test(&do_test_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
162 timed_test(&do_test_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
163 timed_test(&do_test_cv_any_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
164 timed_test(&do_test_cv_any_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
165 timed_test(&do_test_cv_any_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
166 timed_test(&do_test_cv_any_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
167 }
168
169