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/Baton.h>
18 #include <folly/futures/Future.h>
19 #include <folly/futures/InlineExecutor.h>
20 #include <folly/futures/ManualExecutor.h>
21 #include <folly/futures/QueuedImmediateExecutor.h>
22 #include <folly/portability/GTest.h>
24 using namespace folly;
26 TEST(ManualExecutor, runIsStable) {
29 auto f1 = [&]() { count++; };
30 auto f2 = [&]() { x.add(f1); x.add(f1); };
35 TEST(ManualExecutor, scheduleDur) {
38 std::chrono::milliseconds dur {10};
39 x.schedule([&]{ count++; }, dur);
49 TEST(ManualExecutor, laterThingsDontBlockEarlierOnes) {
52 std::chrono::milliseconds dur{10};
53 x.schedule([&] { first = true; }, dur);
54 x.schedule([] {}, 2 * dur);
60 TEST(ManualExecutor, orderWillNotBeQuestioned) {
64 std::chrono::milliseconds dur{10};
65 x.schedule([&] { first = true; }, dur);
66 x.schedule([&] { second = true; }, 2 * dur);
77 TEST(ManualExecutor, evenWhenYouSkipAheadEventsRunInProperOrder) {
82 std::chrono::milliseconds dur{10};
83 x.schedule([&] { first = ++counter; }, dur);
84 x.schedule([&] { second = ++counter; }, 2 * dur);
92 TEST(ManualExecutor, clockStartsAt0) {
94 EXPECT_EQ(x.now(), x.now().min());
97 TEST(ManualExecutor, scheduleAbs) {
100 x.scheduleAt([&]{ count++; }, x.now() + std::chrono::milliseconds(10));
102 x.advance(std::chrono::milliseconds(10));
106 TEST(ManualExecutor, advanceTo) {
109 x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now());
111 x.advanceTo(std::chrono::steady_clock::now());
115 TEST(ManualExecutor, advanceBack) {
118 x.advance(std::chrono::microseconds(5));
119 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
121 x.advanceTo(x.now() - std::chrono::microseconds(1));
125 TEST(ManualExecutor, advanceNeg) {
128 x.advance(std::chrono::microseconds(5));
129 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
131 x.advance(std::chrono::microseconds(-1));
135 TEST(ManualExecutor, waitForDoesNotDeadlock) {
136 ManualExecutor east, west;
137 folly::Baton<> baton;
138 auto f = makeFuture()
140 .then([](Try<Unit>){ return makeFuture(); })
151 TEST(ManualExecutor, getViaDoesNotDeadlock) {
152 ManualExecutor east, west;
153 folly::Baton<> baton;
154 auto f = makeFuture().via(&east).then([](Try<Unit>) {
166 TEST(ManualExecutor, clear) {
169 x.add([&] { ++count; });
170 x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10));
174 x.advance(std::chrono::milliseconds(10));
179 TEST(Executor, InlineExecutor) {
184 EXPECT_EQ(counter, 0);
187 EXPECT_EQ(counter, 1);
190 EXPECT_EQ(counter, 2);
193 TEST(Executor, QueuedImmediateExecutor) {
194 QueuedImmediateExecutor x;
198 EXPECT_EQ(1, counter);
201 EXPECT_EQ(0, counter);
204 EXPECT_EQ(2, counter);
207 TEST(Executor, Runnable) {
211 std::function<void()> fn;
212 void operator()() { fn(); }
215 f.fn = [&]{ counter++; };
217 EXPECT_EQ(counter, 1);
220 TEST(Executor, RunnablePtr) {
223 std::function<void()> fn;
224 void operator()() { fn(); }
227 auto fnp = std::make_shared<Runnable>();
228 fnp->fn = [&]{ counter++; };
230 EXPECT_EQ(counter, 1);
233 TEST(Executor, ThrowableThen) {
235 auto f = Future<Unit>().then([]() { throw std::runtime_error("Faildog"); });
238 auto f = Future<Unit>().via(&x).then([](){
239 throw std::runtime_error("Faildog");
241 EXPECT_THROW(f.value(), std::exception);
244 class CrappyExecutor : public Executor {
246 void add(Func /* f */) override { throw std::runtime_error("bad"); }
249 TEST(Executor, CrappyExecutor) {
252 auto f = folly::via(&x).onError([&](std::runtime_error& e) {
253 EXPECT_STREQ("bad", e.what());
259 class DoNothingExecutor : public Executor {
261 void add(Func f) override {
262 storedFunc_ = std::move(f);
269 TEST(Executor, DoNothingExecutor) {
272 // Submit future callback to DoNothingExecutor
273 auto f = folly::via(&x).then([] { return 42; });
275 // Callback function is stored in DoNothingExecutor, but not executed.
276 EXPECT_FALSE(f.isReady());
278 // Destroy the function stored in DoNothingExecutor. The future callback
279 // will never get executed.
282 EXPECT_TRUE(f.isReady());
283 EXPECT_THROW(f.get(), folly::BrokenPromise);