Use the GTest portability headers
[folly.git] / folly / test / BatonTestHelpers.h
1 /*
2  * Copyright 2016 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 #pragma once
18
19 #include <folly/Baton.h>
20 #include <folly/test/DeterministicSchedule.h>
21 #include <folly/portability/GTest.h>
22
23 namespace folly {
24 namespace test {
25
26 typedef DeterministicSchedule DSched;
27
28 template <template <typename> class Atom>
29 void run_pingpong_test(int numRounds) {
30   Baton<Atom> batons[17];
31   Baton<Atom>& a = batons[0];
32   Baton<Atom>& b = batons[16]; // to get it on a different cache line
33   auto thr = DSched::thread([&] {
34     for (int i = 0; i < numRounds; ++i) {
35       a.wait();
36       a.reset();
37       b.post();
38     }
39   });
40   for (int i = 0; i < numRounds; ++i) {
41     a.post();
42     b.wait();
43     b.reset();
44   }
45   DSched::join(thr);
46 }
47
48 template <template <typename> class Atom, typename Clock>
49 void run_basic_timed_wait_tests() {
50   Baton<Atom> b;
51   b.post();
52   // tests if early delivery works fine
53   EXPECT_TRUE(b.timed_wait(Clock::now()));
54 }
55
56 template <template <typename> class Atom, typename Clock>
57 void run_timed_wait_tmo_tests() {
58   Baton<Atom> b;
59
60   auto thr = DSched::thread([&] {
61     bool rv = b.timed_wait(Clock::now() + std::chrono::milliseconds(1));
62     // main thread is guaranteed to not post until timeout occurs
63     EXPECT_FALSE(rv);
64   });
65   DSched::join(thr);
66 }
67
68 template <template <typename> class Atom, typename Clock>
69 void run_timed_wait_regular_test() {
70   Baton<Atom> b;
71
72   auto thr = DSched::thread([&] {
73     // To wait forever we'd like to use time_point<Clock>::max, but
74     // std::condition_variable does math to convert the timeout to
75     // system_clock without handling overflow.
76     auto farFuture = Clock::now() + std::chrono::hours(1000);
77     bool rv = b.timed_wait(farFuture);
78     if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
79       // DeterministicAtomic ignores actual times, so doesn't guarantee
80       // a lack of timeout
81       EXPECT_TRUE(rv);
82     }
83   });
84
85   if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
86     // If we are using std::atomic (or EmulatedFutexAtomic) then
87     // a sleep here guarantees to a large extent that 'thr' will
88     // execute wait before we post it, thus testing late delivery. For
89     // DeterministicAtomic, we just rely on DeterministicSchedule to do
90     // the scheduling.  The test won't fail if we lose the race, we just
91     // don't get coverage.
92     std::this_thread::sleep_for(std::chrono::milliseconds(2));
93   }
94
95   b.post();
96   DSched::join(thr);
97 }
98
99 template <template <typename> class Atom>
100 void run_try_wait_tests() {
101   Baton<Atom> b;
102   EXPECT_FALSE(b.try_wait());
103   b.post();
104   EXPECT_TRUE(b.try_wait());
105 }
106 }
107 }