Use the GTest portability headers
[folly.git] / folly / test / BatonTest.cpp
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 #include <folly/Baton.h>
18 #include <folly/test/BatonTestHelpers.h>
19 #include <folly/test/DeterministicSchedule.h>
20 #include <folly/portability/GTest.h>
21
22 #include <thread>
23 #include <semaphore.h>
24
25 using namespace folly;
26 using namespace folly::test;
27 using folly::detail::EmulatedFutexAtomic;
28
29 TEST(Baton, basic) {
30   Baton<> b;
31   b.post();
32   b.wait();
33 }
34
35 TEST(Baton, pingpong) {
36   DSched sched(DSched::uniform(0));
37
38   run_pingpong_test<DeterministicAtomic>(1000);
39 }
40
41 TEST(Baton, timed_wait_basic_system_clock) {
42   run_basic_timed_wait_tests<std::atomic, std::chrono::system_clock>();
43   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
44   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::system_clock>();
45 }
46
47 TEST(Baton, timed_wait_timeout_system_clock) {
48   run_timed_wait_tmo_tests<std::atomic, std::chrono::system_clock>();
49   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
50   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::system_clock>();
51 }
52
53 TEST(Baton, timed_wait_system_clock) {
54   run_timed_wait_regular_test<std::atomic, std::chrono::system_clock>();
55   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::system_clock>();
56   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::system_clock>();
57 }
58
59 TEST(Baton, timed_wait_basic_steady_clock) {
60   run_basic_timed_wait_tests<std::atomic, std::chrono::steady_clock>();
61   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
62   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::steady_clock>();
63 }
64
65 TEST(Baton, timed_wait_timeout_steady_clock) {
66   run_timed_wait_tmo_tests<std::atomic, std::chrono::steady_clock>();
67   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
68   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::steady_clock>();
69 }
70
71 TEST(Baton, timed_wait_steady_clock) {
72   run_timed_wait_regular_test<std::atomic, std::chrono::steady_clock>();
73   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::steady_clock>();
74   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::steady_clock>();
75 }
76
77 TEST(Baton, try_wait) {
78   run_try_wait_tests<std::atomic>();
79   run_try_wait_tests<EmulatedFutexAtomic>();
80   run_try_wait_tests<DeterministicAtomic>();
81 }