Handle timekeeperSingleton being nullptr in within()
[folly.git] / folly / futures / test / TimekeeperTest.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/Timekeeper.h>
18 #include <folly/Singleton.h>
19 #include <folly/futures/ThreadWheelTimekeeper.h>
20 #include <folly/portability/GTest.h>
21
22 using namespace folly;
23 using std::chrono::milliseconds;
24
25 std::chrono::milliseconds const zero_ms(0);
26 std::chrono::milliseconds const one_ms(1);
27 std::chrono::milliseconds const awhile(10);
28 std::chrono::seconds const too_long(10);
29
30 std::chrono::steady_clock::time_point now() {
31   return std::chrono::steady_clock::now();
32 }
33
34 struct TimekeeperFixture : public testing::Test {
35   TimekeeperFixture() :
36     timeLord_(folly::detail::getTimekeeperSingleton())
37   {}
38
39   std::shared_ptr<Timekeeper> timeLord_;
40 };
41
42 TEST_F(TimekeeperFixture, after) {
43   auto t1 = now();
44   auto f = timeLord_->after(awhile);
45   EXPECT_FALSE(f.isReady());
46   f.get();
47   auto t2 = now();
48
49   EXPECT_GE(t2 - t1, awhile);
50 }
51
52 TEST(Timekeeper, futureGet) {
53   Promise<int> p;
54   auto t = std::thread([&]{ p.setValue(42); });
55   EXPECT_EQ(42, p.getFuture().get());
56   t.join();
57 }
58
59 TEST(Timekeeper, futureGetBeforeTimeout) {
60   Promise<int> p;
61   auto t = std::thread([&]{ p.setValue(42); });
62   // Technically this is a race and if the test server is REALLY overloaded
63   // and it takes more than a second to do that thread it could be flaky. But
64   // I want a low timeout (in human terms) so if this regresses and someone
65   // runs it by hand they're not sitting there forever wondering why it's
66   // blocked, and get a useful error message instead. If it does get flaky,
67   // empirically increase the timeout to the point where it's very improbable.
68   EXPECT_EQ(42, p.getFuture().get(std::chrono::seconds(2)));
69   t.join();
70 }
71
72 TEST(Timekeeper, futureGetTimeout) {
73   Promise<int> p;
74   EXPECT_THROW(p.getFuture().get(one_ms), folly::TimedOut);
75 }
76
77 TEST(Timekeeper, futureSleep) {
78   auto t1 = now();
79   futures::sleep(one_ms).get();
80   EXPECT_GE(now() - t1, one_ms);
81 }
82
83 TEST(Timekeeper, futureSleepHandlesNullTimekeeperSingleton) {
84   Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
85   SCOPE_EXIT {
86     Singleton<ThreadWheelTimekeeper>::make_mock();
87   };
88   EXPECT_THROW(futures::sleep(one_ms).get(), NoTimekeeper);
89 }
90
91 TEST(Timekeeper, futureWithinHandlesNullTimekeeperSingleton) {
92   Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
93   SCOPE_EXIT {
94     Singleton<ThreadWheelTimekeeper>::make_mock();
95   };
96   Promise<int> p;
97   auto f = p.getFuture().within(one_ms);
98   EXPECT_THROW(f.get(), NoTimekeeper);
99 }
100
101 TEST(Timekeeper, futureDelayed) {
102   auto t1 = now();
103   auto dur = makeFuture()
104     .delayed(one_ms)
105     .then([=]{ return now() - t1; })
106     .get();
107
108   EXPECT_GE(dur, one_ms);
109 }
110
111 TEST(Timekeeper, futureWithinThrows) {
112   Promise<int> p;
113   auto f = p.getFuture()
114     .within(one_ms)
115     .onError([](TimedOut&) { return -1; });
116
117   EXPECT_EQ(-1, f.get());
118 }
119
120 TEST(Timekeeper, futureWithinAlreadyComplete) {
121   auto f = makeFuture(42)
122     .within(one_ms)
123     .onError([&](TimedOut&){ return -1; });
124
125   EXPECT_EQ(42, f.get());
126 }
127
128 TEST(Timekeeper, futureWithinFinishesInTime) {
129   Promise<int> p;
130   auto f = p.getFuture()
131     .within(std::chrono::minutes(1))
132     .onError([&](TimedOut&){ return -1; });
133   p.setValue(42);
134
135   EXPECT_EQ(42, f.get());
136 }
137
138 TEST(Timekeeper, futureWithinVoidSpecialization) {
139   makeFuture().within(one_ms);
140 }
141
142 TEST(Timekeeper, futureWithinException) {
143   Promise<Unit> p;
144   auto f = p.getFuture().within(awhile, std::runtime_error("expected"));
145   EXPECT_THROW(f.get(), std::runtime_error);
146 }
147
148 TEST(Timekeeper, onTimeout) {
149   bool flag = false;
150   makeFuture(42).delayed(10 * one_ms)
151     .onTimeout(zero_ms, [&]{ flag = true; return -1; })
152     .get();
153   EXPECT_TRUE(flag);
154 }
155
156 TEST(Timekeeper, onTimeoutComplete) {
157   bool flag = false;
158   makeFuture(42)
159     .onTimeout(zero_ms, [&]{ flag = true; return -1; })
160     .get();
161   EXPECT_FALSE(flag);
162 }
163
164 TEST(Timekeeper, onTimeoutReturnsFuture) {
165   bool flag = false;
166   makeFuture(42).delayed(10 * one_ms)
167     .onTimeout(zero_ms, [&]{ flag = true; return makeFuture(-1); })
168     .get();
169   EXPECT_TRUE(flag);
170 }
171
172 TEST(Timekeeper, onTimeoutVoid) {
173   makeFuture().delayed(one_ms)
174     .onTimeout(zero_ms, [&]{
175      });
176   makeFuture().delayed(one_ms)
177     .onTimeout(zero_ms, [&]{
178        return makeFuture<Unit>(std::runtime_error("expected"));
179      });
180   // just testing compilation here
181 }
182
183 TEST(Timekeeper, interruptDoesntCrash) {
184   auto f = futures::sleep(too_long);
185   f.cancel();
186 }
187
188 TEST(Timekeeper, chainedInterruptTest) {
189   bool test = false;
190   auto f = futures::sleep(milliseconds(100)).then([&](){
191     test = true;
192   });
193   f.cancel();
194   f.wait();
195   EXPECT_FALSE(test);
196 }
197
198 TEST(Timekeeper, executor) {
199   class ExecutorTester : public Executor {
200    public:
201     void add(Func f) override {
202       count++;
203       f();
204     }
205     std::atomic<int> count{0};
206   };
207
208   Promise<Unit> p;
209   ExecutorTester tester;
210   auto f = p.getFuture().via(&tester).within(one_ms).then([&](){});
211   p.setValue();
212   f.wait();
213   EXPECT_EQ(2, tester.count);
214 }
215
216 // TODO(5921764)
217 /*
218 TEST(Timekeeper, onTimeoutPropagates) {
219   bool flag = false;
220   EXPECT_THROW(
221     makeFuture(42).delayed(one_ms)
222       .onTimeout(zero_ms, [&]{ flag = true; })
223       .get(),
224     TimedOut);
225   EXPECT_TRUE(flag);
226 }
227 */
228
229 TEST_F(TimekeeperFixture, atBeforeNow) {
230   auto f = timeLord_->at(now() - too_long);
231   EXPECT_TRUE(f.isReady());
232   EXPECT_FALSE(f.hasException());
233 }
234
235 TEST_F(TimekeeperFixture, howToCastDuration) {
236   // I'm not sure whether this rounds up or down but it's irrelevant for the
237   // purpose of this example.
238   auto f = timeLord_->after(std::chrono::duration_cast<Duration>(
239       std::chrono::nanoseconds(1)));
240 }