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/executors/InlineExecutor.h>
19 #include <folly/executors/ManualExecutor.h>
20 #include <folly/executors/QueuedImmediateExecutor.h>
21 #include <folly/futures/Future.h>
22 #include <folly/portability/GTest.h>
24 // TODO(jsedgwick) move this test to executors/test/ once the tested executors
27 using namespace folly;
29 TEST(ManualExecutor, runIsStable) {
32 auto f1 = [&]() { count++; };
33 auto f2 = [&]() { x.add(f1); x.add(f1); };
39 TEST(ManualExecutor, drainIsNotStable) {
42 auto f1 = [&]() { count++; };
52 TEST(ManualExecutor, scheduleDur) {
55 std::chrono::milliseconds dur {10};
56 x.schedule([&]{ count++; }, dur);
66 TEST(ManualExecutor, laterThingsDontBlockEarlierOnes) {
69 std::chrono::milliseconds dur{10};
70 x.schedule([&] { first = true; }, dur);
71 x.schedule([] {}, 2 * dur);
77 TEST(ManualExecutor, orderWillNotBeQuestioned) {
81 std::chrono::milliseconds dur{10};
82 x.schedule([&] { first = true; }, dur);
83 x.schedule([&] { second = true; }, 2 * dur);
94 TEST(ManualExecutor, evenWhenYouSkipAheadEventsRunInProperOrder) {
99 std::chrono::milliseconds dur{10};
100 x.schedule([&] { first = ++counter; }, dur);
101 x.schedule([&] { second = ++counter; }, 2 * dur);
103 EXPECT_EQ(second, 0);
106 EXPECT_EQ(second, 2);
109 TEST(ManualExecutor, clockStartsAt0) {
111 EXPECT_EQ(x.now(), x.now().min());
114 TEST(ManualExecutor, scheduleAbs) {
117 x.scheduleAt([&]{ count++; }, x.now() + std::chrono::milliseconds(10));
119 x.advance(std::chrono::milliseconds(10));
123 TEST(ManualExecutor, advanceTo) {
126 x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now());
128 x.advanceTo(std::chrono::steady_clock::now());
132 TEST(ManualExecutor, advanceBack) {
135 x.advance(std::chrono::microseconds(5));
136 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
138 x.advanceTo(x.now() - std::chrono::microseconds(1));
142 TEST(ManualExecutor, advanceNeg) {
145 x.advance(std::chrono::microseconds(5));
146 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
148 x.advance(std::chrono::microseconds(-1));
152 TEST(ManualExecutor, waitForDoesNotDeadlock) {
153 ManualExecutor east, west;
154 folly::Baton<> baton;
155 auto f = makeFuture()
157 .then([](Try<Unit>){ return makeFuture(); })
168 TEST(ManualExecutor, getViaDoesNotDeadlock) {
169 ManualExecutor east, west;
170 folly::Baton<> baton;
171 auto f = makeFuture().via(&east).then([](Try<Unit>) {
183 TEST(ManualExecutor, clear) {
186 x.add([&] { ++count; });
187 x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10));
191 x.advance(std::chrono::milliseconds(10));
196 TEST(Executor, InlineExecutor) {
201 EXPECT_EQ(counter, 0);
204 EXPECT_EQ(counter, 1);
207 EXPECT_EQ(counter, 2);
210 TEST(Executor, QueuedImmediateExecutor) {
211 QueuedImmediateExecutor x;
215 EXPECT_EQ(1, counter);
218 EXPECT_EQ(0, counter);
221 EXPECT_EQ(2, counter);
224 TEST(Executor, Runnable) {
228 std::function<void()> fn;
229 void operator()() { fn(); }
232 f.fn = [&]{ counter++; };
234 EXPECT_EQ(counter, 1);
237 TEST(Executor, ThrowableThen) {
239 auto f = Future<Unit>().then([]() { throw std::runtime_error("Faildog"); });
242 auto f = Future<Unit>().via(&x).then([](){
243 throw std::runtime_error("Faildog");
245 EXPECT_THROW(f.value(), std::exception);
248 class CrappyExecutor : public Executor {
250 void add(Func /* f */) override { throw std::runtime_error("bad"); }
253 TEST(Executor, CrappyExecutor) {
256 auto f = folly::via(&x).onError([&](std::runtime_error& e) {
257 EXPECT_STREQ("bad", e.what());
263 class DoNothingExecutor : public Executor {
265 void add(Func f) override {
266 storedFunc_ = std::move(f);
273 TEST(Executor, DoNothingExecutor) {
276 // Submit future callback to DoNothingExecutor
277 auto f = folly::via(&x).then([] { return 42; });
279 // Callback function is stored in DoNothingExecutor, but not executed.
280 EXPECT_FALSE(f.isReady());
282 // Destroy the function stored in DoNothingExecutor. The future callback
283 // will never get executed.
286 EXPECT_TRUE(f.isReady());
287 EXPECT_THROW(f.get(), folly::BrokenPromise);