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