1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/third_party/quiche/src/epoll_server/fake_simple_epoll_server.h"
6 
7 namespace epoll_server {
8 namespace test {
9 
FakeTimeSimpleEpollServer()10 FakeTimeSimpleEpollServer::FakeTimeSimpleEpollServer() : now_in_usec_(0) {}
11 
12 FakeTimeSimpleEpollServer::~FakeTimeSimpleEpollServer() = default;
13 
NowInUsec() const14 int64_t FakeTimeSimpleEpollServer::NowInUsec() const { return now_in_usec_; }
15 
FakeSimpleEpollServer()16 FakeSimpleEpollServer::FakeSimpleEpollServer() : until_in_usec_(-1) {}
17 
18 FakeSimpleEpollServer::~FakeSimpleEpollServer() = default;
19 
epoll_wait_impl(int epfd,struct epoll_event * events,int max_events,int timeout_in_ms)20 int FakeSimpleEpollServer::epoll_wait_impl(int epfd, struct epoll_event* events,
21                                            int max_events, int timeout_in_ms) {
22   int num_events = 0;
23   while (!event_queue_.empty() && num_events < max_events &&
24          event_queue_.begin()->first <= NowInUsec() &&
25          ((until_in_usec_ == -1) ||
26           (event_queue_.begin()->first < until_in_usec_))) {
27     int64_t event_time_in_usec = event_queue_.begin()->first;
28     events[num_events] = event_queue_.begin()->second;
29     if (event_time_in_usec > NowInUsec()) {
30       set_now_in_usec(event_time_in_usec);
31     }
32     event_queue_.erase(event_queue_.begin());
33     ++num_events;
34   }
35   if (num_events == 0) {       // then we'd have waited 'till the timeout.
36     if (until_in_usec_ < 0) {  // then we don't care what the final time is.
37       if (timeout_in_ms > 0) {
38         AdvanceBy(timeout_in_ms * 1000);
39       }
40     } else {  // except we assume that we don't wait for the timeout
41       // period if until_in_usec_ is a positive number.
42       set_now_in_usec(until_in_usec_);
43       // And reset until_in_usec_ to signal no waiting (as
44       // the AdvanceByExactly* stuff is meant to be one-shot,
45       // as are all similar net::EpollServer functions)
46       until_in_usec_ = -1;
47     }
48   }
49   if (until_in_usec_ >= 0) {
50     CHECK(until_in_usec_ >= NowInUsec());
51   }
52   return num_events;
53 }
54 
55 }  // namespace test
56 }  // namespace epoll_server
57