2 * Copyright 2016 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/Future.h>
20 #include <folly/futures/InlineExecutor.h>
21 #include <folly/futures/ManualExecutor.h>
22 #include <folly/futures/QueuedImmediateExecutor.h>
23 #include <folly/Baton.h>
25 using namespace folly;
27 TEST(ManualExecutor, runIsStable) {
30 auto f1 = [&]() { count++; };
31 auto f2 = [&]() { x.add(f1); x.add(f1); };
36 TEST(ManualExecutor, scheduleDur) {
39 std::chrono::milliseconds dur {10};
40 x.schedule([&]{ count++; }, dur);
50 TEST(ManualExecutor, clockStartsAt0) {
52 EXPECT_EQ(x.now(), x.now().min());
55 TEST(ManualExecutor, scheduleAbs) {
58 x.scheduleAt([&]{ count++; }, x.now() + std::chrono::milliseconds(10));
60 x.advance(std::chrono::milliseconds(10));
64 TEST(ManualExecutor, advanceTo) {
67 x.scheduleAt([&]{ count++; }, std::chrono::steady_clock::now());
69 x.advanceTo(std::chrono::steady_clock::now());
73 TEST(ManualExecutor, advanceBack) {
76 x.advance(std::chrono::microseconds(5));
77 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
79 x.advanceTo(x.now() - std::chrono::microseconds(1));
83 TEST(ManualExecutor, advanceNeg) {
86 x.advance(std::chrono::microseconds(5));
87 x.schedule([&]{ count++; }, std::chrono::microseconds(6));
89 x.advance(std::chrono::microseconds(-1));
93 TEST(ManualExecutor, waitForDoesNotDeadlock) {
94 ManualExecutor east, west;
98 .then([](Try<Unit>){ return makeFuture(); })
109 TEST(ManualExecutor, getViaDoesNotDeadlock) {
110 ManualExecutor east, west;
111 folly::Baton<> baton;
112 auto f = makeFuture().via(&east).then([](Try<Unit>) {
124 TEST(Executor, InlineExecutor) {
129 EXPECT_EQ(counter, 0);
132 EXPECT_EQ(counter, 1);
135 EXPECT_EQ(counter, 2);
138 TEST(Executor, QueuedImmediateExecutor) {
139 QueuedImmediateExecutor x;
143 EXPECT_EQ(1, counter);
146 EXPECT_EQ(0, counter);
149 EXPECT_EQ(2, counter);
152 TEST(Executor, Runnable) {
156 std::function<void()> fn;
157 void operator()() { fn(); }
160 f.fn = [&]{ counter++; };
162 EXPECT_EQ(counter, 1);
165 TEST(Executor, RunnablePtr) {
168 std::function<void()> fn;
169 void operator()() { fn(); }
172 auto fnp = std::make_shared<Runnable>();
173 fnp->fn = [&]{ counter++; };
175 EXPECT_EQ(counter, 1);
178 TEST(Executor, ThrowableThen) {
180 auto f = Future<Unit>().via(&x).then([](){
181 throw std::runtime_error("Faildog");
183 EXPECT_THROW(f.value(), std::exception);
186 class CrappyExecutor : public Executor {
188 void add(Func /* f */) override { throw std::runtime_error("bad"); }
191 TEST(Executor, CrappyExecutor) {
194 auto f = folly::via(&x).onError([&](std::runtime_error& e) {
195 EXPECT_STREQ("bad", e.what());