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