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