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/QueuedImmediateExecutor.h>
19 #include <folly/futures/Future.h>
20 #include <folly/futures/InlineExecutor.h>
21 #include <folly/futures/ManualExecutor.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); };
38 TEST(ManualExecutor, scheduleDur) {
41 std::chrono::milliseconds dur {10};
42 x.schedule([&]{ count++; }, dur);
52 TEST(ManualExecutor, laterThingsDontBlockEarlierOnes) {
55 std::chrono::milliseconds dur{10};
56 x.schedule([&] { first = true; }, dur);
57 x.schedule([] {}, 2 * dur);
63 TEST(ManualExecutor, orderWillNotBeQuestioned) {
67 std::chrono::milliseconds dur{10};
68 x.schedule([&] { first = true; }, dur);
69 x.schedule([&] { second = true; }, 2 * dur);
80 TEST(ManualExecutor, evenWhenYouSkipAheadEventsRunInProperOrder) {
85 std::chrono::milliseconds dur{10};
86 x.schedule([&] { first = ++counter; }, dur);
87 x.schedule([&] { second = ++counter; }, 2 * dur);
95 TEST(ManualExecutor, clockStartsAt0) {
97 EXPECT_EQ(x.now(), x.now().min());
100 TEST(ManualExecutor, scheduleAbs) {
103 x.scheduleAt([&]{ count++; }, x.now() + std::chrono::milliseconds(10));
105 x.advance(std::chrono::milliseconds(10));
109 TEST(ManualExecutor, advanceTo) {
112 x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now());
114 x.advanceTo(std::chrono::steady_clock::now());
118 TEST(ManualExecutor, advanceBack) {
121 x.advance(std::chrono::microseconds(5));
122 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
124 x.advanceTo(x.now() - std::chrono::microseconds(1));
128 TEST(ManualExecutor, advanceNeg) {
131 x.advance(std::chrono::microseconds(5));
132 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
134 x.advance(std::chrono::microseconds(-1));
138 TEST(ManualExecutor, waitForDoesNotDeadlock) {
139 ManualExecutor east, west;
140 folly::Baton<> baton;
141 auto f = makeFuture()
143 .then([](Try<Unit>){ return makeFuture(); })
154 TEST(ManualExecutor, getViaDoesNotDeadlock) {
155 ManualExecutor east, west;
156 folly::Baton<> baton;
157 auto f = makeFuture().via(&east).then([](Try<Unit>) {
169 TEST(ManualExecutor, clear) {
172 x.add([&] { ++count; });
173 x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10));
177 x.advance(std::chrono::milliseconds(10));
182 TEST(Executor, InlineExecutor) {
187 EXPECT_EQ(counter, 0);
190 EXPECT_EQ(counter, 1);
193 EXPECT_EQ(counter, 2);
196 TEST(Executor, QueuedImmediateExecutor) {
197 QueuedImmediateExecutor x;
201 EXPECT_EQ(1, counter);
204 EXPECT_EQ(0, counter);
207 EXPECT_EQ(2, counter);
210 TEST(Executor, Runnable) {
214 std::function<void()> fn;
215 void operator()() { fn(); }
218 f.fn = [&]{ counter++; };
220 EXPECT_EQ(counter, 1);
223 TEST(Executor, ThrowableThen) {
225 auto f = Future<Unit>().then([]() { throw std::runtime_error("Faildog"); });
228 auto f = Future<Unit>().via(&x).then([](){
229 throw std::runtime_error("Faildog");
231 EXPECT_THROW(f.value(), std::exception);
234 class CrappyExecutor : public Executor {
236 void add(Func /* f */) override { throw std::runtime_error("bad"); }
239 TEST(Executor, CrappyExecutor) {
242 auto f = folly::via(&x).onError([&](std::runtime_error& e) {
243 EXPECT_STREQ("bad", e.what());
249 class DoNothingExecutor : public Executor {
251 void add(Func f) override {
252 storedFunc_ = std::move(f);
259 TEST(Executor, DoNothingExecutor) {
262 // Submit future callback to DoNothingExecutor
263 auto f = folly::via(&x).then([] { return 42; });
265 // Callback function is stored in DoNothingExecutor, but not executed.
266 EXPECT_FALSE(f.isReady());
268 // Destroy the function stored in DoNothingExecutor. The future callback
269 // will never get executed.
272 EXPECT_TRUE(f.isReady());
273 EXPECT_THROW(f.get(), folly::BrokenPromise);