fix exceptionStr to work for derived classes of std::exception
[folly.git] / folly / test / BatonTest.cpp
1 /*
2  * Copyright 2014 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/Baton.h>
18 #include <folly/test/DeterministicSchedule.h>
19 #include <thread>
20 #include <semaphore.h>
21 #include <gflags/gflags.h>
22 #include <gtest/gtest.h>
23 #include <folly/Benchmark.h>
24
25 using namespace folly;
26 using namespace folly::test;
27
28 typedef DeterministicSchedule DSched;
29
30 TEST(Baton, basic) {
31   Baton<> b;
32   b.post();
33   b.wait();
34 }
35
36 template <template<typename> class Atom>
37 void run_pingpong_test(int numRounds) {
38   Baton<Atom> batons[17];
39   Baton<Atom>& a = batons[0];
40   Baton<Atom>& b = batons[16]; // to get it on a different cache line
41   auto thr = DSched::thread([&]{
42     for (int i = 0; i < numRounds; ++i) {
43       a.wait();
44       a.reset();
45       b.post();
46     }
47   });
48   for (int i = 0; i < numRounds; ++i) {
49     a.post();
50     b.wait();
51     b.reset();
52   }
53   DSched::join(thr);
54 }
55
56 TEST(Baton, pingpong) {
57   DSched sched(DSched::uniform(0));
58
59   run_pingpong_test<DeterministicAtomic>(1000);
60 }
61
62 BENCHMARK(baton_pingpong, iters) {
63   run_pingpong_test<std::atomic>(iters);
64 }
65
66 BENCHMARK(posix_sem_pingpong, iters) {
67   sem_t sems[3];
68   sem_t* a = sems + 0;
69   sem_t* b = sems + 2; // to get it on a different cache line
70
71   sem_init(a, 0, 0);
72   sem_init(b, 0, 0);
73   auto thr = std::thread([=]{
74     for (int i = 0; i < iters; ++i) {
75       sem_wait(a);
76       sem_post(b);
77     }
78   });
79   for (int i = 0; i < iters; ++i) {
80     sem_post(a);
81     sem_wait(b);
82   }
83   thr.join();
84 }
85
86 template <template<typename> class Atom>
87 void run_basic_timed_wait_tests() {
88   Baton<Atom> b;
89   b.post();
90   // tests if early delivery works fine
91   EXPECT_TRUE(b.timed_wait(std::chrono::system_clock::now()));
92 }
93
94 template <template<typename> class Atom>
95 void run_timed_wait_tmo_tests() {
96   Baton<Atom> b;
97
98   auto thr = DSched::thread([&]{
99     bool rv = b.timed_wait(std::chrono::system_clock::now() +
100                            std::chrono::milliseconds(1));
101     // main thread is guaranteed to not post until timeout occurs
102     EXPECT_FALSE(rv);
103   });
104   DSched::join(thr);
105 }
106
107 template <template<typename> class Atom>
108 void run_timed_wait_regular_test() {
109   Baton<Atom> b;
110
111   auto thr = DSched::thread([&] {
112     bool rv = b.timed_wait(
113                 std::chrono::time_point<std::chrono::system_clock>::max());
114     if (std::is_same<Atom<int>, std::atomic<int>>::value) {
115       // We can only ensure this for std::atomic
116       EXPECT_TRUE(rv);
117     }
118   });
119
120   if (std::is_same<Atom<int>, std::atomic<int>>::value) {
121     // If we are using std::atomic, then a sleep here guarantees to a large
122     // extent that 'thr' will execute wait before we post it, thus testing
123     // late delivery. For DeterministicAtomic, we just rely on
124     // DeterministicSchedule to do the scheduling
125     std::this_thread::sleep_for(std::chrono::milliseconds(2));
126   }
127
128   b.post();
129   DSched::join(thr);
130 }
131
132 TEST(Baton, timed_wait_basic) {
133   run_basic_timed_wait_tests<std::atomic>();
134   run_basic_timed_wait_tests<DeterministicAtomic>();
135 }
136
137 TEST(Baton, timed_wait_timeout) {
138   run_timed_wait_tmo_tests<std::atomic>();
139   run_timed_wait_tmo_tests<DeterministicAtomic>();
140 }
141
142 TEST(Baton, timed_wait) {
143   run_timed_wait_regular_test<std::atomic>();
144   run_timed_wait_regular_test<DeterministicAtomic>();
145 }
146
147 template <template<typename> class Atom>
148 void run_try_wait_tests() {
149   Baton<Atom> b;
150   EXPECT_FALSE(b.try_wait());
151   b.post();
152   EXPECT_TRUE(b.try_wait());
153 }
154
155 TEST(Baton, try_wait) {
156   run_try_wait_tests<std::atomic>();
157   run_try_wait_tests<DeterministicAtomic>();
158 }
159
160 // I am omitting a benchmark result snapshot because these microbenchmarks
161 // mainly illustrate that PreBlockAttempts is very effective for rapid
162 // handoffs.  The performance of Baton and sem_t is essentially identical
163 // to the required futex calls for the blocking case
164
165 int main(int argc, char** argv) {
166   testing::InitGoogleTest(&argc, argv);
167   gflags::ParseCommandLineFlags(&argc, &argv, true);
168
169   auto rv = RUN_ALL_TESTS();
170   if (!rv && FLAGS_benchmark) {
171     folly::runBenchmarks();
172   }
173   return rv;
174 }