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/TimeoutQueue.h>
20 using namespace folly;
22 TEST(TimeoutQueue, Simple) {
23 typedef std::vector<TimeoutQueue::Id> EventVec;
27 TimeoutQueue::Callback cb =
28 [&events](TimeoutQueue::Id id, int64_t now) {
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));
36 EXPECT_TRUE(events.empty());
37 EXPECT_EQ(21, q.runOnce(12)); // now+9
39 bool r = (EventVec{3,1,2} == events);
43 EXPECT_EQ(49, q.runOnce(40));
44 r = (EventVec{3} == events);
48 TEST(TimeoutQueue, Erase) {
49 typedef std::vector<TimeoutQueue::Id> EventVec;
53 TimeoutQueue::Callback cb =
54 [&events, &q](TimeoutQueue::Id id, int64_t now) {
61 EXPECT_EQ(1, q.addRepeating(0, 10, cb));
62 EXPECT_EQ(2, q.add(0, 35, cb));
65 while (now < std::numeric_limits<int64_t>::max()) {
69 bool r = (EventVec{1,1,1,2} == events);
73 TEST(TimeoutQueue, RunOnceRepeating) {
76 TimeoutQueue::Callback cb =
77 [&count, &q](TimeoutQueue::Id id, int64_t now) {
79 EXPECT_TRUE(q.erase(id));
83 EXPECT_EQ(1, q.addRepeating(0, 0, cb));
85 EXPECT_EQ(0, q.runOnce(0));
87 EXPECT_EQ(0, q.runOnce(0));
89 EXPECT_EQ(std::numeric_limits<int64_t>::max(), q.runLoop(0));
90 EXPECT_EQ(100, count);
93 TEST(TimeoutQueue, RunOnceReschedule) {
96 TimeoutQueue::Callback cb;
97 cb = [&count, &q, &cb](TimeoutQueue::Id id, int64_t now) {
99 EXPECT_LT(id, q.add(now, 0, cb));
103 EXPECT_EQ(1, q.add(0, 0, cb));
106 EXPECT_EQ(0, q.runOnce(0));
108 EXPECT_EQ(0, q.runOnce(0));
110 EXPECT_EQ(std::numeric_limits<int64_t>::max(), q.runLoop(0));
111 EXPECT_EQ(100, count);