2 * Copyright 2014 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>
18 #include <folly/wangle/InlineExecutor.h>
19 #include <folly/wangle/ManualExecutor.h>
20 #include <folly/wangle/QueuedImmediateExecutor.h>
21 #include <folly/wangle/Future.h>
22 #include <folly/Baton.h>
24 using namespace folly::wangle;
25 using namespace std::chrono;
26 using namespace testing;
28 TEST(ManualExecutor, runIsStable) {
31 auto f1 = [&]() { count++; };
32 auto f2 = [&]() { x.add(f1); x.add(f1); };
37 TEST(ManualExecutor, scheduleDur) {
40 milliseconds dur {10};
41 x.schedule([&]{ count++; }, dur);
51 TEST(ManualExecutor, clockStartsAt0) {
53 EXPECT_EQ(x.now(), x.now().min());
56 TEST(ManualExecutor, scheduleAbs) {
59 x.scheduleAt([&]{ count++; }, x.now() + milliseconds(10));
61 x.advance(milliseconds(10));
65 TEST(ManualExecutor, advanceTo) {
68 x.scheduleAt([&]{ count++; }, steady_clock::now());
70 x.advanceTo(steady_clock::now());
74 TEST(ManualExecutor, advanceBack) {
77 x.advance(microseconds(5));
78 x.schedule([&]{ count++; }, microseconds(6));
80 x.advanceTo(x.now() - microseconds(1));
84 TEST(ManualExecutor, advanceNeg) {
87 x.advance(microseconds(5));
88 x.schedule([&]{ count++; }, microseconds(6));
90 x.advance(microseconds(-1));
94 TEST(ManualExecutor, waitForDoesNotDeadlock) {
95 ManualExecutor east, west;
99 .then([](Try<void>){ return makeFuture(); })
110 TEST(Executor, InlineExecutor) {
115 EXPECT_EQ(counter++, 0);
117 EXPECT_EQ(counter++, 1);
119 EXPECT_EQ(counter, 2);
122 TEST(Executor, QueuedImmediateExecutor) {
123 QueuedImmediateExecutor x;
127 EXPECT_EQ(1, counter++);
129 EXPECT_EQ(0, counter++);
131 EXPECT_EQ(2, counter);
134 TEST(Executor, Runnable) {
138 std::function<void()> fn;
139 void operator()() { fn(); }
142 f.fn = [&]{ counter++; };
144 EXPECT_EQ(counter, 1);
147 TEST(Executor, RunnablePtr) {
150 std::function<void()> fn;
151 void operator()() { fn(); }
154 auto fnp = std::make_shared<Runnable>();
155 fnp->fn = [&]{ counter++; };
157 EXPECT_EQ(counter, 1);