/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <folly/futures/Timekeeper.h>
+#include <folly/Singleton.h>
+#include <folly/futures/ThreadWheelTimekeeper.h>
#include <folly/portability/GTest.h>
-#include <folly/portability/Unistd.h>
using namespace folly;
using std::chrono::milliseconds;
EXPECT_GE(now() - t1, one_ms);
}
+TEST(Timekeeper, futureSleepHandlesNullTimekeeperSingleton) {
+ Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
+ SCOPE_EXIT {
+ Singleton<ThreadWheelTimekeeper>::make_mock();
+ };
+ EXPECT_THROW(futures::sleep(one_ms).get(), NoTimekeeper);
+}
+
TEST(Timekeeper, futureDelayed) {
auto t1 = now();
auto dur = makeFuture()
EXPECT_TRUE(flag);
}
+TEST(Timekeeper, onTimeoutComplete) {
+ bool flag = false;
+ makeFuture(42)
+ .onTimeout(zero_ms, [&]{ flag = true; return -1; })
+ .get();
+ EXPECT_FALSE(flag);
+}
+
TEST(Timekeeper, onTimeoutReturnsFuture) {
bool flag = false;
makeFuture(42).delayed(10 * one_ms)
EXPECT_FALSE(test);
}
-namespace {
-class ExecutorTester : public Executor {
- public:
- void add(Func f) override {
- count++;
- f();
- }
- std::atomic<int> count{0};
+TEST(Timekeeper, executor) {
+ class ExecutorTester : public Executor {
+ public:
+ void add(Func f) override {
+ count++;
+ f();
+ }
+ std::atomic<int> count{0};
};
- }
-
- TEST(Timekeeper, executor) {
- auto f = makeFuture();
- ExecutorTester tester;
- f.via(&tester).within(one_ms).then([&]() {}).wait();
- EXPECT_EQ(2, tester.count);
- }
-
- TEST(Timekeeper, executorSameAfterDelayed) {
- ExecutorTester tester;
- // make sure we're using the same Executor (tester) after delayed returns
- // by comparing the executor count between invocations
- makeFuture()
- .via(&tester)
- .delayed(one_ms)
- .then([&]() { return tester.count.load(); })
- .then([&](int countBefore) {
- EXPECT_EQ(1, tester.count.load() - countBefore);
- })
- .wait();
+
+ Promise<Unit> p;
+ ExecutorTester tester;
+ auto f = p.getFuture().via(&tester).within(one_ms).then([&](){});
+ p.setValue();
+ f.wait();
+ EXPECT_EQ(2, tester.count);
}
// TODO(5921764)