Skip to content

Commit

Permalink
Add a Clock and TickClock interface for mocking out time
Browse files Browse the repository at this point in the history
Add DefaultClock and DefaultTickClock implementations.

Add SimpleTestClock and SimpleTestTickClock implementations for test.

Port some classes in sync/ and media/ to use SimpleTest{,Tick}Clock.

BUG=166469

Review URL: https://codereview.chromium.org/11607003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@183865 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
akalin@chromium.org committed Feb 21, 2013
1 parent 4d805b5 commit 0cb3d72
Show file tree
Hide file tree
Showing 24 changed files with 407 additions and 103 deletions.
4 changes: 4 additions & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,10 @@
'test/scoped_path_override.h',
'test/sequenced_task_runner_test_template.cc',
'test/sequenced_task_runner_test_template.h',
'test/simple_test_clock.cc',
'test/simple_test_clock.h',
'test/simple_test_tick_clock.cc',
'test/simple_test_tick_clock.h',
'test/task_runner_test_template.cc',
'test/task_runner_test_template.h',
'test/test_file_util.h',
Expand Down
9 changes: 9 additions & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@
'threading/worker_pool_posix.cc',
'threading/worker_pool_posix.h',
'threading/worker_pool_win.cc',
'time/clock.cc',
'time/clock.h',
'time/default_clock.cc',
'time/default_clock.h',
'time/default_tick_clock.cc',
'time/default_tick_clock.h',
'time/tick_clock.cc',
'time/tick_clock.h',
# TODO(akalin): Move time* into time/.
'time.cc',
'time.h',
'time_mac.cc',
Expand Down
3 changes: 3 additions & 0 deletions base/test/mock_time_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(akalin): Change all users of this class to use SimpleTestClock
// or SimpleTestTickClock and remove this class.

// A helper class used to mock out calls to the static method base::Time::Now.
//
// Example usage:
Expand Down
23 changes: 23 additions & 0 deletions base/test/simple_test_clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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/test/simple_test_clock.h"

namespace base {

SimpleTestClock::SimpleTestClock() {}

SimpleTestClock::~SimpleTestClock() {}

Time SimpleTestClock::Now() {
AutoLock lock(lock_);
return now_;
}

void SimpleTestClock::Advance(TimeDelta delta) {
AutoLock lock(lock_);
now_ += delta;
}

} // namespace base
39 changes: 39 additions & 0 deletions base/test/simple_test_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.

#ifndef BASE_SIMPLE_TEST_CLOCK_H_
#define BASE_SIMPLE_TEST_CLOCK_H_

#include "base/compiler_specific.h"
#include "base/synchronization/lock.h"
#include "base/time.h"
#include "base/time/clock.h"

namespace base {

// SimpleTestClock is a Clock implementation that gives control over
// the returned Time objects. All methods can be called from any
// thread.
class SimpleTestClock : public Clock {
public:
// Starts off with a clock set to Time().
SimpleTestClock();
virtual ~SimpleTestClock();

virtual Time Now() OVERRIDE;

// Sets the current time forward by |delta|. Safe to call from any
// thread.
void Advance(TimeDelta delta);

private:
// Protects |now_|.
Lock lock_;

Time now_;
};

} // namespace base

#endif // BASE_SIMPLE_TEST_CLOCK_H_
26 changes: 26 additions & 0 deletions base/test/simple_test_tick_clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/test/simple_test_tick_clock.h"

#include "base/logging.h"

namespace base {

SimpleTestTickClock::SimpleTestTickClock() {}

SimpleTestTickClock::~SimpleTestTickClock() {}

TimeTicks SimpleTestTickClock::NowTicks() {
AutoLock lock(lock_);
return now_ticks_;
}

void SimpleTestTickClock::Advance(TimeDelta delta) {
AutoLock lock(lock_);
DCHECK(delta >= TimeDelta());
now_ticks_ += delta;
}

} // namespace base
39 changes: 39 additions & 0 deletions base/test/simple_test_tick_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.

#ifndef BASE_SIMPLE_TEST_TICK_CLOCK_H_
#define BASE_SIMPLE_TEST_TICK_CLOCK_H_

#include "base/compiler_specific.h"
#include "base/synchronization/lock.h"
#include "base/time.h"
#include "base/time/tick_clock.h"

namespace base {

// SimpleTestTickClock is a TickClock implementation that gives
// control over the returned TimeTicks objects. All methods can be
// called from any thread.
class SimpleTestTickClock : public TickClock {
public:
// Starts off with a clock set to TimeTicks().
SimpleTestTickClock();
virtual ~SimpleTestTickClock();

virtual TimeTicks NowTicks() OVERRIDE;

// Sets the current time forward by |delta|. Safe to call from any
// thread.
void Advance(TimeDelta delta);

private:
// Protects |now_ticks_|.
Lock lock_;

TimeTicks now_ticks_;
};

} // namespace base

#endif // BASE_SIMPLE_TEST_TICK_CLOCK_H_
11 changes: 11 additions & 0 deletions base/time/clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/time/clock.h"

namespace base {

Clock::~Clock() {}

} // namespace base
40 changes: 40 additions & 0 deletions base/time/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.

#ifndef BASE_CLOCK_H_
#define BASE_CLOCK_H_

#include "base/base_export.h"
#include "base/time.h"

namespace base {

// A Clock is an interface for objects that vend Times. It is
// intended to be able to test the behavior of classes with respect to
// time.
//
// See DefaultClock (base/default_clock.h) for the default
// implementation that simply uses Time::Now().
//
// (An implementation that uses Time::SystemTime() should be added as
// needed.)
//
// See SimpleTestClock (base/test/simple_test_clock.h) for a simple
// test implementation.
//
// See TickClock (base/tick_clock.h) for the equivalent interface for
// TimeTicks.
class BASE_EXPORT Clock {
public:
virtual ~Clock();

// Now() must be safe to call from any thread. The caller cannot
// make any ordering assumptions about the returned Time. For
// example, the system clock may change to an earlier time.
virtual Time Now() = 0;
};

} // namespace base

#endif // BASE_CLOCK_H_
15 changes: 15 additions & 0 deletions base/time/default_clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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/time/default_clock.h"

namespace base {

DefaultClock::~DefaultClock() {}

Time DefaultClock::Now() {
return Time::Now();
}

} // namespace base
25 changes: 25 additions & 0 deletions base/time/default_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

#ifndef BASE_DEFAULT_CLOCK_H_
#define BASE_DEFAULT_CLOCK_H_

#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/time/clock.h"

namespace base {

// DefaultClock is a Clock implementation that uses Time::Now().
class BASE_EXPORT DefaultClock : public Clock {
public:
virtual ~DefaultClock();

// Simply returns Time::Now().
virtual Time Now() OVERRIDE;
};

} // namespace base

#endif // BASE_DEFAULT_CLOCK_H_
15 changes: 15 additions & 0 deletions base/time/default_tick_clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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/time/default_tick_clock.h"

namespace base {

DefaultTickClock::~DefaultTickClock() {}

TimeTicks DefaultTickClock::NowTicks() {
return TimeTicks::Now();
}

} // namespace base
25 changes: 25 additions & 0 deletions base/time/default_tick_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

#ifndef BASE_DEFAULT_TICK_CLOCK_H_
#define BASE_DEFAULT_TICK_CLOCK_H_

#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/time/tick_clock.h"

namespace base {

// DefaultClock is a Clock implementation that uses TimeTicks::Now().
class BASE_EXPORT DefaultTickClock : public TickClock {
public:
virtual ~DefaultTickClock();

// Simply returns TimeTicks::Now().
virtual TimeTicks NowTicks() OVERRIDE;
};

} // namespace base

#endif // BASE_DEFAULT_CLOCK_H_
11 changes: 11 additions & 0 deletions base/time/tick_clock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/time/tick_clock.h"

namespace base {

TickClock::~TickClock() {}

} // namespace base
40 changes: 40 additions & 0 deletions base/time/tick_clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.

#ifndef BASE_TICK_CLOCK_H_
#define BASE_TICK_CLOCK_H_

#include "base/base_export.h"
#include "base/time.h"

namespace base {

// A TickClock is an interface for objects that vend TimeTicks. It is
// intended to be able to test the behavior of classes with respect to
// non-decreasing time.
//
// See DefaultTickClock (base/default_tick_clock.h) for the default
// implementation that simply uses TimeTicks::Now().
//
// (Other implementations that use TimeTicks::HighResNow() or
// TimeTicks::NowFromSystemTime() should be added as needed.)
//
// See SimpleTestTickClock (base/test/simple_test_tick_clock.h) for a
// simple test implementation.
//
// See Clock (base/clock.h) for the equivalent interface for Times.
class BASE_EXPORT TickClock {
public:
virtual ~TickClock();

// NowTicks() must be safe to call from any thread. The caller may
// assume that NowTicks() is monotonic (but not strictly monotonic).
// In other words, the returned TimeTicks will never decrease with
// time, although they might "stand still".
virtual TimeTicks NowTicks() = 0;
};

} // namespace base

#endif // BASE_TICK_CLOCK_H_
Loading

0 comments on commit 0cb3d72

Please sign in to comment.