2 * Copyright 2015 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 <gtest/gtest.h>
19 #include <folly/futures/Future.h>
20 #include <folly/futures/InlineExecutor.h>
21 #include <folly/futures/ManualExecutor.h>
22 #include <folly/futures/DrivableExecutor.h>
23 #include <folly/Baton.h>
24 #include <folly/MPMCQueue.h>
28 using namespace folly;
30 struct ManualWaiter : public DrivableExecutor {
31 explicit ManualWaiter(std::shared_ptr<ManualExecutor> ex) : ex(ex) {}
33 void add(Func f) override {
37 void drive() override {
42 std::shared_ptr<ManualExecutor> ex;
45 struct ViaFixture : public testing::Test {
47 westExecutor(new ManualExecutor),
48 eastExecutor(new ManualExecutor),
49 waiter(new ManualWaiter(westExecutor)),
53 ManualWaiter eastWaiter(eastExecutor);
59 ~ViaFixture() override {
61 eastExecutor->add([=]() { });
65 void addAsync(int a, int b, std::function<void(int&&)>&& cob) {
66 eastExecutor->add([=]() {
71 std::shared_ptr<ManualExecutor> westExecutor;
72 std::shared_ptr<ManualExecutor> eastExecutor;
73 std::shared_ptr<ManualWaiter> waiter;
74 InlineExecutor inlineExecutor;
75 std::atomic<bool> done;
79 TEST(Via, exceptionOnLaunch) {
80 auto future = makeFuture<int>(std::runtime_error("E"));
81 EXPECT_THROW(future.value(), std::runtime_error);
84 TEST(Via, thenValue) {
85 auto future = makeFuture(std::move(1))
86 .then([](Try<int>&& t) {
87 return t.value() == 1;
91 EXPECT_TRUE(future.value());
94 TEST(Via, thenFuture) {
95 auto future = makeFuture(1)
96 .then([](Try<int>&& t) {
97 return makeFuture(t.value() == 1);
99 EXPECT_TRUE(future.value());
102 static Future<std::string> doWorkStatic(Try<std::string>&& t) {
103 return makeFuture(t.value() + ";static");
106 TEST(Via, thenFunction) {
108 Future<std::string> doWork(Try<std::string>&& t) {
109 return makeFuture(t.value() + ";class");
111 static Future<std::string> doWorkStatic(Try<std::string>&& t) {
112 return makeFuture(t.value() + ";class-static");
116 auto f = makeFuture(std::string("start"))
118 .then(Worker::doWorkStatic)
119 .then(&Worker::doWork, &w)
122 EXPECT_EQ(f.value(), "start;static;class-static;class");
125 TEST_F(ViaFixture, threadHops) {
126 auto westThreadId = std::this_thread::get_id();
127 auto f = via(eastExecutor.get()).then([=](Try<void>&& t) {
128 EXPECT_NE(std::this_thread::get_id(), westThreadId);
129 return makeFuture<int>(1);
130 }).via(westExecutor.get()
131 ).then([=](Try<int>&& t) {
132 EXPECT_EQ(std::this_thread::get_id(), westThreadId);
135 EXPECT_EQ(f.getVia(waiter.get()), 1);
138 TEST_F(ViaFixture, chainVias) {
139 auto westThreadId = std::this_thread::get_id();
140 auto f = via(eastExecutor.get()).then([=]() {
141 EXPECT_NE(std::this_thread::get_id(), westThreadId);
143 }).then([=](int val) {
144 return makeFuture(val).via(westExecutor.get())
145 .then([=](int val) mutable {
146 EXPECT_EQ(std::this_thread::get_id(), westThreadId);
149 }).then([=](int val) {
150 // even though ultimately the future that triggers this one executed in
151 // the west thread, this then() inherited the executor from its
152 // predecessor, ie the eastExecutor.
153 EXPECT_NE(std::this_thread::get_id(), westThreadId);
155 }).via(westExecutor.get()).then([=](int val) {
156 // go back to west, so we can wait on it
157 EXPECT_EQ(std::this_thread::get_id(), westThreadId);
161 EXPECT_EQ(f.getVia(waiter.get()), 4);
164 TEST_F(ViaFixture, bareViaAssignment) {
165 auto f = via(eastExecutor.get());
167 TEST_F(ViaFixture, viaAssignment) {
169 auto f = makeFuture().via(eastExecutor.get());
171 auto f2 = f.via(eastExecutor.get());
177 .thenMulti([] { return 42; })
183 auto f = makeFuture().thenMulti(
184 [&]{ count++; return 3.14159; },
185 [&](double) { count++; return std::string("hello"); },
186 [&]{ count++; return makeFuture(42); });
187 EXPECT_EQ(42, f.get());
191 struct PriorityExecutor : public Executor {
192 void add(Func f) override {}
194 void addWithPriority(Func f, int8_t priority) override {
195 int mid = getNumPriorities() / 2;
196 int p = priority < 0 ?
197 std::max(0, mid + priority) :
198 std::min(getNumPriorities() - 1, mid + priority);
211 uint8_t getNumPriorities() const override {
220 TEST(Via, priority) {
221 PriorityExecutor exe;
222 via(&exe, -1).then([]{});
223 via(&exe, 0).then([]{});
224 via(&exe, 1).then([]{});
225 via(&exe, 42).then([]{}); // overflow should go to max priority
226 via(&exe, -42).then([]{}); // underflow should go to min priority
227 via(&exe).then([]{}); // default to mid priority
228 via(&exe, Executor::LO_PRI).then([]{});
229 via(&exe, Executor::HI_PRI).then([]{});
230 EXPECT_EQ(3, exe.count0);
231 EXPECT_EQ(2, exe.count1);
232 EXPECT_EQ(3, exe.count2);
235 TEST_F(ViaFixture, chainX1) {
238 .thenMultiWithExecutor(eastExecutor.get(),[] { return 42; })
242 TEST_F(ViaFixture, chainX3) {
243 auto westThreadId = std::this_thread::get_id();
245 auto f = via(westExecutor.get()).thenMultiWithExecutor(
248 EXPECT_NE(std::this_thread::get_id(), westThreadId);
249 count++; return 3.14159;
251 [&](double) { count++; return std::string("hello"); },
254 EXPECT_EQ(std::this_thread::get_id(), westThreadId);
255 return makeFuture(42);
257 EXPECT_EQ(42, f.getVia(waiter.get()));
262 ManualExecutor x1, x2;
263 bool a = false, b = false, c = false;
265 .then([&]{ a = true; })
266 .then(&x2, [&]{ b = true; })
267 .then([&]{ c = true; });
285 TEST(Via, then2Variadic) {
286 struct Foo { bool a = false; void foo(Try<void>) { a = true; } };
289 makeFuture().then(&x, &Foo::foo, &f);
295 #ifndef __APPLE__ // TODO #7372389
296 /// Simple executor that does work in another thread
297 class ThreadExecutor : public Executor {
298 folly::MPMCQueue<Func> funcs;
299 std::atomic<bool> done {false};
301 folly::Baton<> baton;
307 while (!funcs.isEmpty()) {
308 funcs.blockingRead(fn);
315 explicit ThreadExecutor(size_t n = 1024)
317 worker = std::thread(std::bind(&ThreadExecutor::work, this));
320 ~ThreadExecutor() override {
326 void add(Func fn) override {
327 funcs.blockingWrite(std::move(fn));
330 void waitForStartup() {
335 TEST(Via, viaThenGetWasRacy) {
337 std::unique_ptr<int> val = folly::via(&x)
338 .then([] { return folly::make_unique<int>(42); })
344 TEST(Via, callbackRace) {
348 auto promises = std::make_shared<std::vector<Promise<void>>>(4);
349 std::vector<Future<void>> futures;
351 for (auto& p : *promises) {
352 futures.emplace_back(
355 .then([](Try<void>&&){}));
360 for (auto& p : *promises) {
365 return collectAll(futures);
372 class DummyDrivableExecutor : public DrivableExecutor {
374 void add(Func f) override {}
375 void drive() override { ran = true; }
383 auto f = via(&x).then([]{ return true; });
384 EXPECT_TRUE(f.getVia(&x));
390 auto f = via(&x).then();
395 DummyDrivableExecutor x;
396 auto f = makeFuture(true);
397 EXPECT_TRUE(f.getVia(&x));
405 auto f = via(&x).then();
406 EXPECT_FALSE(f.isReady());
408 EXPECT_TRUE(f.isReady());
412 // try rvalue as well
414 auto f = via(&x).then().waitVia(&x);
415 EXPECT_TRUE(f.isReady());
419 DummyDrivableExecutor x;
420 makeFuture(true).waitVia(&x);
425 TEST(Via, viaRaces) {
428 auto tid = std::this_thread::get_id();
434 .then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
435 .then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
436 .then([&](Try<void>&&) { done = true; });
443 while (!done) x.run();
448 TEST(ViaFunc, liftsVoid) {
451 Future<void> f = via(&x, [&]{ count++; });
458 TEST(ViaFunc, value) {
460 EXPECT_EQ(42, via(&x, []{ return 42; }).getVia(&x));
463 TEST(ViaFunc, exception) {
466 via(&x, []() -> int { throw std::runtime_error("expected"); })
471 TEST(ViaFunc, future) {
473 EXPECT_EQ(42, via(&x, []{ return makeFuture(42); })
477 TEST(ViaFunc, voidFuture) {
480 via(&x, [&]{ count++; }).getVia(&x);
484 TEST(ViaFunc, isSticky) {
488 auto f = via(&x, [&]{ count++; });
491 f.then([&]{ count++; });