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.
16 #include <boost/thread/barrier.hpp>
17 #include <folly/experimental/FutureDAG.h>
18 #include <gtest/gtest.h>
20 using namespace folly;
22 struct FutureDAGTest : public testing::Test {
23 typedef FutureDAG::Handle Handle;
26 auto node = folly::make_unique<TestNode>(this);
27 auto handle = node->handle;
28 nodes.emplace(handle, std::move(node));
34 std::unordered_set<Handle> memo;
35 for (auto& node : nodes) {
36 for (Handle handle : node.second->dependencies) {
40 for (auto& node : nodes) {
41 if (memo.find(node.first) == memo.end()) {
42 source_node = node.first;
45 for (auto it = nodes.cbegin(); it != nodes.cend();) {
46 if (it->first != source_node) {
55 void remove(Handle a) {
56 for (auto itr = nodes.begin(); itr != nodes.end(); itr++) {
57 auto& deps = itr->second->dependencies;
58 if (std::find(deps.begin(), deps.end(), a) != deps.end()) {
59 deps.erase(deps.begin() + a);
65 void dependency(Handle a, Handle b) {
66 nodes.at(b)->dependencies.push_back(a);
67 dag->dependency(a, b);
71 EXPECT_EQ(nodes.size(), order.size());
72 for (auto& kv : nodes) {
73 auto handle = kv.first;
74 auto& node = kv.second;
75 auto it = order.begin();
76 while (*it != handle) {
79 for (auto dep : node->dependencies) {
80 EXPECT_TRUE(std::find(it, order.end(), dep) == order.end());
86 explicit TestNode(FutureDAGTest* test) {
88 test->order.push_back(handle);
89 return Future<Unit>();
91 handle = test->dag->add(func);
94 FutureDAG::FutureFunc func;
96 std::vector<Handle> dependencies;
99 std::shared_ptr<FutureDAG> dag = FutureDAG::create();
100 std::map<Handle, std::unique_ptr<TestNode>> nodes;
101 std::vector<Handle> order;
104 TEST_F(FutureDAGTest, SingleNode) {
106 ASSERT_NO_THROW(dag->go().get());
110 TEST_F(FutureDAGTest, RemoveSingleNode) {
114 ASSERT_NO_THROW(dag->go().get());
118 TEST_F(FutureDAGTest, RemoveNodeComplex) {
126 ASSERT_NO_THROW(dag->go().get());
130 TEST_F(FutureDAGTest, ResetDAG) {
138 ASSERT_NO_THROW(dag->go().get());
142 TEST_F(FutureDAGTest, FanOut) {
148 ASSERT_NO_THROW(dag->go().get());
152 TEST_F(FutureDAGTest, FanIn) {
158 ASSERT_NO_THROW(dag->go().get());
162 TEST_F(FutureDAGTest, FanOutFanIn) {
171 ASSERT_NO_THROW(dag->go().get());
175 TEST_F(FutureDAGTest, Complex) {
208 ASSERT_NO_THROW(dag->go().get());
212 FutureDAG::FutureFunc makeFutureFunc = [] { return makeFuture(); };
214 FutureDAG::FutureFunc throwFunc = [] {
215 return makeFuture<Unit>(std::runtime_error("oops"));
218 TEST_F(FutureDAGTest, ThrowBegin) {
219 auto h1 = dag->add(throwFunc);
220 auto h2 = dag->add(makeFutureFunc);
221 dag->dependency(h1, h2);
222 EXPECT_THROW(dag->go().get(), std::runtime_error);
225 TEST_F(FutureDAGTest, ThrowEnd) {
226 auto h1 = dag->add(makeFutureFunc);
227 auto h2 = dag->add(throwFunc);
228 dag->dependency(h1, h2);
229 EXPECT_THROW(dag->go().get(), std::runtime_error);
232 TEST_F(FutureDAGTest, Cycle1) {
235 EXPECT_THROW(dag->go().get(), std::runtime_error);
238 TEST_F(FutureDAGTest, Cycle2) {
243 EXPECT_THROW(dag->go().get(), std::runtime_error);
246 TEST_F(FutureDAGTest, Cycle3) {
253 EXPECT_THROW(dag->go().get(), std::runtime_error);
256 TEST_F(FutureDAGTest, DestroyBeforeComplete) {
257 auto barrier = std::make_shared<boost::barrier>(2);
260 auto dag = FutureDAG::create();
261 auto h1 = dag->add([barrier] {
262 auto p = std::make_shared<Promise<Unit>>();
263 std::thread t([p, barrier] {
268 return p->getFuture();
270 auto h2 = dag->add(makeFutureFunc);
271 dag->dependency(h1, h2);
275 ASSERT_NO_THROW(f.get());