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 <folly/experimental/FutureDAG.h>
17 #include <gtest/gtest.h>
18 #include <boost/thread/barrier.hpp>
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));
32 void dependency(Handle a, Handle b) {
33 nodes.at(b)->dependencies.push_back(a);
34 dag->dependency(a, b);
38 EXPECT_EQ(nodes.size(), order.size());
39 for (auto& kv : nodes) {
40 auto handle = kv.first;
41 auto& node = kv.second;
42 auto it = order.begin();
43 while (*it != handle) {
46 for (auto dep : node->dependencies) {
47 EXPECT_TRUE(std::find(it, order.end(), dep) == order.end());
53 explicit TestNode(FutureDAGTest* test) {
55 test->order.push_back(handle);
56 return Future<Unit>();
58 handle = test->dag->add(func);
61 FutureDAG::FutureFunc func;
63 std::vector<Handle> dependencies;
66 std::shared_ptr<FutureDAG> dag = FutureDAG::create();
67 std::map<Handle, std::unique_ptr<TestNode>> nodes;
68 std::vector<Handle> order;
72 TEST_F(FutureDAGTest, SingleNode) {
74 ASSERT_NO_THROW(dag->go().get());
78 TEST_F(FutureDAGTest, FanOut) {
84 ASSERT_NO_THROW(dag->go().get());
88 TEST_F(FutureDAGTest, FanIn) {
94 ASSERT_NO_THROW(dag->go().get());
98 TEST_F(FutureDAGTest, FanOutFanIn) {
107 ASSERT_NO_THROW(dag->go().get());
111 TEST_F(FutureDAGTest, Complex) {
144 ASSERT_NO_THROW(dag->go().get());
148 FutureDAG::FutureFunc makeFutureFunc = []{
152 FutureDAG::FutureFunc throwFunc = []{
153 return makeFuture<Unit>(std::runtime_error("oops"));
156 TEST_F(FutureDAGTest, ThrowBegin) {
157 auto h1 = dag->add(throwFunc);
158 auto h2 = dag->add(makeFutureFunc);
159 dag->dependency(h1, h2);
160 EXPECT_THROW(dag->go().get(), std::runtime_error);
163 TEST_F(FutureDAGTest, ThrowEnd) {
164 auto h1 = dag->add(makeFutureFunc);
165 auto h2 = dag->add(throwFunc);
166 dag->dependency(h1, h2);
167 EXPECT_THROW(dag->go().get(), std::runtime_error);
170 TEST_F(FutureDAGTest, Cycle1) {
173 EXPECT_THROW(dag->go().get(), std::runtime_error);
176 TEST_F(FutureDAGTest, Cycle2) {
181 EXPECT_THROW(dag->go().get(), std::runtime_error);
184 TEST_F(FutureDAGTest, Cycle3) {
191 EXPECT_THROW(dag->go().get(), std::runtime_error);
194 TEST_F(FutureDAGTest, DestroyBeforeComplete) {
195 auto barrier = std::make_shared<boost::barrier>(2);
198 auto dag = FutureDAG::create();
199 auto h1 = dag->add([barrier] {
200 auto p = std::make_shared<Promise<Unit>>();
201 std::thread t([p, barrier]{
206 return p->getFuture();
208 auto h2 = dag->add(makeFutureFunc);
209 dag->dependency(h1, h2);
213 ASSERT_NO_THROW(f.get());