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 /sync/notifier | |
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 'sync/notifier')
-rw-r--r-- | sync/notifier/ack_tracker.cc | 19 | ||||
-rw-r--r-- | sync/notifier/ack_tracker.h | 11 | ||||
-rw-r--r-- | sync/notifier/ack_tracker_unittest.cc | 87 |
3 files changed, 54 insertions, 63 deletions
diff --git a/sync/notifier/ack_tracker.cc b/sync/notifier/ack_tracker.cc index 4014672..759b869 100644 --- a/sync/notifier/ack_tracker.cc +++ b/sync/notifier/ack_tracker.cc @@ -10,6 +10,7 @@ #include "base/callback.h" #include "base/stl_util.h" +#include "base/time/tick_clock.h" #include "google/cacheinvalidation/include/types.h" namespace syncer { @@ -73,10 +74,11 @@ AckTracker::Entry::Entry(scoped_ptr<net::BackoffEntry> backoff, AckTracker::Entry::~Entry() { } -AckTracker::AckTracker(Delegate* delegate) - : now_callback_(base::Bind(&base::TimeTicks::Now)), - create_backoff_entry_callback_(base::Bind(&CreateDefaultBackoffEntry)), +AckTracker::AckTracker(base::TickClock* tick_clock, Delegate* delegate) + : create_backoff_entry_callback_(base::Bind(&CreateDefaultBackoffEntry)), + tick_clock_(tick_clock), delegate_(delegate) { + DCHECK(tick_clock_); DCHECK(delegate_); } @@ -139,7 +141,7 @@ void AckTracker::NudgeTimer() { return; } - const base::TimeTicks now = now_callback_.Run(); + const base::TimeTicks now = tick_clock_->NowTicks(); // There are two cases when the timer needs to be started: // 1. |desired_run_time_| is in the past. By definition, the timer has already // fired at this point. Since the queue is non-empty, we need to set the @@ -159,7 +161,7 @@ void AckTracker::NudgeTimer() { void AckTracker::OnTimeout() { DCHECK(thread_checker_.CalledOnValidThread()); - OnTimeoutAt(now_callback_.Run()); + OnTimeoutAt(tick_clock_->NowTicks()); } void AckTracker::OnTimeoutAt(base::TimeTicks now) { @@ -188,13 +190,6 @@ void AckTracker::OnTimeoutAt(base::TimeTicks now) { } // Testing helpers. -void AckTracker::SetNowCallbackForTest( - const NowCallback& now_callback) { - DCHECK(thread_checker_.CalledOnValidThread()); - - now_callback_ = now_callback; -} - void AckTracker::SetCreateBackoffEntryCallbackForTest( const CreateBackoffEntryCallback& create_backoff_entry_callback) { DCHECK(thread_checker_.CalledOnValidThread()); diff --git a/sync/notifier/ack_tracker.h b/sync/notifier/ack_tracker.h index 08fcad9..a21a914 100644 --- a/sync/notifier/ack_tracker.h +++ b/sync/notifier/ack_tracker.h @@ -17,6 +17,10 @@ #include "sync/base/sync_export.h" #include "sync/notifier/invalidation_util.h" +namespace base { +class TickClock; +} // namespace base + namespace syncer { // A simple class that tracks sets of object IDs that have not yet been @@ -34,11 +38,10 @@ class SYNC_EXPORT_PRIVATE AckTracker { virtual void OnTimeout(const ObjectIdSet& ids) = 0; }; - typedef base::Callback<base::TimeTicks()> NowCallback; typedef base::Callback<scoped_ptr<net::BackoffEntry>( const net::BackoffEntry::Policy* const)> CreateBackoffEntryCallback; - explicit AckTracker(Delegate* delegate); + AckTracker(base::TickClock* tick_clock, Delegate* delegate); ~AckTracker(); // Equivalent to calling Ack() on all currently registered object IDs. @@ -53,7 +56,6 @@ class SYNC_EXPORT_PRIVATE AckTracker { void Ack(const ObjectIdSet& ids); // Testing methods. - void SetNowCallbackForTest(const NowCallback& now_callback); void SetCreateBackoffEntryCallbackForTest( const CreateBackoffEntryCallback& create_backoff_entry_callback); // Returns true iff there are no timeouts scheduled to occur before |now|. @@ -83,9 +85,10 @@ class SYNC_EXPORT_PRIVATE AckTracker { const net::BackoffEntry::Policy* const policy); // Used for testing purposes. - NowCallback now_callback_; CreateBackoffEntryCallback create_backoff_entry_callback_; + base::TickClock* const tick_clock_; + Delegate* const delegate_; base::OneShotTimer<AckTracker> timer_; diff --git a/sync/notifier/ack_tracker_unittest.cc b/sync/notifier/ack_tracker_unittest.cc index 9a5c802..7208d76 100644 --- a/sync/notifier/ack_tracker_unittest.cc +++ b/sync/notifier/ack_tracker_unittest.cc @@ -4,8 +4,10 @@ #include "sync/notifier/ack_tracker.h" +#include "base/compiler_specific.h" #include "base/memory/ref_counted.h" #include "base/message_loop.h" +#include "base/time/tick_clock.h" #include "google/cacheinvalidation/include/types.h" #include "google/cacheinvalidation/types.pb.h" #include "testing/gmock/include/gmock/gmock.h" @@ -15,15 +17,15 @@ namespace syncer { namespace { -typedef AckTracker::NowCallback NowCallback; - -class MockTimeProvider : public base::RefCountedThreadSafe<MockTimeProvider> { +class FakeTickClock : public base::TickClock { public: - MockTimeProvider() : fake_now_(base::TimeTicks::Now()) {} + FakeTickClock() {} + + virtual ~FakeTickClock() {} void LeapForward(int seconds) { ASSERT_GT(seconds, 0); - fake_now_ += base::TimeDelta::FromSeconds(seconds); + fake_now_ticks_ += base::TimeDelta::FromSeconds(seconds); } // After the next call to Now(), immediately leap forward by |seconds|. @@ -32,38 +34,34 @@ class MockTimeProvider : public base::RefCountedThreadSafe<MockTimeProvider> { delayed_leap_ = base::TimeDelta::FromSeconds(seconds); } - base::TimeTicks Now() { - base::TimeTicks fake_now = fake_now_; + virtual base::TimeTicks NowTicks() OVERRIDE { + base::TimeTicks fake_now_ticks = fake_now_ticks_; if (delayed_leap_ > base::TimeDelta()) { - fake_now_ += delayed_leap_; + fake_now_ticks_ += delayed_leap_; delayed_leap_ = base::TimeDelta(); } - return fake_now; + return fake_now_ticks; } private: - friend class base::RefCountedThreadSafe<MockTimeProvider>; - - ~MockTimeProvider() {} - - base::TimeTicks fake_now_; + base::TimeTicks fake_now_ticks_; base::TimeDelta delayed_leap_; }; class FakeBackoffEntry : public net::BackoffEntry { public: - FakeBackoffEntry(const Policy* const policy, const NowCallback& now_callback) + FakeBackoffEntry(const Policy* const policy, base::TickClock* tick_clock) : BackoffEntry(policy), - now_callback_(now_callback) { + tick_clock_(tick_clock) { } protected: virtual base::TimeTicks ImplGetTimeNow() const OVERRIDE { - return now_callback_.Run(); + return tick_clock_->NowTicks(); } private: - NowCallback now_callback_; + base::TickClock* const tick_clock_; }; class MockDelegate : public AckTracker::Delegate { @@ -72,10 +70,10 @@ class MockDelegate : public AckTracker::Delegate { }; scoped_ptr<net::BackoffEntry> CreateMockEntry( - const NowCallback& now_callback, + base::TickClock* tick_clock, const net::BackoffEntry::Policy* const policy) { return scoped_ptr<net::BackoffEntry>(new FakeBackoffEntry( - policy, now_callback)); + policy, tick_clock)); } } // namespace @@ -83,21 +81,16 @@ scoped_ptr<net::BackoffEntry> CreateMockEntry( class AckTrackerTest : public testing::Test { public: AckTrackerTest() - : time_provider_(new MockTimeProvider), - ack_tracker_(&delegate_), + : ack_tracker_(&fake_tick_clock_, &delegate_), kIdOne(ipc::invalidation::ObjectSource::TEST, "one"), kIdTwo(ipc::invalidation::ObjectSource::TEST, "two") { - ack_tracker_.SetNowCallbackForTest( - base::Bind(&MockTimeProvider::Now, time_provider_)); ack_tracker_.SetCreateBackoffEntryCallbackForTest( - base::Bind(&CreateMockEntry, - base::Bind(&MockTimeProvider::Now, - time_provider_))); + base::Bind(&CreateMockEntry, &fake_tick_clock_)); } protected: bool TriggerTimeoutNow() { - return ack_tracker_.TriggerTimeoutAtForTest(time_provider_->Now()); + return ack_tracker_.TriggerTimeoutAtForTest(fake_tick_clock_.NowTicks()); } base::TimeDelta GetTimerDelay() const { @@ -107,7 +100,7 @@ class AckTrackerTest : public testing::Test { return timer.GetCurrentDelay(); } - scoped_refptr<MockTimeProvider> time_provider_; + FakeTickClock fake_tick_clock_; ::testing::StrictMock<MockDelegate> delegate_; AckTracker ack_tracker_; @@ -200,32 +193,32 @@ TEST_F(AckTrackerTest, SimpleTimeout) { EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); EXPECT_EQ(base::TimeDelta::FromSeconds(60), GetTimerDelay()); - time_provider_->LeapForward(60); + fake_tick_clock_.LeapForward(60); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(120), GetTimerDelay()); - time_provider_->LeapForward(120); + fake_tick_clock_.LeapForward(120); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(240), GetTimerDelay()); - time_provider_->LeapForward(240); + fake_tick_clock_.LeapForward(240); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(480), GetTimerDelay()); - time_provider_->LeapForward(480); + fake_tick_clock_.LeapForward(480); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(600), GetTimerDelay()); - time_provider_->LeapForward(600); + fake_tick_clock_.LeapForward(600); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(600), GetTimerDelay()); - time_provider_->LeapForward(600); + fake_tick_clock_.LeapForward(600); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); @@ -238,7 +231,7 @@ TEST_F(AckTrackerTest, SimpleTimeout) { EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); EXPECT_EQ(base::TimeDelta::FromSeconds(60), GetTimerDelay()); - time_provider_->LeapForward(60); + fake_tick_clock_.LeapForward(60); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); @@ -259,27 +252,27 @@ TEST_F(AckTrackerTest, InterleavedTimeout) { ack_tracker_.Track(ids_one); EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); ack_tracker_.Track(ids_two); EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); EXPECT_EQ(base::TimeDelta::FromSeconds(60), GetTimerDelay()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); EXPECT_CALL(delegate_, OnTimeout(ids_one)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(30), GetTimerDelay()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); EXPECT_CALL(delegate_, OnTimeout(ids_two)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(90), GetTimerDelay()); - time_provider_->LeapForward(90); + fake_tick_clock_.LeapForward(90); EXPECT_CALL(delegate_, OnTimeout(ids_one)); EXPECT_TRUE(TriggerTimeoutNow()); EXPECT_EQ(base::TimeDelta::FromSeconds(30), GetTimerDelay()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); EXPECT_CALL(delegate_, OnTimeout(ids_two)); EXPECT_TRUE(TriggerTimeoutNow()); @@ -301,27 +294,27 @@ TEST_F(AckTrackerTest, ShortenTimeout) { EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); EXPECT_EQ(base::TimeDelta::FromSeconds(60), GetTimerDelay()); - time_provider_->LeapForward(60); + fake_tick_clock_.LeapForward(60); EXPECT_CALL(delegate_, OnTimeout(ids_one)); EXPECT_TRUE(TriggerTimeoutNow()); // Without this next register, the next timeout should occur in 120 seconds // from the last timeout event. EXPECT_EQ(base::TimeDelta::FromSeconds(120), GetTimerDelay()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); ack_tracker_.Track(ids_two); EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); // Now that we've registered another entry though, we should receive a timeout // in 60 seconds. EXPECT_EQ(base::TimeDelta::FromSeconds(60), GetTimerDelay()); - time_provider_->LeapForward(60); + fake_tick_clock_.LeapForward(60); EXPECT_CALL(delegate_, OnTimeout(ids_two)); EXPECT_TRUE(TriggerTimeoutNow()); // Verify that the original timeout for kIdOne still occurs as expected. EXPECT_EQ(base::TimeDelta::FromSeconds(30), GetTimerDelay()); - time_provider_->LeapForward(30); + fake_tick_clock_.LeapForward(30); EXPECT_CALL(delegate_, OnTimeout(ids_one)); EXPECT_TRUE(TriggerTimeoutNow()); @@ -337,7 +330,7 @@ TEST_F(AckTrackerTest, ImmediateTimeout) { ObjectIdSet ids; ids.insert(kIdOne); - time_provider_->DelayedLeapForward(90); + fake_tick_clock_.DelayedLeapForward(90); EXPECT_TRUE(ack_tracker_.IsQueueEmptyForTest()); ack_tracker_.Track(ids); EXPECT_FALSE(ack_tracker_.IsQueueEmptyForTest()); @@ -348,7 +341,7 @@ TEST_F(AckTrackerTest, ImmediateTimeout) { // The next timeout should still be scheduled normally. EXPECT_EQ(base::TimeDelta::FromSeconds(120), GetTimerDelay()); - time_provider_->LeapForward(120); + fake_tick_clock_.LeapForward(120); EXPECT_CALL(delegate_, OnTimeout(ids)); EXPECT_TRUE(TriggerTimeoutNow()); |