proxygen
TimeoutQueueTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (TimeoutQueue, Simple)
 
 TEST (TimeoutQueue, Erase)
 
 TEST (TimeoutQueue, RunOnceRepeating)
 
 TEST (TimeoutQueue, RunOnceReschedule)
 

Function Documentation

TEST ( TimeoutQueue  ,
Simple   
)

Definition at line 23 of file TimeoutQueueTest.cpp.

References folly::TimeoutQueue::add(), folly::TimeoutQueue::addRepeating(), EXPECT_EQ, EXPECT_TRUE, int64_t, and folly::TimeoutQueue::runOnce().

23  {
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 */) {
30  events.push_back(id);
31  };
32 
33  EXPECT_EQ(1, q.add(0, 10, cb));
34  EXPECT_EQ(2, q.add(0, 11, cb));
35  EXPECT_EQ(3, q.addRepeating(0, 9, cb));
36 
37  EXPECT_TRUE(events.empty());
38  EXPECT_EQ(21, q.runOnce(12)); // now+9
39 
40  bool r = (EventVec{3, 1, 2} == events);
41  EXPECT_TRUE(r);
42 
43  events.clear();
44  EXPECT_EQ(49, q.runOnce(40));
45  r = (EventVec{3} == events);
46  EXPECT_TRUE(r);
47 }
int64_t runOnce(int64_t now)
Definition: TimeoutQueue.h:87
Id add(int64_t now, int64_t delay, Callback callback)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::function< void(Id, int64_t)> Callback
Definition: TimeoutQueue.h:43
Id addRepeating(int64_t now, int64_t interval, Callback callback)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( TimeoutQueue  ,
Erase   
)

Definition at line 49 of file TimeoutQueueTest.cpp.

References folly::TimeoutQueue::add(), folly::TimeoutQueue::addRepeating(), folly::TimeoutQueue::erase(), EXPECT_EQ, EXPECT_TRUE, int64_t, max, now(), and folly::TimeoutQueue::runOnce().

49  {
50  typedef std::vector<TimeoutQueue::Id> EventVec;
51  EventVec events;
52 
53  TimeoutQueue q;
54  TimeoutQueue::Callback cb = [&events, &q](
55  TimeoutQueue::Id id, int64_t /* now */) {
56  events.push_back(id);
57  if (id == 2) {
58  q.erase(1);
59  }
60  };
61 
62  EXPECT_EQ(1, q.addRepeating(0, 10, cb));
63  EXPECT_EQ(2, q.add(0, 35, cb));
64 
65  int64_t now = 0;
66  while (now < std::numeric_limits<int64_t>::max()) {
67  now = q.runOnce(now);
68  }
69 
70  bool r = (EventVec{1, 1, 1, 2} == events);
71  EXPECT_TRUE(r);
72 }
int64_t runOnce(int64_t now)
Definition: TimeoutQueue.h:87
LogLevel max
Definition: LogLevel.cpp:31
Id add(int64_t now, int64_t delay, Callback callback)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::chrono::steady_clock::time_point now()
std::function< void(Id, int64_t)> Callback
Definition: TimeoutQueue.h:43
Id addRepeating(int64_t now, int64_t interval, Callback callback)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( TimeoutQueue  ,
RunOnceRepeating   
)

Definition at line 74 of file TimeoutQueueTest.cpp.

References folly::TimeoutQueue::addRepeating(), count, folly::TimeoutQueue::erase(), EXPECT_EQ, EXPECT_TRUE, int64_t, max, folly::TimeoutQueue::runLoop(), and folly::TimeoutQueue::runOnce().

74  {
75  int count = 0;
76  TimeoutQueue q;
77  TimeoutQueue::Callback cb = [&count, &q](
78  TimeoutQueue::Id id, int64_t /* now */) {
79  if (++count == 100) {
80  EXPECT_TRUE(q.erase(id));
81  }
82  };
83 
84  EXPECT_EQ(1, q.addRepeating(0, 0, cb));
85 
86  EXPECT_EQ(0, q.runOnce(0));
87  EXPECT_EQ(1, count);
88  EXPECT_EQ(0, q.runOnce(0));
89  EXPECT_EQ(2, count);
91  EXPECT_EQ(100, count);
92 }
int64_t runOnce(int64_t now)
Definition: TimeoutQueue.h:87
int64_t runLoop(int64_t now)
Definition: TimeoutQueue.h:90
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::function< void(Id, int64_t)> Callback
Definition: TimeoutQueue.h:43
Id addRepeating(int64_t now, int64_t interval, Callback callback)
int * count
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( TimeoutQueue  ,
RunOnceReschedule   
)

Definition at line 94 of file TimeoutQueueTest.cpp.

References count, EXPECT_EQ, EXPECT_LT, int64_t, max, and now().

94  {
95  int count = 0;
96  TimeoutQueue q;
98  cb = [&count, &q, &cb](TimeoutQueue::Id id, int64_t now) {
99  if (++count < 100) {
100  EXPECT_LT(id, q.add(now, 0, cb));
101  }
102  };
103 
104  EXPECT_EQ(1, q.add(0, 0, cb));
105 
106  EXPECT_EQ(0, q.runOnce(0));
107  EXPECT_EQ(1, count);
108  EXPECT_EQ(0, q.runOnce(0));
109  EXPECT_EQ(2, count);
111  EXPECT_EQ(100, count);
112 }
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::chrono::steady_clock::time_point now()
std::function< void(Id, int64_t)> Callback
Definition: TimeoutQueue.h:43
int * count
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930