1 // Copyright (C) 2007-8 Anthony Williams
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)
6 #define BOOST_THREAD_VERSION 2
8 #define BOOST_TEST_MODULE Boost.Threads: condition_variable test suite
10 #include <boost/thread/detail/config.hpp>
12 #include <boost/thread/condition.hpp>
13 #include <boost/thread/thread_only.hpp>
15 #include <boost/test/unit_test.hpp>
23 unsigned const timeout_seconds
=2;
24 unsigned const timeout_grace
=1;
25 boost::posix_time::milliseconds
const timeout_resolution(100);
28 void do_test_timed_wait_times_out()
30 boost::condition_variable cond
;
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
;
38 while(cond
.timed_wait(lock
,timeout
)) {}
40 boost::system_time
const end
=boost::get_system_time();
41 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
44 void do_test_timed_wait_with_predicate_times_out()
46 boost::condition_variable cond
;
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
;
54 bool const res
=cond
.timed_wait(lock
,timeout
,fake_predicate
);
56 boost::system_time
const end
=boost::get_system_time();
58 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
61 void do_test_relative_timed_wait_with_predicate_times_out()
63 boost::condition_variable cond
;
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();
70 bool const res
=cond
.timed_wait(lock
,delay
,fake_predicate
);
72 boost::system_time
const end
=boost::get_system_time();
74 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
77 void do_test_timed_wait_relative_times_out()
79 boost::condition_variable cond
;
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();
86 while(cond
.timed_wait(lock
,delay
)) {}
88 boost::system_time
const end
=boost::get_system_time();
89 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
92 void do_test_cv_any_timed_wait_times_out()
94 boost::condition_variable_any cond
;
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
;
102 while(cond
.timed_wait(lock
,timeout
)) {}
104 boost::system_time
const end
=boost::get_system_time();
105 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
108 void do_test_cv_any_timed_wait_with_predicate_times_out()
110 boost::condition_variable_any cond
;
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
;
118 bool const res
=cond
.timed_wait(lock
,timeout
,fake_predicate
);
120 boost::system_time
const end
=boost::get_system_time();
122 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
125 void do_test_cv_any_relative_timed_wait_with_predicate_times_out()
127 boost::condition_variable_any cond
;
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();
134 bool const res
=cond
.timed_wait(lock
,delay
,fake_predicate
);
136 boost::system_time
const end
=boost::get_system_time();
138 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
141 void do_test_cv_any_timed_wait_relative_times_out()
143 boost::condition_variable_any cond
;
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();
150 while(cond
.timed_wait(lock
,delay
)) {}
152 boost::system_time
const end
=boost::get_system_time();
153 BOOST_CHECK((delay
-timeout_resolution
)<=(end
-start
));
157 BOOST_AUTO_TEST_CASE(test_timed_wait_times_out
)
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
);