diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-21 21:50:34 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-21 21:50:34 +0000 |
commit | 0cb3d72e44bbca50104f8a79acdff5c6344278b8 (patch) | |
tree | a79b35e3eb8c1c705268ae173fc9caa00bf47e8b /base | |
parent | 4d805b5db89c08532633e9fcacdc4df0828fe1fe (diff) | |
download | chromium_src-0cb3d72e44bbca50104f8a79acdff5c6344278b8.zip chromium_src-0cb3d72e44bbca50104f8a79acdff5c6344278b8.tar.gz chromium_src-0cb3d72e44bbca50104f8a79acdff5c6344278b8.tar.bz2 |
Add a Clock and TickClock interface for mocking out time
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
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 4 | ||||
-rw-r--r-- | base/base.gypi | 9 | ||||
-rw-r--r-- | base/test/mock_time_provider.h | 3 | ||||
-rw-r--r-- | base/test/simple_test_clock.cc | 23 | ||||
-rw-r--r-- | base/test/simple_test_clock.h | 39 | ||||
-rw-r--r-- | base/test/simple_test_tick_clock.cc | 26 | ||||
-rw-r--r-- | base/test/simple_test_tick_clock.h | 39 | ||||
-rw-r--r-- | base/time/clock.cc | 11 | ||||
-rw-r--r-- | base/time/clock.h | 40 | ||||
-rw-r--r-- | base/time/default_clock.cc | 15 | ||||
-rw-r--r-- | base/time/default_clock.h | 25 | ||||
-rw-r--r-- | base/time/default_tick_clock.cc | 15 | ||||
-rw-r--r-- | base/time/default_tick_clock.h | 25 | ||||
-rw-r--r-- | base/time/tick_clock.cc | 11 | ||||
-rw-r--r-- | base/time/tick_clock.h | 40 |
15 files changed, 325 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index 2c1ecc7..7c63ec2 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -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', diff --git a/base/base.gypi b/base/base.gypi index 9f588c8..7cad42a 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -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', diff --git a/base/test/mock_time_provider.h b/base/test/mock_time_provider.h index 81240f7..b493209 100644 --- a/base/test/mock_time_provider.h +++ b/base/test/mock_time_provider.h @@ -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: diff --git a/base/test/simple_test_clock.cc b/base/test/simple_test_clock.cc new file mode 100644 index 0000000..3bd6080 --- /dev/null +++ b/base/test/simple_test_clock.cc @@ -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 diff --git a/base/test/simple_test_clock.h b/base/test/simple_test_clock.h new file mode 100644 index 0000000..a83312f --- /dev/null +++ b/base/test/simple_test_clock.h @@ -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_ diff --git a/base/test/simple_test_tick_clock.cc b/base/test/simple_test_tick_clock.cc new file mode 100644 index 0000000..1b4696f --- /dev/null +++ b/base/test/simple_test_tick_clock.cc @@ -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 diff --git a/base/test/simple_test_tick_clock.h b/base/test/simple_test_tick_clock.h new file mode 100644 index 0000000..c53301b --- /dev/null +++ b/base/test/simple_test_tick_clock.h @@ -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_ diff --git a/base/time/clock.cc b/base/time/clock.cc new file mode 100644 index 0000000..34dc37e --- /dev/null +++ b/base/time/clock.cc @@ -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 diff --git a/base/time/clock.h b/base/time/clock.h new file mode 100644 index 0000000..9fef254 --- /dev/null +++ b/base/time/clock.h @@ -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_ diff --git a/base/time/default_clock.cc b/base/time/default_clock.cc new file mode 100644 index 0000000..5f70114 --- /dev/null +++ b/base/time/default_clock.cc @@ -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 diff --git a/base/time/default_clock.h b/base/time/default_clock.h new file mode 100644 index 0000000..2022d5c --- /dev/null +++ b/base/time/default_clock.h @@ -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_ diff --git a/base/time/default_tick_clock.cc b/base/time/default_tick_clock.cc new file mode 100644 index 0000000..ce62fcc --- /dev/null +++ b/base/time/default_tick_clock.cc @@ -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 diff --git a/base/time/default_tick_clock.h b/base/time/default_tick_clock.h new file mode 100644 index 0000000..553a8d2 --- /dev/null +++ b/base/time/default_tick_clock.h @@ -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_ diff --git a/base/time/tick_clock.cc b/base/time/tick_clock.cc new file mode 100644 index 0000000..495805c --- /dev/null +++ b/base/time/tick_clock.cc @@ -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 diff --git a/base/time/tick_clock.h b/base/time/tick_clock.h new file mode 100644 index 0000000..396e0c9 --- /dev/null +++ b/base/time/tick_clock.h @@ -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_ |