2 * Copyright 2014 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.
19 #include <folly/small_vector.h>
20 #include <gtest/gtest.h>
24 #include <type_traits>
26 #include <folly/Memory.h>
27 #include <folly/Executor.h>
28 #include <folly/wangle/futures/Future.h>
29 #include <folly/wangle/futures/ManualExecutor.h>
30 #include <folly/MPMCQueue.h>
32 #include <folly/io/async/Request.h>
34 using namespace folly;
35 using namespace folly::wangle;
38 using std::unique_ptr;
41 #define EXPECT_TYPE(x, T) \
42 EXPECT_TRUE((std::is_same<decltype(x), T>::value))
44 /// Simple executor that does work in another thread
45 class ThreadExecutor : public Executor {
46 folly::MPMCQueue<Func> funcs;
47 std::atomic<bool> done {false};
55 while (!funcs.isEmpty()) {
56 funcs.blockingRead(fn);
63 ThreadExecutor(size_t n = 1024)
64 : funcs(n), worker(std::bind(&ThreadExecutor::work, this)) {}
72 void add(Func fn) override {
73 funcs.blockingWrite(std::move(fn));
76 void waitForStartup() {
81 typedef WangleException eggs_t;
82 static eggs_t eggs("eggs");
99 Try<A> t_a(std::move(a));
103 EXPECT_EQ(5, t_a.value().x());
106 TEST(Future, special) {
107 EXPECT_FALSE(std::is_copy_constructible<Future<int>>::value);
108 EXPECT_FALSE(std::is_copy_assignable<Future<int>>::value);
109 EXPECT_TRUE(std::is_move_constructible<Future<int>>::value);
110 EXPECT_TRUE(std::is_move_assignable<Future<int>>::value);
113 TEST(Future, thenTry) {
116 makeFuture<int>(42).then([&](Try<int>&& t) {
118 EXPECT_EQ(42, t.value());
120 EXPECT_TRUE(flag); flag = false;
123 .then([](Try<int>&& t) { return t.value(); })
124 .then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
125 EXPECT_TRUE(flag); flag = false;
127 makeFuture().then([&](Try<void>&& t) { flag = true; t.value(); });
128 EXPECT_TRUE(flag); flag = false;
131 auto f = p.getFuture().then([&](Try<void>&& t) { flag = true; });
133 EXPECT_FALSE(f.isReady());
136 EXPECT_TRUE(f.isReady());
139 TEST(Future, thenValue) {
141 makeFuture<int>(42).then([&](int i){
145 EXPECT_TRUE(flag); flag = false;
148 .then([](int i){ return i; })
149 .then([&](int i) { flag = true; EXPECT_EQ(42, i); });
150 EXPECT_TRUE(flag); flag = false;
152 makeFuture().then([&]{
155 EXPECT_TRUE(flag); flag = false;
157 auto f = makeFuture<int>(eggs).then([&](int i){});
158 EXPECT_THROW(f.value(), eggs_t);
160 f = makeFuture<void>(eggs).then([&]{});
161 EXPECT_THROW(f.value(), eggs_t);
164 TEST(Future, thenValueFuture) {
167 .then([](int i){ return makeFuture<int>(std::move(i)); })
168 .then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
169 EXPECT_TRUE(flag); flag = false;
172 .then([]{ return makeFuture(); })
173 .then([&](Try<void>&& t) { flag = true; });
174 EXPECT_TRUE(flag); flag = false;
177 static string doWorkStatic(Try<string>&& t) {
178 return t.value() + ";static";
181 TEST(Future, thenFunction) {
183 string doWork(Try<string>&& t) {
184 return t.value() + ";class";
186 static string doWorkStatic(Try<string>&& t) {
187 return t.value() + ";class-static";
191 auto f = makeFuture<string>("start")
193 .then(Worker::doWorkStatic)
194 .then(&w, &Worker::doWork);
196 EXPECT_EQ(f.value(), "start;static;class-static;class");
199 static Future<string> doWorkStaticFuture(Try<string>&& t) {
200 return makeFuture(t.value() + ";static");
203 TEST(Future, thenFunctionFuture) {
205 Future<string> doWorkFuture(Try<string>&& t) {
206 return makeFuture(t.value() + ";class");
208 static Future<string> doWorkStaticFuture(Try<string>&& t) {
209 return makeFuture(t.value() + ";class-static");
213 auto f = makeFuture<string>("start")
214 .then(doWorkStaticFuture)
215 .then(Worker::doWorkStaticFuture)
216 .then(&w, &Worker::doWorkFuture);
218 EXPECT_EQ(f.value(), "start;static;class-static;class");
221 TEST(Future, value) {
222 auto f = makeFuture(unique_ptr<int>(new int(42)));
223 auto up = std::move(f.value());
226 EXPECT_THROW(makeFuture<int>(eggs).value(), eggs_t);
229 TEST(Future, isReady) {
231 auto f = p.getFuture();
232 EXPECT_FALSE(f.isReady());
234 EXPECT_TRUE(f.isReady());
237 TEST(Future, futureNotReady) {
239 Future<int> f = p.getFuture();
240 EXPECT_THROW(f.value(), eggs_t);
243 TEST(Future, hasException) {
244 EXPECT_TRUE(makeFuture<int>(eggs).getTry().hasException());
245 EXPECT_FALSE(makeFuture(42).getTry().hasException());
248 TEST(Future, hasValue) {
249 EXPECT_TRUE(makeFuture(42).getTry().hasValue());
250 EXPECT_FALSE(makeFuture<int>(eggs).getTry().hasValue());
253 TEST(Future, makeFuture) {
254 EXPECT_TYPE(makeFuture(42), Future<int>);
255 EXPECT_EQ(42, makeFuture(42).value());
257 EXPECT_TYPE(makeFuture<float>(42), Future<float>);
258 EXPECT_EQ(42, makeFuture<float>(42).value());
260 auto fun = [] { return 42; };
261 EXPECT_TYPE(makeFutureTry(fun), Future<int>);
262 EXPECT_EQ(42, makeFutureTry(fun).value());
264 auto failfun = []() -> int { throw eggs; };
265 EXPECT_TYPE(makeFutureTry(failfun), Future<int>);
266 EXPECT_THROW(makeFutureTry(failfun).value(), eggs_t);
268 EXPECT_TYPE(makeFuture(), Future<void>);
273 TEST(Promise, special) {
274 EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
275 EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
276 EXPECT_TRUE(std::is_move_constructible<Promise<int>>::value);
277 EXPECT_TRUE(std::is_move_assignable<Promise<int>>::value);
280 TEST(Promise, getFuture) {
282 Future<int> f = p.getFuture();
283 EXPECT_FALSE(f.isReady());
286 TEST(Promise, setValue) {
288 auto ffund = fund.getFuture();
290 EXPECT_EQ(42, ffund.value());
298 auto fpod = pod.getFuture();
299 Foo f = {"the answer", 42};
301 Foo f2 = fpod.value();
302 EXPECT_EQ(f.name, f2.name);
303 EXPECT_EQ(f.value, f2.value);
305 pod = Promise<Foo>();
306 fpod = pod.getFuture();
307 pod.setValue(std::move(f2));
308 Foo f3 = fpod.value();
309 EXPECT_EQ(f.name, f3.name);
310 EXPECT_EQ(f.value, f3.value);
312 Promise<unique_ptr<int>> mov;
313 auto fmov = mov.getFuture();
314 mov.setValue(unique_ptr<int>(new int(42)));
315 unique_ptr<int> ptr = std::move(fmov.value());
319 auto fv = v.getFuture();
321 EXPECT_TRUE(fv.isReady());
324 TEST(Promise, setException) {
327 auto f = p.getFuture();
328 p.setException(eggs);
329 EXPECT_THROW(f.value(), eggs_t);
333 auto f = p.getFuture();
337 p.setException(std::current_exception());
339 EXPECT_THROW(f.value(), eggs_t);
343 TEST(Promise, fulfil) {
346 auto f = p.getFuture();
347 p.fulfil([] { return 42; });
348 EXPECT_EQ(42, f.value());
352 auto f = p.getFuture();
353 p.fulfil([]() -> int { throw eggs; });
354 EXPECT_THROW(f.value(), eggs_t);
358 TEST(Future, finish) {
359 auto x = std::make_shared<int>(0);
362 auto f = p.getFuture().then([x](Try<int>&& t) { *x = t.value(); });
364 // The callback hasn't executed
367 // The callback has a reference to x
368 EXPECT_EQ(2, x.use_count());
372 // the callback has executed
375 // the callback has been destructed
376 // and has released its reference to x
377 EXPECT_EQ(1, x.use_count());
380 TEST(Future, unwrap) {
384 auto fa = a.getFuture();
385 auto fb = b.getFuture();
390 // do a, then do b, and get the result of a + b.
391 Future<int> f = fa.then([&](Try<int>&& ta) {
392 auto va = ta.value();
394 return fb.then([va, &flag2](Try<int>&& tb) {
396 return va + tb.value();
402 EXPECT_FALSE(f.isReady());
407 EXPECT_FALSE(f.isReady());
412 EXPECT_EQ(7, f.value());
415 TEST(Future, whenAll) {
416 // returns a vector variant
418 vector<Promise<int>> promises(10);
419 vector<Future<int>> futures;
421 for (auto& p : promises)
422 futures.push_back(p.getFuture());
424 auto allf = whenAll(futures.begin(), futures.end());
426 random_shuffle(promises.begin(), promises.end());
427 for (auto& p : promises) {
428 EXPECT_FALSE(allf.isReady());
432 EXPECT_TRUE(allf.isReady());
433 auto& results = allf.value();
434 for (auto& t : results) {
435 EXPECT_EQ(42, t.value());
439 // check error semantics
441 vector<Promise<int>> promises(4);
442 vector<Future<int>> futures;
444 for (auto& p : promises)
445 futures.push_back(p.getFuture());
447 auto allf = whenAll(futures.begin(), futures.end());
450 promises[0].setValue(42);
451 promises[1].setException(eggs);
453 EXPECT_FALSE(allf.isReady());
455 promises[2].setValue(42);
457 EXPECT_FALSE(allf.isReady());
459 promises[3].setException(eggs);
461 EXPECT_TRUE(allf.isReady());
462 EXPECT_FALSE(allf.getTry().hasException());
464 auto& results = allf.value();
465 EXPECT_EQ(42, results[0].value());
466 EXPECT_TRUE(results[1].hasException());
467 EXPECT_EQ(42, results[2].value());
468 EXPECT_TRUE(results[3].hasException());
471 // check that futures are ready in then()
473 vector<Promise<void>> promises(10);
474 vector<Future<void>> futures;
476 for (auto& p : promises)
477 futures.push_back(p.getFuture());
479 auto allf = whenAll(futures.begin(), futures.end())
480 .then([](Try<vector<Try<void>>>&& ts) {
481 for (auto& f : ts.value())
485 random_shuffle(promises.begin(), promises.end());
486 for (auto& p : promises)
488 EXPECT_TRUE(allf.isReady());
493 TEST(Future, whenAny) {
495 vector<Promise<int>> promises(10);
496 vector<Future<int>> futures;
498 for (auto& p : promises)
499 futures.push_back(p.getFuture());
501 for (auto& f : futures) {
502 EXPECT_FALSE(f.isReady());
505 auto anyf = whenAny(futures.begin(), futures.end());
507 /* futures were moved in, so these are invalid now */
508 EXPECT_FALSE(anyf.isReady());
510 promises[7].setValue(42);
511 EXPECT_TRUE(anyf.isReady());
512 auto& idx_fut = anyf.value();
514 auto i = idx_fut.first;
517 auto& f = idx_fut.second;
518 EXPECT_EQ(42, f.value());
523 vector<Promise<void>> promises(10);
524 vector<Future<void>> futures;
526 for (auto& p : promises)
527 futures.push_back(p.getFuture());
529 for (auto& f : futures) {
530 EXPECT_FALSE(f.isReady());
533 auto anyf = whenAny(futures.begin(), futures.end());
535 EXPECT_FALSE(anyf.isReady());
537 promises[3].setException(eggs);
538 EXPECT_TRUE(anyf.isReady());
539 EXPECT_TRUE(anyf.value().second.hasException());
544 vector<Promise<int>> promises(10);
545 vector<Future<int>> futures;
547 for (auto& p : promises)
548 futures.push_back(p.getFuture());
550 auto anyf = whenAny(futures.begin(), futures.end())
551 .then([](Try<pair<size_t, Try<int>>>&& f) {
552 EXPECT_EQ(42, f.value().second.value());
555 promises[3].setValue(42);
556 EXPECT_TRUE(anyf.isReady());
561 TEST(when, already_completed) {
563 vector<Future<void>> fs;
564 for (int i = 0; i < 10; i++)
565 fs.push_back(makeFuture());
567 whenAll(fs.begin(), fs.end())
568 .then([&](Try<vector<Try<void>>>&& t) {
569 EXPECT_EQ(fs.size(), t.value().size());
573 vector<Future<int>> fs;
574 for (int i = 0; i < 10; i++)
575 fs.push_back(makeFuture(i));
577 whenAny(fs.begin(), fs.end())
578 .then([&](Try<pair<size_t, Try<int>>>&& t) {
580 EXPECT_EQ(p.first, p.second.value());
586 vector<Promise<void>> promises(10);
587 vector<Future<void>> futures;
589 for (auto& p : promises)
590 futures.push_back(p.getFuture());
594 whenN(futures.begin(), futures.end(), n)
595 .then([&](Try<vector<pair<size_t, Try<void>>>>&& t) {
598 EXPECT_EQ(n, v.size());
600 EXPECT_TRUE(tt.second.hasValue());
603 promises[0].setValue();
605 promises[1].setValue();
607 promises[2].setValue();
611 /* Ensure that we can compile when_{all,any} with folly::small_vector */
612 TEST(when, small_vector) {
614 static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(Future<void>),
615 "Futures should not be trivially copyable");
616 static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(Future<int>),
617 "Futures should not be trivially copyable");
619 using folly::small_vector;
621 small_vector<Future<void>> futures;
623 for (int i = 0; i < 10; i++)
624 futures.push_back(makeFuture());
626 auto anyf = whenAny(futures.begin(), futures.end());
630 small_vector<Future<void>> futures;
632 for (int i = 0; i < 10; i++)
633 futures.push_back(makeFuture());
635 auto allf = whenAll(futures.begin(), futures.end());
639 TEST(Future, whenAllVariadic) {
642 Future<bool> fb = pb.getFuture();
643 Future<int> fi = pi.getFuture();
645 whenAll(std::move(fb), std::move(fi))
646 .then([&](Try<std::tuple<Try<bool>, Try<int>>>&& t) {
648 EXPECT_TRUE(t.hasValue());
649 EXPECT_TRUE(std::get<0>(t.value()).hasValue());
650 EXPECT_EQ(std::get<0>(t.value()).value(), true);
651 EXPECT_TRUE(std::get<1>(t.value()).hasValue());
652 EXPECT_EQ(std::get<1>(t.value()).value(), 42);
660 TEST(Future, whenAllVariadicReferences) {
663 Future<bool> fb = pb.getFuture();
664 Future<int> fi = pi.getFuture();
667 .then([&](Try<std::tuple<Try<bool>, Try<int>>>&& t) {
669 EXPECT_TRUE(t.hasValue());
670 EXPECT_TRUE(std::get<0>(t.value()).hasValue());
671 EXPECT_EQ(std::get<0>(t.value()).value(), true);
672 EXPECT_TRUE(std::get<1>(t.value()).hasValue());
673 EXPECT_EQ(std::get<1>(t.value()).value(), 42);
681 TEST(Future, whenAll_none) {
682 vector<Future<int>> fs;
683 auto f = whenAll(fs.begin(), fs.end());
684 EXPECT_TRUE(f.isReady());
687 TEST(Future, throwCaughtInImmediateThen) {
688 // Neither of these should throw "Promise already satisfied"
690 [=](Try<void>&&) -> int { throw std::exception(); });
692 [=](Try<void>&&) -> Future<int> { throw std::exception(); });
695 TEST(Future, throwIfFailed) {
696 makeFuture<void>(eggs)
697 .then([=](Try<void>&& t) {
698 EXPECT_THROW(t.throwIfFailed(), eggs_t);
701 .then([=](Try<void>&& t) {
702 EXPECT_NO_THROW(t.throwIfFailed());
705 makeFuture<int>(eggs)
706 .then([=](Try<int>&& t) {
707 EXPECT_THROW(t.throwIfFailed(), eggs_t);
710 .then([=](Try<int>&& t) {
711 EXPECT_NO_THROW(t.throwIfFailed());
715 TEST(Future, waitWithSemaphoreImmediate) {
716 waitWithSemaphore(makeFuture());
717 auto done = waitWithSemaphore(makeFuture(42)).value();
720 vector<int> v{1,2,3};
721 auto done_v = waitWithSemaphore(makeFuture(v)).value();
722 EXPECT_EQ(v.size(), done_v.size());
723 EXPECT_EQ(v, done_v);
725 vector<Future<void>> v_f;
726 v_f.push_back(makeFuture());
727 v_f.push_back(makeFuture());
728 auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end())).value();
729 EXPECT_EQ(2, done_v_f.size());
731 vector<Future<bool>> v_fb;
732 v_fb.push_back(makeFuture(true));
733 v_fb.push_back(makeFuture(false));
734 auto fut = whenAll(v_fb.begin(), v_fb.end());
735 auto done_v_fb = std::move(waitWithSemaphore(std::move(fut)).value());
736 EXPECT_EQ(2, done_v_fb.size());
739 TEST(Future, waitWithSemaphore) {
741 Future<int> f = p.getFuture();
742 std::atomic<bool> flag{false};
743 std::atomic<int> result{1};
744 std::atomic<std::thread::id> id;
746 std::thread t([&](Future<int>&& tf){
747 auto n = tf.then([&](Try<int> && t) {
748 id = std::this_thread::get_id();
752 result.store(waitWithSemaphore(std::move(n)).value());
757 EXPECT_EQ(result.load(), 1);
760 // validate that the callback ended up executing in this thread, which
761 // is more to ensure that this test actually tests what it should
762 EXPECT_EQ(id, std::this_thread::get_id());
763 EXPECT_EQ(result.load(), 42);
766 TEST(Future, waitWithSemaphoreForTime) {
769 Future<int> f = p.getFuture();
770 auto t = waitWithSemaphore(std::move(f),
771 std::chrono::microseconds(1));
772 EXPECT_FALSE(t.isReady());
774 EXPECT_TRUE(t.isReady());
778 Future<int> f = p.getFuture();
780 auto t = waitWithSemaphore(std::move(f),
781 std::chrono::milliseconds(1));
782 EXPECT_TRUE(t.isReady());
785 vector<Future<bool>> v_fb;
786 v_fb.push_back(makeFuture(true));
787 v_fb.push_back(makeFuture(false));
788 auto f = whenAll(v_fb.begin(), v_fb.end());
789 auto t = waitWithSemaphore(std::move(f),
790 std::chrono::milliseconds(1));
791 EXPECT_TRUE(t.isReady());
792 EXPECT_EQ(2, t.value().size());
795 vector<Future<bool>> v_fb;
798 v_fb.push_back(p1.getFuture());
799 v_fb.push_back(p2.getFuture());
800 auto f = whenAll(v_fb.begin(), v_fb.end());
801 auto t = waitWithSemaphore(std::move(f),
802 std::chrono::milliseconds(1));
803 EXPECT_FALSE(t.isReady());
805 EXPECT_FALSE(t.isReady());
807 EXPECT_TRUE(t.isReady());
810 auto t = waitWithSemaphore(makeFuture(),
811 std::chrono::milliseconds(1));
812 EXPECT_TRUE(t.isReady());
816 TEST(Future, callbackAfterActivate) {
818 auto f = p.getFuture();
822 f.then([&](Try<void>&&) { count++; });
831 TEST(Future, activateOnDestruct) {
832 auto f = std::make_shared<Future<void>>(makeFuture());
836 f->then([&](Try<void>&&) { count++; });
843 TEST(Future, viaActsCold) {
848 fv.then([&](Try<void>&&) { count++; });
854 EXPECT_EQ(1, x.run());
858 TEST(Future, viaIsCold) {
860 EXPECT_FALSE(via(&x).isActive());
863 TEST(Future, viaRaces) {
866 auto tid = std::this_thread::get_id();
872 .then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
873 .then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
874 .then([&](Try<void>&&) { done = true; });
881 while (!done) x.run();
887 TEST(Future, viaRaces_2stage) {
890 auto tid = std::this_thread::get_id();
894 auto f2 = p.getFuture().via(&x);
895 f2.then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
896 .then([&](Try<void>&&) { EXPECT_EQ(tid, std::this_thread::get_id()); })
897 .then([&](Try<void>&&) { done = true; });
899 // the bug was in the promise being fulfilled before f2 is reactivated. we
900 // could sleep, but yielding should cause this to fail with reasonable
902 std::this_thread::yield();
910 while (!done) x.run();
915 TEST(Future, getFuture_after_setValue) {
918 EXPECT_EQ(42, p.getFuture().value());
921 TEST(Future, getFuture_after_setException) {
923 p.fulfil([]() -> void { throw std::logic_error("foo"); });
924 EXPECT_THROW(p.getFuture().value(), std::logic_error);
927 TEST(Future, detachRace) {
929 // This test is designed to detect a race that was in Core::detachOne()
930 // where detached_ was incremented and then tested, and that
931 // allowed a race where both Promise and Future would think they were the
932 // second and both try to delete. This showed up at scale but was very
933 // difficult to reliably repro in a test. As it is, this only fails about
934 // once in every 1,000 executions. Doing this 1,000 times is going to make a
935 // slow test so I won't do that but if it ever fails, take it seriously, and
936 // run the test binary with "--gtest_repeat=10000 --gtest_filter=*detachRace"
937 // (Don't forget to enable ASAN)
938 auto p = folly::make_unique<Promise<bool>>();
939 auto f = folly::make_unique<Future<bool>>(p->getFuture());
940 folly::Baton<> baton;
950 class TestData : public RequestData {
952 explicit TestData(int data) : data_(data) {}
953 virtual ~TestData() {}
957 TEST(Future, context) {
959 // Start a new context
960 RequestContext::create();
962 EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
964 // Set some test data
965 RequestContext::get()->setContextData(
967 std::unique_ptr<TestData>(new TestData(10)));
971 auto future = p.getFuture().then([&]{
972 // Check that the context followed the future
973 EXPECT_TRUE(RequestContext::get() != nullptr);
974 auto a = dynamic_cast<TestData*>(
975 RequestContext::get()->getContextData("test"));
976 auto data = a->data_;
981 RequestContext::setContext(nullptr);
983 EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
985 // Fulfil the promise
990 // This only fails about 1 in 1k times when the bug is present :(
991 TEST(Future, t5506504) {
995 auto promises = std::make_shared<vector<Promise<void>>>(4);
996 vector<Future<void>> futures;
998 for (auto& p : *promises) {
999 futures.emplace_back(
1002 .then([](Try<void>&&){}));
1007 for (auto& p : *promises) p.setValue();
1010 return whenAll(futures.begin(), futures.end());
1013 waitWithSemaphore(fn());