Codemod: use #include angle brackets in folly and thrift
[folly.git] / folly / wangle / test / ExecutorTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <gtest/gtest.h>
18 #include <folly/wangle/InlineExecutor.h>
19 #include <folly/wangle/ManualExecutor.h>
20 #include <folly/wangle/QueuedImmediateExecutor.h>
21
22 using namespace folly::wangle;
23 using namespace std::chrono;
24 using namespace testing;
25
26 TEST(ManualExecutor, runIsStable) {
27   ManualExecutor x;
28   size_t count = 0;
29   auto f1 = [&]() { count++; };
30   auto f2 = [&]() { x.add(f1); x.add(f1); };
31   x.add(f2);
32   x.run();
33 }
34
35 TEST(ManualExecutor, scheduleDur) {
36   ManualExecutor x;
37   size_t count = 0;
38   milliseconds dur {10};
39   x.schedule([&]{ count++; }, dur);
40   EXPECT_EQ(count, 0);
41   x.run();
42   EXPECT_EQ(count, 0);
43   x.advance(dur/2);
44   EXPECT_EQ(count, 0);
45   x.advance(dur/2);
46   EXPECT_EQ(count, 1);
47 }
48
49 TEST(ManualExecutor, clockStartsAt0) {
50   ManualExecutor x;
51   EXPECT_EQ(x.now(), x.now().min());
52 }
53
54 TEST(ManualExecutor, scheduleAbs) {
55   ManualExecutor x;
56   size_t count = 0;
57   x.scheduleAt([&]{ count++; }, x.now() + milliseconds(10));
58   EXPECT_EQ(count, 0);
59   x.advance(milliseconds(10));
60   EXPECT_EQ(count, 1);
61 }
62
63 TEST(ManualExecutor, advanceTo) {
64   ManualExecutor x;
65   size_t count = 0;
66   x.scheduleAt([&]{ count++; }, steady_clock::now());
67   EXPECT_EQ(count, 0);
68   x.advanceTo(steady_clock::now());
69   EXPECT_EQ(count, 1);
70 }
71
72 TEST(ManualExecutor, advanceBack) {
73   ManualExecutor x;
74   size_t count = 0;
75   x.advance(microseconds(5));
76   x.schedule([&]{ count++; }, microseconds(6));
77   EXPECT_EQ(count, 0);
78   x.advanceTo(x.now() - microseconds(1));
79   EXPECT_EQ(count, 0);
80 }
81
82 TEST(ManualExecutor, advanceNeg) {
83   ManualExecutor x;
84   size_t count = 0;
85   x.advance(microseconds(5));
86   x.schedule([&]{ count++; }, microseconds(6));
87   EXPECT_EQ(count, 0);
88   x.advance(microseconds(-1));
89   EXPECT_EQ(count, 0);
90 }
91
92 TEST(Executor, InlineExecutor) {
93   InlineExecutor x;
94   size_t counter = 0;
95   x.add([&]{
96     x.add([&]{
97       EXPECT_EQ(counter++, 0);
98     });
99     EXPECT_EQ(counter++, 1);
100   });
101   EXPECT_EQ(counter, 2);
102 }
103
104 TEST(Executor, QueuedImmediateExecutor) {
105   QueuedImmediateExecutor x;
106   size_t counter = 0;
107   x.add([&]{
108     x.add([&]{
109       EXPECT_EQ(1, counter++);
110     });
111     EXPECT_EQ(0, counter++);
112   });
113   EXPECT_EQ(2, counter);
114 }
115
116 TEST(Executor, Runnable) {
117   InlineExecutor x;
118   size_t counter = 0;
119   struct Runnable {
120     std::function<void()> fn;
121     void operator()() { fn(); }
122   };
123   Runnable f;
124   f.fn = [&]{ counter++; };
125   x.add(f);
126   EXPECT_EQ(counter, 1);
127 }
128
129 TEST(Executor, RunnablePtr) {
130   InlineExecutor x;
131   struct Runnable {
132     std::function<void()> fn;
133     void operator()() { fn(); }
134   };
135   size_t counter = 0;
136   auto fnp = std::make_shared<Runnable>();
137   fnp->fn = [&]{ counter++; };
138   x.addPtr(fnp);
139   EXPECT_EQ(counter, 1);
140 }