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#ifndef NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ 6#define NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ 7 8#include "base/logging.h" 9#include "net/tools/flip_server/epoll_server.h" 10#include "testing/gmock/include/gmock/gmock.h" 11 12namespace net { 13namespace tools { 14namespace test { 15 16// Unlike the full MockEpollServer, this only lies about the time but lets 17// fd events operate normally. Usefully when interacting with real backends 18// but wanting to skip forward in time to trigger timeouts. 19class FakeTimeEpollServer : public EpollServer { 20 public: 21 FakeTimeEpollServer(); 22 virtual ~FakeTimeEpollServer(); 23 24 // Replaces the EpollServer NowInUsec. 25 virtual int64 NowInUsec() const OVERRIDE; 26 27 void set_now_in_usec(int64 nius) { now_in_usec_ = nius; } 28 29 // Advances the virtual 'now' by advancement_usec. 30 void AdvanceBy(int64 advancement_usec) { 31 set_now_in_usec(NowInUsec() + advancement_usec); 32 } 33 34 // Advances the virtual 'now' by advancement_usec, and 35 // calls WaitForEventAndExecteCallbacks. 36 // Note that the WaitForEventsAndExecuteCallbacks invocation 37 // may cause NowInUs to advance beyond what was specified here. 38 // If that is not desired, use the AdvanceByExactly calls. 39 void AdvanceByAndCallCallbacks(int64 advancement_usec) { 40 AdvanceBy(advancement_usec); 41 WaitForEventsAndExecuteCallbacks(); 42 } 43 44 private: 45 int64 now_in_usec_; 46}; 47 48class MockEpollServer : public FakeTimeEpollServer { 49 public: // type definitions 50 typedef base::hash_multimap<int64, struct epoll_event> EventQueue; 51 52 MockEpollServer(); 53 virtual ~MockEpollServer(); 54 55 // time_in_usec is the time at which the event specified 56 // by 'ee' will be delivered. Note that it -is- possible 57 // to add an event for a time which has already been passed.. 58 // .. upon the next time that the callbacks are invoked, 59 // all events which are in the 'past' will be delivered. 60 void AddEvent(int64 time_in_usec, const struct epoll_event& ee) { 61 event_queue_.insert(std::make_pair(time_in_usec, ee)); 62 } 63 64 // Advances the virtual 'now' by advancement_usec, 65 // and ensure that the next invocation of 66 // WaitForEventsAndExecuteCallbacks goes no farther than 67 // advancement_usec from the current time. 68 void AdvanceByExactly(int64 advancement_usec) { 69 until_in_usec_ = NowInUsec() + advancement_usec; 70 set_now_in_usec(NowInUsec() + advancement_usec); 71 } 72 73 // As above, except calls WaitForEventsAndExecuteCallbacks. 74 void AdvanceByExactlyAndCallCallbacks(int64 advancement_usec) { 75 AdvanceByExactly(advancement_usec); 76 WaitForEventsAndExecuteCallbacks(); 77 } 78 79 base::hash_set<AlarmCB*>::size_type NumberOfAlarms() const { 80 return all_alarms_.size(); 81 } 82 83 protected: // functions 84 // These functions do nothing here, as we're not actually 85 // using the epoll_* syscalls. 86 virtual void DelFD(int fd) const OVERRIDE { } 87 virtual void AddFD(int fd, int event_mask) const OVERRIDE { } 88 virtual void ModFD(int fd, int event_mask) const OVERRIDE { } 89 90 // Replaces the epoll_server's epoll_wait_impl. 91 virtual int epoll_wait_impl(int epfd, 92 struct epoll_event* events, 93 int max_events, 94 int timeout_in_ms) OVERRIDE; 95 virtual void SetNonblocking (int fd) OVERRIDE { } 96 97 private: // members 98 EventQueue event_queue_; 99 int64 until_in_usec_; 100}; 101 102} // namespace test 103} // namespace tools 104} // namespace net 105 106#endif // NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ 107