2 * Copyright 2015 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 <gtest/gtest.h>
19 #include <folly/futures/Timekeeper.h>
23 using namespace folly;
24 using std::chrono::milliseconds;
26 std::chrono::milliseconds const zero_ms(0);
27 std::chrono::milliseconds const one_ms(1);
28 std::chrono::milliseconds const awhile(10);
29 std::chrono::seconds const too_long(10);
31 std::chrono::steady_clock::time_point now() {
32 return std::chrono::steady_clock::now();
35 struct TimekeeperFixture : public testing::Test {
37 timeLord_(folly::detail::getTimekeeperSingleton())
40 Timekeeper* timeLord_;
43 TEST_F(TimekeeperFixture, after) {
45 auto f = timeLord_->after(awhile);
46 EXPECT_FALSE(f.isReady());
50 EXPECT_GE(t2 - t1, awhile);
53 TEST(Timekeeper, futureGet) {
55 auto t = std::thread([&]{ p.setValue(42); });
56 EXPECT_EQ(42, p.getFuture().get());
60 TEST(Timekeeper, futureGetBeforeTimeout) {
62 auto t = std::thread([&]{ p.setValue(42); });
63 // Technically this is a race and if the test server is REALLY overloaded
64 // and it takes more than a second to do that thread it could be flaky. But
65 // I want a low timeout (in human terms) so if this regresses and someone
66 // runs it by hand they're not sitting there forever wondering why it's
67 // blocked, and get a useful error message instead. If it does get flaky,
68 // empirically increase the timeout to the point where it's very improbable.
69 EXPECT_EQ(42, p.getFuture().get(std::chrono::seconds(2)));
73 TEST(Timekeeper, futureGetTimeout) {
75 EXPECT_THROW(p.getFuture().get(one_ms), folly::TimedOut);
78 TEST(Timekeeper, futureSleep) {
80 futures::sleep(one_ms).get();
81 EXPECT_GE(now() - t1, one_ms);
84 TEST(Timekeeper, futureDelayed) {
86 auto dur = makeFuture()
88 .then([=]{ return now() - t1; })
91 EXPECT_GE(dur, one_ms);
94 TEST(Timekeeper, futureWithinThrows) {
96 auto f = p.getFuture()
98 .onError([](TimedOut&) { return -1; });
100 EXPECT_EQ(-1, f.get());
103 TEST(Timekeeper, futureWithinAlreadyComplete) {
104 auto f = makeFuture(42)
106 .onError([&](TimedOut&){ return -1; });
108 EXPECT_EQ(42, f.get());
111 TEST(Timekeeper, futureWithinFinishesInTime) {
113 auto f = p.getFuture()
114 .within(std::chrono::minutes(1))
115 .onError([&](TimedOut&){ return -1; });
118 EXPECT_EQ(42, f.get());
121 TEST(Timekeeper, futureWithinVoidSpecialization) {
122 makeFuture().within(one_ms);
125 TEST(Timekeeper, futureWithinException) {
127 auto f = p.getFuture().within(awhile, std::runtime_error("expected"));
128 EXPECT_THROW(f.get(), std::runtime_error);
131 TEST(Timekeeper, onTimeout) {
133 makeFuture(42).delayed(one_ms)
134 .onTimeout(zero_ms, [&]{ flag = true; return -1; })
139 TEST(Timekeeper, onTimeoutReturnsFuture) {
141 makeFuture(42).delayed(one_ms)
142 .onTimeout(zero_ms, [&]{ flag = true; return makeFuture(-1); })
147 TEST(Timekeeper, onTimeoutVoid) {
148 makeFuture().delayed(one_ms)
149 .onTimeout(zero_ms, [&]{
151 makeFuture().delayed(one_ms)
152 .onTimeout(zero_ms, [&]{
153 return makeFuture<Unit>(std::runtime_error("expected"));
155 // just testing compilation here
158 TEST(Timekeeper, interruptDoesntCrash) {
159 auto f = futures::sleep(too_long);
163 TEST(Timekeeper, chainedInterruptTest) {
165 auto f = futures::sleep(milliseconds(100)).then([&](){
173 TEST(Timekeeper, executor) {
174 class ExecutorTester : public Executor {
176 void add(Func f) override {
180 std::atomic<int> count{0};
183 auto f = makeFuture();
184 ExecutorTester tester;
185 f.via(&tester).within(one_ms).then([&](){}).wait();
186 EXPECT_EQ(2, tester.count);
191 TEST(Timekeeper, onTimeoutPropagates) {
194 makeFuture(42).delayed(one_ms)
195 .onTimeout(zero_ms, [&]{ flag = true; })