2 * Copyright 2017 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 <folly/futures/Timekeeper.h>
18 #include <folly/portability/GTest.h>
20 using namespace folly;
21 using std::chrono::milliseconds;
23 std::chrono::milliseconds const zero_ms(0);
24 std::chrono::milliseconds const one_ms(1);
25 std::chrono::milliseconds const awhile(10);
26 std::chrono::seconds const too_long(10);
28 std::chrono::steady_clock::time_point now() {
29 return std::chrono::steady_clock::now();
32 struct TimekeeperFixture : public testing::Test {
34 timeLord_(folly::detail::getTimekeeperSingleton())
37 std::shared_ptr<Timekeeper> timeLord_;
40 TEST_F(TimekeeperFixture, after) {
42 auto f = timeLord_->after(awhile);
43 EXPECT_FALSE(f.isReady());
47 EXPECT_GE(t2 - t1, awhile);
50 TEST(Timekeeper, futureGet) {
52 auto t = std::thread([&]{ p.setValue(42); });
53 EXPECT_EQ(42, p.getFuture().get());
57 TEST(Timekeeper, futureGetBeforeTimeout) {
59 auto t = std::thread([&]{ p.setValue(42); });
60 // Technically this is a race and if the test server is REALLY overloaded
61 // and it takes more than a second to do that thread it could be flaky. But
62 // I want a low timeout (in human terms) so if this regresses and someone
63 // runs it by hand they're not sitting there forever wondering why it's
64 // blocked, and get a useful error message instead. If it does get flaky,
65 // empirically increase the timeout to the point where it's very improbable.
66 EXPECT_EQ(42, p.getFuture().get(std::chrono::seconds(2)));
70 TEST(Timekeeper, futureGetTimeout) {
72 EXPECT_THROW(p.getFuture().get(one_ms), folly::TimedOut);
75 TEST(Timekeeper, futureSleep) {
77 futures::sleep(one_ms).get();
78 EXPECT_GE(now() - t1, one_ms);
81 TEST(Timekeeper, futureDelayed) {
83 auto dur = makeFuture()
85 .then([=]{ return now() - t1; })
88 EXPECT_GE(dur, one_ms);
91 TEST(Timekeeper, futureWithinThrows) {
93 auto f = p.getFuture()
95 .onError([](TimedOut&) { return -1; });
97 EXPECT_EQ(-1, f.get());
100 TEST(Timekeeper, futureWithinAlreadyComplete) {
101 auto f = makeFuture(42)
103 .onError([&](TimedOut&){ return -1; });
105 EXPECT_EQ(42, f.get());
108 TEST(Timekeeper, futureWithinFinishesInTime) {
110 auto f = p.getFuture()
111 .within(std::chrono::minutes(1))
112 .onError([&](TimedOut&){ return -1; });
115 EXPECT_EQ(42, f.get());
118 TEST(Timekeeper, futureWithinVoidSpecialization) {
119 makeFuture().within(one_ms);
122 TEST(Timekeeper, futureWithinException) {
124 auto f = p.getFuture().within(awhile, std::runtime_error("expected"));
125 EXPECT_THROW(f.get(), std::runtime_error);
128 TEST(Timekeeper, onTimeout) {
130 makeFuture(42).delayed(10 * one_ms)
131 .onTimeout(zero_ms, [&]{ flag = true; return -1; })
136 TEST(Timekeeper, onTimeoutReturnsFuture) {
138 makeFuture(42).delayed(10 * one_ms)
139 .onTimeout(zero_ms, [&]{ flag = true; return makeFuture(-1); })
144 TEST(Timekeeper, onTimeoutVoid) {
145 makeFuture().delayed(one_ms)
146 .onTimeout(zero_ms, [&]{
148 makeFuture().delayed(one_ms)
149 .onTimeout(zero_ms, [&]{
150 return makeFuture<Unit>(std::runtime_error("expected"));
152 // just testing compilation here
155 TEST(Timekeeper, interruptDoesntCrash) {
156 auto f = futures::sleep(too_long);
160 TEST(Timekeeper, chainedInterruptTest) {
162 auto f = futures::sleep(milliseconds(100)).then([&](){
170 TEST(Timekeeper, executor) {
171 class ExecutorTester : public Executor {
173 void add(Func f) override {
177 std::atomic<int> count{0};
180 auto f = makeFuture();
181 ExecutorTester tester;
182 f.via(&tester).within(one_ms).then([&](){}).wait();
183 EXPECT_EQ(2, tester.count);
188 TEST(Timekeeper, onTimeoutPropagates) {
191 makeFuture(42).delayed(one_ms)
192 .onTimeout(zero_ms, [&]{ flag = true; })
199 TEST_F(TimekeeperFixture, atBeforeNow) {
200 auto f = timeLord_->at(now() - too_long);
201 EXPECT_TRUE(f.isReady());
202 EXPECT_FALSE(f.hasException());
205 TEST_F(TimekeeperFixture, howToCastDuration) {
206 // I'm not sure whether this rounds up or down but it's irrelevant for the
207 // purpose of this example.
208 auto f = timeLord_->after(std::chrono::duration_cast<Duration>(
209 std::chrono::nanoseconds(1)));