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