OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ | 5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ |
6 #define NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ | 6 #define NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
| 11 #include <unordered_map> |
| 12 #include <unordered_set> |
| 13 |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/macros.h" | 15 #include "base/macros.h" |
13 #include "net/tools/epoll_server/epoll_server.h" | 16 #include "net/tools/epoll_server/epoll_server.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
15 | 18 |
16 namespace net { | 19 namespace net { |
17 namespace test { | 20 namespace test { |
18 | 21 |
19 // Unlike the full MockEpollServer, this only lies about the time but lets | 22 // Unlike the full MockEpollServer, this only lies about the time but lets |
20 // fd events operate normally. Usefully when interacting with real backends | 23 // fd events operate normally. Usefully when interacting with real backends |
(...skipping 24 matching lines...) Expand all Loading... |
45 } | 48 } |
46 | 49 |
47 private: | 50 private: |
48 int64_t now_in_usec_; | 51 int64_t now_in_usec_; |
49 | 52 |
50 DISALLOW_COPY_AND_ASSIGN(FakeTimeEpollServer); | 53 DISALLOW_COPY_AND_ASSIGN(FakeTimeEpollServer); |
51 }; | 54 }; |
52 | 55 |
53 class MockEpollServer : public FakeTimeEpollServer { | 56 class MockEpollServer : public FakeTimeEpollServer { |
54 public: // type definitions | 57 public: // type definitions |
55 typedef base::hash_multimap<int64_t, struct epoll_event> EventQueue; | 58 using EventQueue = std::unordered_multimap<int64_t, struct epoll_event>; |
56 | 59 |
57 MockEpollServer(); | 60 MockEpollServer(); |
58 ~MockEpollServer() override; | 61 ~MockEpollServer() override; |
59 | 62 |
60 // time_in_usec is the time at which the event specified | 63 // time_in_usec is the time at which the event specified |
61 // by 'ee' will be delivered. Note that it -is- possible | 64 // by 'ee' will be delivered. Note that it -is- possible |
62 // to add an event for a time which has already been passed.. | 65 // to add an event for a time which has already been passed.. |
63 // .. upon the next time that the callbacks are invoked, | 66 // .. upon the next time that the callbacks are invoked, |
64 // all events which are in the 'past' will be delivered. | 67 // all events which are in the 'past' will be delivered. |
65 void AddEvent(int64_t time_in_usec, const struct epoll_event& ee) { | 68 void AddEvent(int64_t time_in_usec, const struct epoll_event& ee) { |
66 event_queue_.insert(std::make_pair(time_in_usec, ee)); | 69 event_queue_.insert(std::make_pair(time_in_usec, ee)); |
67 } | 70 } |
68 | 71 |
69 // Advances the virtual 'now' by advancement_usec, | 72 // Advances the virtual 'now' by advancement_usec, |
70 // and ensure that the next invocation of | 73 // and ensure that the next invocation of |
71 // WaitForEventsAndExecuteCallbacks goes no farther than | 74 // WaitForEventsAndExecuteCallbacks goes no farther than |
72 // advancement_usec from the current time. | 75 // advancement_usec from the current time. |
73 void AdvanceByExactly(int64_t advancement_usec) { | 76 void AdvanceByExactly(int64_t advancement_usec) { |
74 until_in_usec_ = NowInUsec() + advancement_usec; | 77 until_in_usec_ = NowInUsec() + advancement_usec; |
75 set_now_in_usec(NowInUsec() + advancement_usec); | 78 set_now_in_usec(NowInUsec() + advancement_usec); |
76 } | 79 } |
77 | 80 |
78 // As above, except calls WaitForEventsAndExecuteCallbacks. | 81 // As above, except calls WaitForEventsAndExecuteCallbacks. |
79 void AdvanceByExactlyAndCallCallbacks(int64_t advancement_usec) { | 82 void AdvanceByExactlyAndCallCallbacks(int64_t advancement_usec) { |
80 AdvanceByExactly(advancement_usec); | 83 AdvanceByExactly(advancement_usec); |
81 WaitForEventsAndExecuteCallbacks(); | 84 WaitForEventsAndExecuteCallbacks(); |
82 } | 85 } |
83 | 86 |
84 base::hash_set<AlarmCB*>::size_type NumberOfAlarms() const { | 87 std::unordered_set<AlarmCB*>::size_type NumberOfAlarms() const { |
85 return all_alarms_.size(); | 88 return all_alarms_.size(); |
86 } | 89 } |
87 | 90 |
88 protected: // functions | 91 protected: // functions |
89 // These functions do nothing here, as we're not actually | 92 // These functions do nothing here, as we're not actually |
90 // using the epoll_* syscalls. | 93 // using the epoll_* syscalls. |
91 void DelFD(int fd) const override {} | 94 void DelFD(int fd) const override {} |
92 void AddFD(int fd, int event_mask) const override {} | 95 void AddFD(int fd, int event_mask) const override {} |
93 void ModFD(int fd, int event_mask) const override {} | 96 void ModFD(int fd, int event_mask) const override {} |
94 | 97 |
95 // Replaces the epoll_server's epoll_wait_impl. | 98 // Replaces the epoll_server's epoll_wait_impl. |
96 int epoll_wait_impl(int epfd, | 99 int epoll_wait_impl(int epfd, |
97 struct epoll_event* events, | 100 struct epoll_event* events, |
98 int max_events, | 101 int max_events, |
99 int timeout_in_ms) override; | 102 int timeout_in_ms) override; |
100 void SetNonblocking(int fd) override {} | 103 void SetNonblocking(int fd) override {} |
101 | 104 |
102 private: // members | 105 private: // members |
103 EventQueue event_queue_; | 106 EventQueue event_queue_; |
104 int64_t until_in_usec_; | 107 int64_t until_in_usec_; |
105 | 108 |
106 DISALLOW_COPY_AND_ASSIGN(MockEpollServer); | 109 DISALLOW_COPY_AND_ASSIGN(MockEpollServer); |
107 }; | 110 }; |
108 | 111 |
109 } // namespace test | 112 } // namespace test |
110 } // namespace net | 113 } // namespace net |
111 | 114 |
112 #endif // NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ | 115 #endif // NET_TOOLS_QUIC_TEST_TOOLS_MOCK_EPOLL_SERVER_H_ |
OLD | NEW |