TEST(TimeoutQueue, Simple) { typedef std::vector<TimeoutQueue::Id> EventVec; EventVec events; TimeoutQueue q; TimeoutQueue::Callback cb = [&events](TimeoutQueue::Id id, int64_t now) { events.push_back(id); }; EXPECT_EQ(1, q.add(0, 10, cb)); EXPECT_EQ(2, q.add(0, 11, cb)); EXPECT_EQ(3, q.addRepeating(0, 9, cb)); EXPECT_TRUE(events.empty()); EXPECT_EQ(21, q.runOnce(12)); // now+9 bool r = (EventVec{3,1,2} == events); EXPECT_TRUE(r); events.clear(); EXPECT_EQ(49, q.runOnce(40)); r = (EventVec{3} == events); EXPECT_TRUE(r); }
int EPoller::poll(EventVec& activevec){ int num = epoll_wait(epollfd, &(*fdvec.begin()), MAX_EVENTS, -1); for(int i=0;i<num;i++){ void* ptr = fdvec[i].data.ptr; Event* ev = static_cast<Event*>(ptr); activevec.push_back(ev); } return num; }
TEST(TimeoutQueue, Erase) { typedef std::vector<TimeoutQueue::Id> EventVec; EventVec events; TimeoutQueue q; TimeoutQueue::Callback cb = [&events, &q](TimeoutQueue::Id id, int64_t now) { events.push_back(id); if (id == 2) { q.erase(1); } }; EXPECT_EQ(1, q.addRepeating(0, 10, cb)); EXPECT_EQ(2, q.add(0, 35, cb)); int64_t now = 0; while (now < std::numeric_limits<int64_t>::max()) { now = q.runOnce(now); } bool r = (EventVec{1,1,1,2} == events); EXPECT_TRUE(r); }