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