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;
25 std::chrono::milliseconds const one_ms(1);
26 std::chrono::milliseconds const awhile(10);
27 std::chrono::seconds const too_long(10);
29 std::chrono::steady_clock::time_point now() {
30 return std::chrono::steady_clock::now();
33 struct TimekeeperFixture : public testing::Test {
35 timeLord_(folly::detail::getTimekeeperSingleton())
38 Timekeeper* timeLord_;
41 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 std::thread([&]{ p.setValue(42); }).detach();
56 EXPECT_EQ(42, p.getFuture().get());
59 TEST(Timekeeper, futureGetBeforeTimeout) {
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)));
72 TEST(Timekeeper, futureGetTimeout) {
74 EXPECT_THROW(p.getFuture().get(Duration(1)), folly::TimedOut);
77 TEST(Timekeeper, futureSleep) {
79 futures::sleep(one_ms).get();
80 EXPECT_GE(now() - t1, one_ms);
83 TEST(Timekeeper, futureDelayed) {
85 auto dur = makeFuture()
87 .then([=]{ return now() - t1; })
90 EXPECT_GE(dur, one_ms);
93 TEST(Timekeeper, futureWithinThrows) {
95 auto f = p.getFuture()
97 .onError([](TimedOut&) { return -1; });
99 EXPECT_EQ(-1, f.get());
102 TEST(Timekeeper, futureWithinAlreadyComplete) {
103 auto f = makeFuture(42)
105 .onError([&](TimedOut&){ return -1; });
107 EXPECT_EQ(42, f.get());
110 TEST(Timekeeper, futureWithinFinishesInTime) {
112 auto f = p.getFuture()
113 .within(std::chrono::minutes(1))
114 .onError([&](TimedOut&){ return -1; });
117 EXPECT_EQ(42, f.get());
120 TEST(Timekeeper, futureWithinVoidSpecialization) {
121 makeFuture().within(one_ms);
124 TEST(Timekeeper, futureWithinException) {
126 auto f = p.getFuture().within(awhile, std::runtime_error("expected"));
127 EXPECT_THROW(f.get(), std::runtime_error);
130 TEST(Timekeeper, onTimeout) {
132 makeFuture(42).delayed(one_ms)
133 .onTimeout(Duration(0), [&]{ flag = true; return -1; })
138 TEST(Timekeeper, onTimeoutReturnsFuture) {
140 makeFuture(42).delayed(one_ms)
141 .onTimeout(Duration(0), [&]{ flag = true; return makeFuture(-1); })
146 TEST(Timekeeper, onTimeoutVoid) {
147 makeFuture().delayed(one_ms)
148 .onTimeout(Duration(0), [&]{
150 makeFuture().delayed(one_ms)
151 .onTimeout(Duration(0), [&]{
152 return makeFuture<void>(std::runtime_error("expected"));
154 // just testing compilation here
157 TEST(Timekeeper, interruptDoesntCrash) {
158 auto f = futures::sleep(too_long);
162 TEST(Timekeeper, chainedInterruptTest) {
164 auto f = futures::sleep(Duration(100)).then([&](){
172 TEST(Timekeeper, executor) {
173 class ExecutorTester : public Executor {
175 void add(Func f) override {
179 std::atomic<int> count{0};
182 auto f = makeFuture();
183 ExecutorTester tester;
184 f.via(&tester).within(std::chrono::milliseconds(1)).then([&](){}).wait();
185 EXPECT_EQ(2, tester.count);
189 TEST(Timekeeper, onTimeoutPropagates) {
192 makeFuture(42).delayed(one_ms)
193 .onTimeout(Duration(0), [&]{ flag = true; })