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