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.
16 #include <gtest/gtest.h>
18 #include <folly/futures/Timekeeper.h>
21 using namespace folly;
22 using namespace std::chrono;
23 using folly::Timekeeper;
24 using Duration = folly::Duration;
26 std::chrono::milliseconds const one_ms(1);
27 std::chrono::milliseconds const awhile(10);
28 std::chrono::seconds const too_long(10);
30 std::chrono::steady_clock::time_point now() {
31 return std::chrono::steady_clock::now();
34 struct TimekeeperFixture : public testing::Test {
36 timeLord_(folly::detail::getTimekeeperSingleton())
39 Timekeeper* timeLord_;
42 TEST_F(TimekeeperFixture, after) {
46 auto f = timeLord_->after(awhile);
47 EXPECT_FALSE(f.isReady());
51 EXPECT_GE(t2 - t1, awhile);
54 TEST(Timekeeper, futureGet) {
56 std::thread([&]{ p.setValue(42); }).detach();
57 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(seconds(2)));
73 TEST(Timekeeper, futureGetTimeout) {
75 EXPECT_THROW(p.getFuture().get(Duration(1)), 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(Duration(0), [&]{ flag = true; return -1; })
139 TEST(Timekeeper, onTimeoutReturnsFuture) {
141 makeFuture(42).delayed(one_ms)
142 .onTimeout(Duration(0), [&]{ flag = true; return makeFuture(-1); })
147 TEST(Timekeeper, onTimeoutVoid) {
148 makeFuture().delayed(one_ms)
149 .onTimeout(Duration(0), [&]{
151 makeFuture().delayed(one_ms)
152 .onTimeout(Duration(0), [&]{
153 return makeFuture<void>(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(Duration(100)).then([&](){
174 TEST(Timekeeper, onTimeoutPropagates) {
177 makeFuture(42).delayed(one_ms)
178 .onTimeout(Duration(0), [&]{ flag = true; })