allow passing function pointers to Future::onError()
[folly.git] / folly / futures / test / FutureTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/futures/Future.h>
18 #include <folly/Unit.h>
19 #include <folly/Memory.h>
20 #include <folly/Executor.h>
21 #include <folly/dynamic.h>
22 #include <folly/Baton.h>
23 #include <folly/portability/GTest.h>
24
25 #include <algorithm>
26 #include <atomic>
27 #include <memory>
28 #include <numeric>
29 #include <string>
30 #include <thread>
31 #include <type_traits>
32
33 using namespace folly;
34
35 #define EXPECT_TYPE(x, T) \
36   EXPECT_TRUE((std::is_same<decltype(x), T>::value))
37
38 typedef FutureException eggs_t;
39 static eggs_t eggs("eggs");
40
41 // Future
42
43 TEST(Future, futureDefaultCtor) {
44   Future<Unit>();
45 }
46
47 TEST(Future, futureToUnit) {
48   Future<Unit> fu = makeFuture(42).unit();
49   fu.value();
50   EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
51 }
52
53 TEST(Future, voidFutureToUnit) {
54   Future<Unit> fu = makeFuture().unit();
55   fu.value();
56   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
57 }
58
59 TEST(Future, unitFutureToUnitIdentity) {
60   Future<Unit> fu = makeFuture(Unit{}).unit();
61   fu.value();
62   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
63 }
64
65 TEST(Future, toUnitWhileInProgress) {
66   Promise<int> p;
67   Future<Unit> fu = p.getFuture().unit();
68   EXPECT_FALSE(fu.isReady());
69   p.setValue(42);
70   EXPECT_TRUE(fu.isReady());
71 }
72
73 TEST(Future, makeFutureWithUnit) {
74   int count = 0;
75   Future<Unit> fu = makeFutureWith([&] { count++; });
76   EXPECT_EQ(1, count);
77 }
78
79 namespace {
80 Future<int> onErrorHelperEggs(const eggs_t&) {
81   return makeFuture(10);
82 }
83 Future<int> onErrorHelperGeneric(const std::exception&) {
84   return makeFuture(20);
85 }
86 }
87
88 TEST(Future, onError) {
89   bool theFlag = false;
90   auto flag = [&]{ theFlag = true; };
91 #define EXPECT_FLAG() \
92   do { \
93     EXPECT_TRUE(theFlag); \
94     theFlag = false; \
95   } while(0);
96
97 #define EXPECT_NO_FLAG() \
98   do { \
99     EXPECT_FALSE(theFlag); \
100     theFlag = false; \
101   } while(0);
102
103   // By reference
104   {
105     auto f = makeFuture().then([] {
106       throw eggs;
107     }).onError([&](eggs_t& /* e */) { flag(); });
108     EXPECT_FLAG();
109     EXPECT_NO_THROW(f.value());
110   }
111
112   {
113     auto f = makeFuture()
114                  .then([] { throw eggs; })
115                  .onError([&](eggs_t& /* e */) {
116                    flag();
117                    return makeFuture();
118                  });
119     EXPECT_FLAG();
120     EXPECT_NO_THROW(f.value());
121   }
122
123   // By value
124   {
125     auto f = makeFuture().then([] {
126       throw eggs;
127     }).onError([&](eggs_t /* e */) { flag(); });
128     EXPECT_FLAG();
129     EXPECT_NO_THROW(f.value());
130   }
131
132   {
133     auto f = makeFuture()
134                  .then([] { throw eggs; })
135                  .onError([&](eggs_t /* e */) {
136                    flag();
137                    return makeFuture();
138                  });
139     EXPECT_FLAG();
140     EXPECT_NO_THROW(f.value());
141   }
142
143   // Polymorphic
144   {
145     auto f = makeFuture().then([] {
146       throw eggs;
147     }).onError([&](std::exception& /* e */) { flag(); });
148     EXPECT_FLAG();
149     EXPECT_NO_THROW(f.value());
150   }
151
152   {
153     auto f = makeFuture()
154                  .then([] { throw eggs; })
155                  .onError([&](std::exception& /* e */) {
156                    flag();
157                    return makeFuture();
158                  });
159     EXPECT_FLAG();
160     EXPECT_NO_THROW(f.value());
161   }
162
163   // Non-exceptions
164   {
165     auto f = makeFuture().then([] {
166       throw - 1;
167     }).onError([&](int /* e */) { flag(); });
168     EXPECT_FLAG();
169     EXPECT_NO_THROW(f.value());
170   }
171
172   {
173     auto f = makeFuture()
174                  .then([] { throw - 1; })
175                  .onError([&](int /* e */) {
176                    flag();
177                    return makeFuture();
178                  });
179     EXPECT_FLAG();
180     EXPECT_NO_THROW(f.value());
181   }
182
183   // Mutable lambda
184   {
185     auto f = makeFuture().then([] {
186       throw eggs;
187     }).onError([&](eggs_t& /* e */) mutable { flag(); });
188     EXPECT_FLAG();
189     EXPECT_NO_THROW(f.value());
190   }
191
192   {
193     auto f = makeFuture()
194                  .then([] { throw eggs; })
195                  .onError([&](eggs_t& /* e */) mutable {
196                    flag();
197                    return makeFuture();
198                  });
199     EXPECT_FLAG();
200     EXPECT_NO_THROW(f.value());
201   }
202
203   // Function pointer
204   {
205     auto f = makeFuture()
206                  .then([]() -> int { throw eggs; })
207                  .onError(onErrorHelperEggs)
208                  .onError(onErrorHelperGeneric);
209     EXPECT_EQ(10, f.value());
210   }
211   {
212     auto f = makeFuture()
213                  .then([]() -> int { throw std::runtime_error("test"); })
214                  .onError(onErrorHelperEggs)
215                  .onError(onErrorHelperGeneric);
216     EXPECT_EQ(20, f.value());
217   }
218   {
219     auto f = makeFuture()
220                  .then([]() -> int { throw std::runtime_error("test"); })
221                  .onError(onErrorHelperEggs);
222     EXPECT_THROW(f.value(), std::runtime_error);
223   }
224
225   // No throw
226   {
227     auto f = makeFuture()
228                  .then([] { return 42; })
229                  .onError([&](eggs_t& /* e */) {
230                    flag();
231                    return -1;
232                  });
233     EXPECT_NO_FLAG();
234     EXPECT_EQ(42, f.value());
235   }
236
237   {
238     auto f = makeFuture()
239                  .then([] { return 42; })
240                  .onError([&](eggs_t& /* e */) {
241                    flag();
242                    return makeFuture<int>(-1);
243                  });
244     EXPECT_NO_FLAG();
245     EXPECT_EQ(42, f.value());
246   }
247
248   // Catch different exception
249   {
250     auto f = makeFuture().then([] {
251       throw eggs;
252     }).onError([&](std::runtime_error& /* e */) { flag(); });
253     EXPECT_NO_FLAG();
254     EXPECT_THROW(f.value(), eggs_t);
255   }
256
257   {
258     auto f = makeFuture()
259                  .then([] { throw eggs; })
260                  .onError([&](std::runtime_error& /* e */) {
261                    flag();
262                    return makeFuture();
263                  });
264     EXPECT_NO_FLAG();
265     EXPECT_THROW(f.value(), eggs_t);
266   }
267
268   // Returned value propagates
269   {
270     auto f = makeFuture().then([]() -> int {
271       throw eggs;
272     }).onError([&](eggs_t& /* e */) { return 42; });
273     EXPECT_EQ(42, f.value());
274   }
275
276   // Returned future propagates
277   {
278     auto f = makeFuture().then([]() -> int {
279       throw eggs;
280     }).onError([&](eggs_t& /* e */) { return makeFuture<int>(42); });
281     EXPECT_EQ(42, f.value());
282   }
283
284   // Throw in callback
285   {
286     auto f = makeFuture()
287       .then([]() -> int { throw eggs; })
288       .onError([&] (eggs_t& e) -> int { throw e; });
289     EXPECT_THROW(f.value(), eggs_t);
290   }
291
292   {
293     auto f = makeFuture()
294       .then([]() -> int { throw eggs; })
295       .onError([&] (eggs_t& e) -> Future<int> { throw e; });
296     EXPECT_THROW(f.value(), eggs_t);
297   }
298
299   // exception_wrapper, return Future<T>
300   {
301     auto f = makeFuture()
302                  .then([] { throw eggs; })
303                  .onError([&](exception_wrapper /* e */) {
304                    flag();
305                    return makeFuture();
306                  });
307     EXPECT_FLAG();
308     EXPECT_NO_THROW(f.value());
309   }
310
311   // exception_wrapper, return Future<T> but throw
312   {
313     auto f = makeFuture()
314                  .then([]() -> int {
315                    throw eggs;
316                  })
317                  .onError([&](exception_wrapper /* e */) -> Future<int> {
318                    flag();
319                    throw eggs;
320                  });
321     EXPECT_FLAG();
322     EXPECT_THROW(f.value(), eggs_t);
323   }
324
325   // exception_wrapper, return T
326   {
327     auto f = makeFuture()
328                  .then([]() -> int {
329                    throw eggs;
330                  })
331                  .onError([&](exception_wrapper /* e */) {
332                    flag();
333                    return -1;
334                  });
335     EXPECT_FLAG();
336     EXPECT_EQ(-1, f.value());
337   }
338
339   // exception_wrapper, return T but throw
340   {
341     auto f = makeFuture()
342                  .then([]() -> int {
343                    throw eggs;
344                  })
345                  .onError([&](exception_wrapper /* e */) -> int {
346                    flag();
347                    throw eggs;
348                  });
349     EXPECT_FLAG();
350     EXPECT_THROW(f.value(), eggs_t);
351   }
352
353   // const exception_wrapper&
354   {
355     auto f = makeFuture()
356                  .then([] { throw eggs; })
357                  .onError([&](const exception_wrapper& /* e */) {
358                    flag();
359                    return makeFuture();
360                  });
361     EXPECT_FLAG();
362     EXPECT_NO_THROW(f.value());
363   }
364
365 }
366
367 TEST(Future, special) {
368   EXPECT_FALSE(std::is_copy_constructible<Future<int>>::value);
369   EXPECT_FALSE(std::is_copy_assignable<Future<int>>::value);
370   EXPECT_TRUE(std::is_move_constructible<Future<int>>::value);
371   EXPECT_TRUE(std::is_move_assignable<Future<int>>::value);
372 }
373
374 TEST(Future, then) {
375   auto f = makeFuture<std::string>("0")
376     .then([](){
377       return makeFuture<std::string>("1"); })
378     .then([](Try<std::string>&& t) {
379       return makeFuture(t.value() + ";2"); })
380     .then([](const Try<std::string>&& t) {
381       return makeFuture(t.value() + ";3"); })
382     .then([](Try<std::string>& t) {
383       return makeFuture(t.value() + ";4"); })
384     .then([](const Try<std::string>& t) {
385       return makeFuture(t.value() + ";5"); })
386     .then([](Try<std::string> t) {
387       return makeFuture(t.value() + ";6"); })
388     .then([](const Try<std::string> t) {
389       return makeFuture(t.value() + ";7"); })
390     .then([](std::string&& s) {
391       return makeFuture(s + ";8"); })
392     .then([](const std::string&& s) {
393       return makeFuture(s + ";9"); })
394     .then([](std::string& s) {
395       return makeFuture(s + ";10"); })
396     .then([](const std::string& s) {
397       return makeFuture(s + ";11"); })
398     .then([](std::string s) {
399       return makeFuture(s + ";12"); })
400     .then([](const std::string s) {
401       return makeFuture(s + ";13"); })
402   ;
403   EXPECT_EQ(f.value(), "1;2;3;4;5;6;7;8;9;10;11;12;13");
404 }
405
406 TEST(Future, thenTry) {
407   bool flag = false;
408
409   makeFuture<int>(42).then([&](Try<int>&& t) {
410                               flag = true;
411                               EXPECT_EQ(42, t.value());
412                             });
413   EXPECT_TRUE(flag); flag = false;
414
415   makeFuture<int>(42)
416     .then([](Try<int>&& t) { return t.value(); })
417     .then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
418   EXPECT_TRUE(flag); flag = false;
419
420   makeFuture().then([&](Try<Unit>&& t) { flag = true; t.value(); });
421   EXPECT_TRUE(flag); flag = false;
422
423   Promise<Unit> p;
424   auto f = p.getFuture().then([&](Try<Unit>&& /* t */) { flag = true; });
425   EXPECT_FALSE(flag);
426   EXPECT_FALSE(f.isReady());
427   p.setValue();
428   EXPECT_TRUE(flag);
429   EXPECT_TRUE(f.isReady());
430 }
431
432 TEST(Future, thenValue) {
433   bool flag = false;
434   makeFuture<int>(42).then([&](int i){
435     EXPECT_EQ(42, i);
436     flag = true;
437   });
438   EXPECT_TRUE(flag); flag = false;
439
440   makeFuture<int>(42)
441     .then([](int i){ return i; })
442     .then([&](int i) { flag = true; EXPECT_EQ(42, i); });
443   EXPECT_TRUE(flag); flag = false;
444
445   makeFuture().then([&]{
446     flag = true;
447   });
448   EXPECT_TRUE(flag); flag = false;
449
450   auto f = makeFuture<int>(eggs).then([&](int /* i */) {});
451   EXPECT_THROW(f.value(), eggs_t);
452
453   f = makeFuture<Unit>(eggs).then([&]{});
454   EXPECT_THROW(f.value(), eggs_t);
455 }
456
457 TEST(Future, thenValueFuture) {
458   bool flag = false;
459   makeFuture<int>(42)
460     .then([](int i){ return makeFuture<int>(std::move(i)); })
461     .then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
462   EXPECT_TRUE(flag); flag = false;
463
464   makeFuture().then([] {
465     return makeFuture();
466   }).then([&](Try<Unit>&& /* t */) { flag = true; });
467   EXPECT_TRUE(flag); flag = false;
468 }
469
470 static std::string doWorkStatic(Try<std::string>&& t) {
471   return t.value() + ";static";
472 }
473
474 TEST(Future, thenFunction) {
475   struct Worker {
476     std::string doWork(Try<std::string>&& t) {
477       return t.value() + ";class";
478     }
479     static std::string doWorkStatic(Try<std::string>&& t) {
480       return t.value() + ";class-static";
481     }
482   } w;
483
484   auto f = makeFuture<std::string>("start")
485     .then(doWorkStatic)
486     .then(Worker::doWorkStatic)
487     .then(&Worker::doWork, &w);
488
489   EXPECT_EQ(f.value(), "start;static;class-static;class");
490 }
491
492 static Future<std::string> doWorkStaticFuture(Try<std::string>&& t) {
493   return makeFuture(t.value() + ";static");
494 }
495
496 TEST(Future, thenFunctionFuture) {
497   struct Worker {
498     Future<std::string> doWorkFuture(Try<std::string>&& t) {
499       return makeFuture(t.value() + ";class");
500     }
501     static Future<std::string> doWorkStaticFuture(Try<std::string>&& t) {
502       return makeFuture(t.value() + ";class-static");
503     }
504   } w;
505
506   auto f = makeFuture<std::string>("start")
507     .then(doWorkStaticFuture)
508     .then(Worker::doWorkStaticFuture)
509     .then(&Worker::doWorkFuture, &w);
510
511   EXPECT_EQ(f.value(), "start;static;class-static;class");
512 }
513
514 TEST(Future, thenStdFunction) {
515   {
516     std::function<int()> fn = [](){ return 42; };
517     auto f = makeFuture().then(std::move(fn));
518     EXPECT_EQ(f.value(), 42);
519   }
520   {
521     std::function<int(int)> fn = [](int i){ return i + 23; };
522     auto f = makeFuture(19).then(std::move(fn));
523     EXPECT_EQ(f.value(), 42);
524   }
525   {
526     std::function<int(Try<int>&)> fn = [](Try<int>& t){ return t.value() + 2; };
527     auto f = makeFuture(1).then(std::move(fn));
528     EXPECT_EQ(f.value(), 3);
529   }
530   {
531     bool flag = false;
532     std::function<void()> fn = [&flag](){ flag = true; };
533     auto f = makeFuture().then(std::move(fn));
534     EXPECT_TRUE(f.isReady());
535     EXPECT_TRUE(flag);
536   }
537 }
538
539 TEST(Future, thenBind) {
540   auto l = []() {
541     return makeFuture("bind");
542   };
543   auto b = std::bind(l);
544   auto f = makeFuture().then(std::move(b));
545   EXPECT_EQ(f.value(), "bind");
546 }
547
548 TEST(Future, thenBindTry) {
549   auto l = [](Try<std::string>&& t) {
550     return makeFuture(t.value() + ";bind");
551   };
552   auto b = std::bind(l, std::placeholders::_1);
553   auto f = makeFuture<std::string>("start").then(std::move(b));
554
555   EXPECT_EQ(f.value(), "start;bind");
556 }
557
558 TEST(Future, value) {
559   auto f = makeFuture(std::unique_ptr<int>(new int(42)));
560   auto up = std::move(f.value());
561   EXPECT_EQ(42, *up);
562
563   EXPECT_THROW(makeFuture<int>(eggs).value(), eggs_t);
564 }
565
566 TEST(Future, isReady) {
567   Promise<int> p;
568   auto f = p.getFuture();
569   EXPECT_FALSE(f.isReady());
570   p.setValue(42);
571   EXPECT_TRUE(f.isReady());
572   }
573
574 TEST(Future, futureNotReady) {
575   Promise<int> p;
576   Future<int> f = p.getFuture();
577   EXPECT_THROW(f.value(), eggs_t);
578 }
579
580 TEST(Future, hasException) {
581   EXPECT_TRUE(makeFuture<int>(eggs).getTry().hasException());
582   EXPECT_FALSE(makeFuture(42).getTry().hasException());
583 }
584
585 TEST(Future, hasValue) {
586   EXPECT_TRUE(makeFuture(42).getTry().hasValue());
587   EXPECT_FALSE(makeFuture<int>(eggs).getTry().hasValue());
588 }
589
590 TEST(Future, makeFuture) {
591   EXPECT_TYPE(makeFuture(42), Future<int>);
592   EXPECT_EQ(42, makeFuture(42).value());
593
594   EXPECT_TYPE(makeFuture<float>(42), Future<float>);
595   EXPECT_EQ(42, makeFuture<float>(42).value());
596
597   auto fun = [] { return 42; };
598   EXPECT_TYPE(makeFutureWith(fun), Future<int>);
599   EXPECT_EQ(42, makeFutureWith(fun).value());
600
601   auto funf = [] { return makeFuture<int>(43); };
602   EXPECT_TYPE(makeFutureWith(funf), Future<int>);
603   EXPECT_EQ(43, makeFutureWith(funf).value());
604
605   auto failfun = []() -> int { throw eggs; };
606   EXPECT_TYPE(makeFutureWith(failfun), Future<int>);
607   EXPECT_NO_THROW(makeFutureWith(failfun));
608   EXPECT_THROW(makeFutureWith(failfun).value(), eggs_t);
609
610   auto failfunf = []() -> Future<int> { throw eggs; };
611   EXPECT_TYPE(makeFutureWith(failfunf), Future<int>);
612   EXPECT_NO_THROW(makeFutureWith(failfunf));
613   EXPECT_THROW(makeFutureWith(failfunf).value(), eggs_t);
614
615   EXPECT_TYPE(makeFuture(), Future<Unit>);
616 }
617
618 TEST(Future, finish) {
619   auto x = std::make_shared<int>(0);
620
621   Promise<int> p;
622   auto f = p.getFuture().then([x](Try<int>&& t) { *x = t.value(); });
623
624   // The callback hasn't executed
625   EXPECT_EQ(0, *x);
626
627   // The callback has a reference to x
628   EXPECT_EQ(2, x.use_count());
629
630   p.setValue(42);
631
632   // the callback has executed
633   EXPECT_EQ(42, *x);
634
635   // the callback has been destructed
636   // and has released its reference to x
637   EXPECT_EQ(1, x.use_count());
638 }
639
640 TEST(Future, finishBigLambda) {
641   auto x = std::make_shared<int>(0);
642
643   // bulk_data, to be captured in the lambda passed to Future::then.
644   // This is meant to force that the lambda can't be stored inside
645   // the Future object.
646   std::array<char, sizeof(detail::Core<int>)> bulk_data = {{0}};
647
648   // suppress gcc warning about bulk_data not being used
649   EXPECT_EQ(bulk_data[0], 0);
650
651   Promise<int> p;
652   auto f = p.getFuture().then([x, bulk_data](Try<int>&& t) {
653     (void)bulk_data;
654     *x = t.value();
655   });
656
657   // The callback hasn't executed
658   EXPECT_EQ(0, *x);
659
660   // The callback has a reference to x
661   EXPECT_EQ(2, x.use_count());
662
663   p.setValue(42);
664
665   // the callback has executed
666   EXPECT_EQ(42, *x);
667
668   // the callback has been destructed
669   // and has released its reference to x
670   EXPECT_EQ(1, x.use_count());
671 }
672
673 TEST(Future, unwrap) {
674   Promise<int> a;
675   Promise<int> b;
676
677   auto fa = a.getFuture();
678   auto fb = b.getFuture();
679
680   bool flag1 = false;
681   bool flag2 = false;
682
683   // do a, then do b, and get the result of a + b.
684   Future<int> f = fa.then([&](Try<int>&& ta) {
685     auto va = ta.value();
686     flag1 = true;
687     return fb.then([va, &flag2](Try<int>&& tb) {
688       flag2 = true;
689       return va + tb.value();
690     });
691   });
692
693   EXPECT_FALSE(flag1);
694   EXPECT_FALSE(flag2);
695   EXPECT_FALSE(f.isReady());
696
697   a.setValue(3);
698   EXPECT_TRUE(flag1);
699   EXPECT_FALSE(flag2);
700   EXPECT_FALSE(f.isReady());
701
702   b.setValue(4);
703   EXPECT_TRUE(flag1);
704   EXPECT_TRUE(flag2);
705   EXPECT_EQ(7, f.value());
706 }
707
708 TEST(Future, throwCaughtInImmediateThen) {
709   // Neither of these should throw "Promise already satisfied"
710   makeFuture().then(
711     [=](Try<Unit>&&) -> int { throw std::exception(); });
712   makeFuture().then(
713     [=](Try<Unit>&&) -> Future<int> { throw std::exception(); });
714 }
715
716 TEST(Future, throwIfFailed) {
717   makeFuture<Unit>(eggs)
718     .then([=](Try<Unit>&& t) {
719       EXPECT_THROW(t.throwIfFailed(), eggs_t);
720     });
721   makeFuture()
722     .then([=](Try<Unit>&& t) {
723       EXPECT_NO_THROW(t.throwIfFailed());
724     });
725
726   makeFuture<int>(eggs)
727     .then([=](Try<int>&& t) {
728       EXPECT_THROW(t.throwIfFailed(), eggs_t);
729     });
730   makeFuture<int>(42)
731     .then([=](Try<int>&& t) {
732       EXPECT_NO_THROW(t.throwIfFailed());
733     });
734 }
735
736 TEST(Future, getFutureAfterSetValue) {
737   Promise<int> p;
738   p.setValue(42);
739   EXPECT_EQ(42, p.getFuture().value());
740 }
741
742 TEST(Future, getFutureAfterSetException) {
743   Promise<Unit> p;
744   p.setWith([]() -> void { throw std::logic_error("foo"); });
745   EXPECT_THROW(p.getFuture().value(), std::logic_error);
746 }
747
748 TEST(Future, detachRace) {
749   // Task #5438209
750   // This test is designed to detect a race that was in Core::detachOne()
751   // where detached_ was incremented and then tested, and that
752   // allowed a race where both Promise and Future would think they were the
753   // second and both try to delete. This showed up at scale but was very
754   // difficult to reliably repro in a test. As it is, this only fails about
755   // once in every 1,000 executions. Doing this 1,000 times is going to make a
756   // slow test so I won't do that but if it ever fails, take it seriously, and
757   // run the test binary with "--gtest_repeat=10000 --gtest_filter=*detachRace"
758   // (Don't forget to enable ASAN)
759   auto p = std::make_unique<Promise<bool>>();
760   auto f = std::make_unique<Future<bool>>(p->getFuture());
761   folly::Baton<> baton;
762   std::thread t1([&]{
763     baton.post();
764     p.reset();
765   });
766   baton.wait();
767   f.reset();
768   t1.join();
769 }
770
771 // Test of handling of a circular dependency. It's never recommended
772 // to have one because of possible memory leaks. Here we test that
773 // we can handle freeing of the Future while it is running.
774 TEST(Future, CircularDependencySharedPtrSelfReset) {
775   Promise<int64_t> promise;
776   auto ptr = std::make_shared<Future<int64_t>>(promise.getFuture());
777
778   ptr->then([ptr](folly::Try<int64_t>&& /* uid */) mutable {
779     EXPECT_EQ(1, ptr.use_count());
780
781     // Leaving no references to ourselves.
782     ptr.reset();
783     EXPECT_EQ(0, ptr.use_count());
784   });
785
786   EXPECT_EQ(2, ptr.use_count());
787
788   ptr.reset();
789
790   promise.setValue(1);
791 }
792
793 TEST(Future, Constructor) {
794   auto f1 = []() -> Future<int> { return Future<int>(3); }();
795   EXPECT_EQ(f1.value(), 3);
796   auto f2 = []() -> Future<Unit> { return Future<Unit>(); }();
797   EXPECT_NO_THROW(f2.value());
798 }
799
800 TEST(Future, ImplicitConstructor) {
801   auto f1 = []() -> Future<int> { return 3; }();
802   EXPECT_EQ(f1.value(), 3);
803   // Unfortunately, the C++ standard does not allow the
804   // following implicit conversion to work:
805   //auto f2 = []() -> Future<Unit> { }();
806 }
807
808 TEST(Future, thenDynamic) {
809   // folly::dynamic has a constructor that takes any T, this test makes
810   // sure that we call the then lambda with folly::dynamic and not
811   // Try<folly::dynamic> because that then fails to compile
812   Promise<folly::dynamic> p;
813   Future<folly::dynamic> f = p.getFuture().then(
814       [](const folly::dynamic& d) {
815         return folly::dynamic(d.asInt() + 3);
816       }
817   );
818   p.setValue(2);
819   EXPECT_EQ(f.get(), 5);
820 }
821
822 TEST(Future, RequestContext) {
823   class NewThreadExecutor : public Executor {
824    public:
825     ~NewThreadExecutor() override {
826       std::for_each(v_.begin(), v_.end(), [](std::thread& t){ t.join(); });
827     }
828     void add(Func f) override {
829       if (throwsOnAdd_) { throw std::exception(); }
830       v_.emplace_back(std::move(f));
831     }
832     void addWithPriority(Func f, int8_t /* prio */) override {
833       add(std::move(f));
834     }
835     uint8_t getNumPriorities() const override { return numPriorities_; }
836
837     void setHandlesPriorities() { numPriorities_ = 2; }
838     void setThrowsOnAdd() { throwsOnAdd_ = true; }
839    private:
840     std::vector<std::thread> v_;
841     uint8_t numPriorities_ = 1;
842     bool throwsOnAdd_ = false;
843   };
844
845   struct MyRequestData : RequestData {
846     MyRequestData(bool value = false) : value(value) {}
847     bool value;
848   };
849
850   Promise<int> p1, p2;
851   NewThreadExecutor e;
852   {
853     folly::RequestContextScopeGuard rctx;
854     RequestContext::get()->setContextData(
855         "key", std::make_unique<MyRequestData>(true));
856     auto checker = [](int lineno) {
857       return [lineno](Try<int>&& /* t */) {
858         auto d = static_cast<MyRequestData*>(
859             RequestContext::get()->getContextData("key"));
860         EXPECT_TRUE(d && d->value) << "on line " << lineno;
861       };
862     };
863
864     makeFuture(1).via(&e).then(checker(__LINE__));
865
866     e.setHandlesPriorities();
867     makeFuture(2).via(&e).then(checker(__LINE__));
868
869     p1.getFuture().then(checker(__LINE__));
870
871     e.setThrowsOnAdd();
872     p2.getFuture().via(&e).then(checker(__LINE__));
873   }
874   // Assert that no RequestContext is set
875   EXPECT_FALSE(RequestContext::saveContext());
876   p1.setValue(3);
877   p2.setValue(4);
878 }
879
880 TEST(Future, makeFutureNoThrow) {
881   makeFuture().value();
882 }
883
884 TEST(Future, invokeCallbackReturningValueAsRvalue) {
885   struct Foo {
886     int operator()(int x) & {
887       return x + 1;
888     }
889     int operator()(int x) const& {
890       return x + 2;
891     }
892     int operator()(int x) && {
893       return x + 3;
894     }
895   };
896
897   Foo foo;
898   Foo const cfoo;
899
900   // The callback will be copied when given as lvalue or const ref, and moved
901   // if provided as rvalue. Either way, it should be executed as rvalue.
902   EXPECT_EQ(103, makeFuture<int>(100).then(foo).value());
903   EXPECT_EQ(203, makeFuture<int>(200).then(cfoo).value());
904   EXPECT_EQ(303, makeFuture<int>(300).then(Foo()).value());
905 }
906
907 TEST(Future, invokeCallbackReturningFutureAsRvalue) {
908   struct Foo {
909     Future<int> operator()(int x) & {
910       return x + 1;
911     }
912     Future<int> operator()(int x) const& {
913       return x + 2;
914     }
915     Future<int> operator()(int x) && {
916       return x + 3;
917     }
918   };
919
920   Foo foo;
921   Foo const cfoo;
922
923   // The callback will be copied when given as lvalue or const ref, and moved
924   // if provided as rvalue. Either way, it should be executed as rvalue.
925   EXPECT_EQ(103, makeFuture<int>(100).then(foo).value());
926   EXPECT_EQ(203, makeFuture<int>(200).then(cfoo).value());
927   EXPECT_EQ(303, makeFuture<int>(300).then(Foo()).value());
928 }