blob: 742a957d882296e582c03db9493eda9ce88a347a [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/logging.h"
#include "net/tools/epoll_server/epoll_server.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace net {
namespace tools {
namespace test {
// Unlike the full MockEpollServer, this only lies about the time but lets
// fd events operate normally. Usefully when interacting with real backends
// but wanting to skip forward in time to trigger timeouts.
class FakeTimeEpollServer : public EpollServer {
~FakeTimeEpollServer() override;
// Replaces the EpollServer NowInUsec.
int64 NowInUsec() const override;
void set_now_in_usec(int64 nius) { now_in_usec_ = nius; }
// Advances the virtual 'now' by advancement_usec.
void AdvanceBy(int64 advancement_usec) {
set_now_in_usec(NowInUsec() + advancement_usec);
// Advances the virtual 'now' by advancement_usec, and
// calls WaitForEventAndExecteCallbacks.
// Note that the WaitForEventsAndExecuteCallbacks invocation
// may cause NowInUs to advance beyond what was specified here.
// If that is not desired, use the AdvanceByExactly calls.
void AdvanceByAndCallCallbacks(int64 advancement_usec) {
int64 now_in_usec_;
class MockEpollServer : public FakeTimeEpollServer {
public: // type definitions
typedef base::hash_multimap<int64, struct epoll_event> EventQueue;
~MockEpollServer() override;
// time_in_usec is the time at which the event specified
// by 'ee' will be delivered. Note that it -is- possible
// to add an event for a time which has already been passed..
// .. upon the next time that the callbacks are invoked,
// all events which are in the 'past' will be delivered.
void AddEvent(int64 time_in_usec, const struct epoll_event& ee) {
event_queue_.insert(std::make_pair(time_in_usec, ee));
// Advances the virtual 'now' by advancement_usec,
// and ensure that the next invocation of
// WaitForEventsAndExecuteCallbacks goes no farther than
// advancement_usec from the current time.
void AdvanceByExactly(int64 advancement_usec) {
until_in_usec_ = NowInUsec() + advancement_usec;
set_now_in_usec(NowInUsec() + advancement_usec);
// As above, except calls WaitForEventsAndExecuteCallbacks.
void AdvanceByExactlyAndCallCallbacks(int64 advancement_usec) {
base::hash_set<AlarmCB*>::size_type NumberOfAlarms() const {
return all_alarms_.size();
protected: // functions
// These functions do nothing here, as we're not actually
// using the epoll_* syscalls.
void DelFD(int fd) const override {}
void AddFD(int fd, int event_mask) const override {}
void ModFD(int fd, int event_mask) const override {}
// Replaces the epoll_server's epoll_wait_impl.
int epoll_wait_impl(int epfd,
struct epoll_event* events,
int max_events,
int timeout_in_ms) override;
void SetNonblocking(int fd) override {}
private: // members
EventQueue event_queue_;
int64 until_in_usec_;
} // namespace test
} // namespace tools
} // namespace net