fc925d6cd70e58af73f799e5413fa605ec8d9710
[folly.git] / folly / synchronization / test / BatonTest.cpp
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/synchronization/Baton.h>
18
19 #include <thread>
20
21 #include <folly/portability/GTest.h>
22 #include <folly/synchronization/test/BatonTestHelpers.h>
23 #include <folly/test/DeterministicSchedule.h>
24
25 using namespace folly;
26 using namespace folly::test;
27 using folly::detail::EmulatedFutexAtomic;
28
29 /// Basic test
30
31 TEST(Baton, basic_blocking) {
32   run_basic_test<true, std::atomic>();
33   run_basic_test<true, EmulatedFutexAtomic>();
34   run_basic_test<true, DeterministicAtomic>();
35 }
36
37 TEST(Baton, basic_nonblocking) {
38   run_basic_test<false, std::atomic>();
39   run_basic_test<false, EmulatedFutexAtomic>();
40   run_basic_test<false, DeterministicAtomic>();
41 }
42
43 /// Ping pong tests
44
45 TEST(Baton, pingpong_blocking) {
46   DSched sched(DSched::uniform(0));
47
48   run_pingpong_test<true, DeterministicAtomic>(1000);
49 }
50
51 TEST(Baton, pingpong_nonblocking) {
52   DSched sched(DSched::uniform(0));
53
54   run_pingpong_test<false, DeterministicAtomic>(1000);
55 }
56
57 /// Timed wait tests - Nonblocking Baton does not support try_wait_until()
58
59 // Timed wait basic system clock tests
60
61 TEST(Baton, timed_wait_basic_system_clock) {
62   run_basic_timed_wait_tests<std::atomic, std::chrono::system_clock>();
63   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
64   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::system_clock>();
65 }
66
67 // Timed wait timeout system clock tests
68
69 TEST(Baton, timed_wait_timeout_system_clock) {
70   run_timed_wait_tmo_tests<std::atomic, std::chrono::system_clock>();
71   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
72   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::system_clock>();
73 }
74
75 // Timed wait regular system clock tests
76
77 TEST(Baton, timed_wait_system_clock) {
78   run_timed_wait_regular_test<std::atomic, std::chrono::system_clock>();
79   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::system_clock>();
80   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::system_clock>();
81 }
82
83 // Timed wait basic steady clock tests
84
85 TEST(Baton, timed_wait_basic_steady_clock) {
86   run_basic_timed_wait_tests<std::atomic, std::chrono::steady_clock>();
87   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
88   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::steady_clock>();
89 }
90
91 // Timed wait timeout steady clock tests
92
93 TEST(Baton, timed_wait_timeout_steady_clock) {
94   run_timed_wait_tmo_tests<std::atomic, std::chrono::steady_clock>();
95   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
96   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::steady_clock>();
97 }
98
99 // Timed wait regular steady clock tests
100
101 TEST(Baton, timed_wait_steady_clock) {
102   run_timed_wait_regular_test<std::atomic, std::chrono::steady_clock>();
103   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::steady_clock>();
104   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::steady_clock>();
105 }
106
107 /// Try wait tests
108
109 TEST(Baton, try_wait_blocking) {
110   run_try_wait_tests<true, std::atomic>();
111   run_try_wait_tests<true, EmulatedFutexAtomic>();
112   run_try_wait_tests<true, DeterministicAtomic>();
113 }
114
115 TEST(Baton, try_wait_nonblocking) {
116   run_try_wait_tests<false, std::atomic>();
117   run_try_wait_tests<false, EmulatedFutexAtomic>();
118   run_try_wait_tests<false, DeterministicAtomic>();
119 }