2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/futures/Barrier.h>
20 #include <condition_variable>
23 #include <folly/Random.h>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
27 namespace folly { namespace futures { namespace test {
29 TEST(BarrierTest, Simple) {
30 constexpr uint32_t numThreads = 10;
33 std::condition_variable b1DoneCond;
34 std::condition_variable b2DoneCond;
35 std::atomic<uint32_t> b1TrueSeen(0);
36 std::atomic<uint32_t> b1Passed(0);
37 std::atomic<uint32_t> b2TrueSeen(0);
38 std::atomic<uint32_t> b2Passed(0);
40 Barrier barrier(numThreads + 1);
42 std::vector<std::thread> threads;
43 threads.reserve(numThreads);
44 for (uint32_t i = 0; i < numThreads; ++i) {
45 threads.emplace_back([&] () {
49 std::unique_lock<std::mutex> lock(mutex);
50 b1TrueSeen += uint32_t(v);
51 if (++b1Passed == numThreads) {
52 b1DoneCond.notify_one();
54 return barrier.wait();
58 std::unique_lock<std::mutex> lock(mutex);
59 b2TrueSeen += uint32_t(v);
60 if (++b2Passed == numThreads) {
61 b2DoneCond.notify_one();
69 std::this_thread::sleep_for(std::chrono::milliseconds(50));
70 EXPECT_EQ(0, b1Passed);
71 EXPECT_EQ(0, b1TrueSeen);
73 b1TrueSeen += barrier.wait().get();
76 std::unique_lock<std::mutex> lock(mutex);
77 while (b1Passed != numThreads) {
78 b1DoneCond.wait(lock);
80 EXPECT_EQ(1, b1TrueSeen);
84 std::this_thread::sleep_for(std::chrono::milliseconds(50));
85 EXPECT_EQ(0, b2Passed);
86 EXPECT_EQ(0, b2TrueSeen);
88 b2TrueSeen += barrier.wait().get();
91 std::unique_lock<std::mutex> lock(mutex);
92 while (b2Passed != numThreads) {
93 b2DoneCond.wait(lock);
95 EXPECT_EQ(1, b2TrueSeen);
98 for (auto& t : threads) {
103 TEST(BarrierTest, Random) {
104 // Create numThreads threads.
106 // Each thread repeats the following numIterations times:
107 // - grab a randomly chosen number of futures from the barrier, waiting
108 // for a short random time between each
109 // - wait for all futures to complete
110 // - record whether the one future returning true was seen among them
112 // At the end, we verify that exactly one future returning true was seen
113 // for each iteration.
114 constexpr uint32_t numIterations = 1;
115 auto numThreads = folly::Random::rand32(30, 91);
120 uint32_t iteration = 0;
122 std::vector<uint32_t> trueSeen;
125 std::vector<ThreadInfo> threads;
126 threads.resize(numThreads);
128 uint32_t totalFutures = 0;
129 for (auto& tinfo : threads) {
130 tinfo.numFutures = folly::Random::rand32(100);
131 tinfo.trueSeen.resize(numIterations);
132 totalFutures += tinfo.numFutures;
135 Barrier barrier(totalFutures);
137 for (auto& tinfo : threads) {
139 tinfo.thread = std::thread(
140 [numIterations, pinfo, &barrier] () {
141 std::vector<folly::Future<bool>> futures;
142 futures.reserve(pinfo->numFutures);
143 for (uint32_t i = 0; i < numIterations; ++i, ++pinfo->iteration) {
145 for (uint32_t j = 0; j < pinfo->numFutures; ++j) {
146 futures.push_back(barrier.wait());
147 auto nanos = folly::Random::rand32(10 * 1000 * 1000);
149 std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
151 auto results = folly::collect(futures).get();
153 std::count(results.begin(), results.end(), true);
158 for (auto& tinfo : threads) {
160 EXPECT_EQ(numIterations, tinfo.iteration);
163 for (uint32_t i = 0; i < numIterations; ++i) {
164 uint32_t trueCount = 0;
165 for (auto& tinfo : threads) {
166 trueCount += tinfo.trueSeen[i];
168 EXPECT_EQ(1, trueCount);