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