2017
[folly.git] / folly / test / TimeoutQueueTest.cpp
1 /*
2  * Copyright 2017 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 <folly/TimeoutQueue.h>
18
19 #include <folly/portability/GTest.h>
20
21 using namespace folly;
22
23 TEST(TimeoutQueue, Simple) {
24   typedef std::vector<TimeoutQueue::Id> EventVec;
25   EventVec events;
26
27   TimeoutQueue q;
28   TimeoutQueue::Callback cb = [&events](
29       TimeoutQueue::Id id, int64_t /* now */) { events.push_back(id); };
30
31   EXPECT_EQ(1, q.add(0, 10, cb));
32   EXPECT_EQ(2, q.add(0, 11, cb));
33   EXPECT_EQ(3, q.addRepeating(0, 9, cb));
34
35   EXPECT_TRUE(events.empty());
36   EXPECT_EQ(21, q.runOnce(12));  // now+9
37
38   bool r = (EventVec{3,1,2} == events);
39   EXPECT_TRUE(r);
40
41   events.clear();
42   EXPECT_EQ(49, q.runOnce(40));
43   r = (EventVec{3} == events);
44   EXPECT_TRUE(r);
45 }
46
47 TEST(TimeoutQueue, Erase) {
48   typedef std::vector<TimeoutQueue::Id> EventVec;
49   EventVec events;
50
51   TimeoutQueue q;
52   TimeoutQueue::Callback cb =
53       [&events, &q](TimeoutQueue::Id id, int64_t /* now */) {
54         events.push_back(id);
55         if (id == 2) {
56           q.erase(1);
57         }
58       };
59
60   EXPECT_EQ(1, q.addRepeating(0, 10, cb));
61   EXPECT_EQ(2, q.add(0, 35, cb));
62
63   int64_t now = 0;
64   while (now < std::numeric_limits<int64_t>::max()) {
65     now = q.runOnce(now);
66   }
67
68   bool r = (EventVec{1,1,1,2} == events);
69   EXPECT_TRUE(r);
70 }
71
72 TEST(TimeoutQueue, RunOnceRepeating) {
73   int count = 0;
74   TimeoutQueue q;
75   TimeoutQueue::Callback cb =
76       [&count, &q](TimeoutQueue::Id id, int64_t /* now */) {
77         if (++count == 100) {
78           EXPECT_TRUE(q.erase(id));
79         }
80       };
81
82   EXPECT_EQ(1, q.addRepeating(0, 0, cb));
83
84   EXPECT_EQ(0, q.runOnce(0));
85   EXPECT_EQ(1, count);
86   EXPECT_EQ(0, q.runOnce(0));
87   EXPECT_EQ(2, count);
88   EXPECT_EQ(std::numeric_limits<int64_t>::max(), q.runLoop(0));
89   EXPECT_EQ(100, count);
90 }
91
92 TEST(TimeoutQueue, RunOnceReschedule) {
93   int count = 0;
94   TimeoutQueue q;
95   TimeoutQueue::Callback cb;
96   cb = [&count, &q, &cb](TimeoutQueue::Id id, int64_t now) {
97       if (++count < 100) {
98         EXPECT_LT(id, q.add(now, 0, cb));
99       }
100     };
101
102   EXPECT_EQ(1, q.add(0, 0, cb));
103
104   EXPECT_EQ(0, q.runOnce(0));
105   EXPECT_EQ(1, count);
106   EXPECT_EQ(0, q.runOnce(0));
107   EXPECT_EQ(2, count);
108   EXPECT_EQ(std::numeric_limits<int64_t>::max(), q.runLoop(0));
109   EXPECT_EQ(100, count);
110 }