Home | History | Annotate | Download | only in test_tools
      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/tools/quic/test_tools/mock_epoll_server.h"
      6 
      7 namespace net {
      8 namespace tools {
      9 namespace test {
     10 
     11 FakeTimeEpollServer::FakeTimeEpollServer(): now_in_usec_(0) {
     12 }
     13 
     14 FakeTimeEpollServer::~FakeTimeEpollServer() {
     15 }
     16 
     17 int64 FakeTimeEpollServer::NowInUsec() const {
     18   return now_in_usec_;
     19 }
     20 
     21 MockEpollServer::MockEpollServer() : until_in_usec_(-1) {
     22 }
     23 
     24 MockEpollServer::~MockEpollServer() {
     25 }
     26 
     27 int MockEpollServer::epoll_wait_impl(int epfd,
     28                                      struct epoll_event* events,
     29                                      int max_events,
     30                                      int timeout_in_ms) {
     31   int num_events = 0;
     32   while (!event_queue_.empty() &&
     33          num_events < max_events &&
     34          event_queue_.begin()->first <= NowInUsec() &&
     35          ((until_in_usec_ == -1) ||
     36           (event_queue_.begin()->first < until_in_usec_))
     37         ) {
     38     int64 event_time_in_usec = event_queue_.begin()->first;
     39     events[num_events] = event_queue_.begin()->second;
     40     if (event_time_in_usec > NowInUsec()) {
     41       set_now_in_usec(event_time_in_usec);
     42     }
     43     event_queue_.erase(event_queue_.begin());
     44     ++num_events;
     45   }
     46   if (num_events == 0) {  // then we'd have waited 'till the timeout.
     47     if (until_in_usec_ < 0) {  // then we don't care what the final time is.
     48       if (timeout_in_ms > 0) {
     49         AdvanceBy(timeout_in_ms * 1000);
     50       }
     51     } else {  // except we assume that we don't wait for the timeout
     52       // period if until_in_usec_ is a positive number.
     53       set_now_in_usec(until_in_usec_);
     54       // And reset until_in_usec_ to signal no waiting (as
     55       // the AdvanceByExactly* stuff is meant to be one-shot,
     56       // as are all similar EpollServer functions)
     57       until_in_usec_ = -1;
     58     }
     59   }
     60   if (until_in_usec_ >= 0) {
     61     CHECK(until_in_usec_ >= NowInUsec());
     62   }
     63   return num_events;
     64 }
     65 
     66 }  // namespace test
     67 }  // namespace tools
     68 }  // namespace net
     69