diff options
author | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-24 21:05:36 +0000 |
---|---|---|
committer | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-24 21:05:36 +0000 |
commit | 506cb1db9a466aad6b8d3fdce9cb94eb54516e47 (patch) | |
tree | 8959bc3c7ab135de91fa746bf416603cc6c98bdd /chrome/browser/sync/engine/syncer_thread_unittest.cc | |
parent | 7b3c0312746e6f21478a01c3d56db4714b48618f (diff) | |
download | chromium_src-506cb1db9a466aad6b8d3fdce9cb94eb54516e47.zip chromium_src-506cb1db9a466aad6b8d3fdce9cb94eb54516e47.tar.gz chromium_src-506cb1db9a466aad6b8d3fdce9cb94eb54516e47.tar.bz2 |
Reverting 27117.
Review URL: http://codereview.chromium.org/235010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27124 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/engine/syncer_thread_unittest.cc')
-rw-r--r-- | chrome/browser/sync/engine/syncer_thread_unittest.cc | 244 |
1 files changed, 56 insertions, 188 deletions
diff --git a/chrome/browser/sync/engine/syncer_thread_unittest.cc b/chrome/browser/sync/engine/syncer_thread_unittest.cc index fa28f7f..6b758a7 100644 --- a/chrome/browser/sync/engine/syncer_thread_unittest.cc +++ b/chrome/browser/sync/engine/syncer_thread_unittest.cc @@ -7,158 +7,89 @@ #include <set> #include <strstream> -#include "base/command_line.h" #include "base/scoped_ptr.h" -#include "base/time.h" -#include "chrome/browser/sync/engine/model_safe_worker.h" #include "chrome/browser/sync/engine/syncer_thread.h" -#include "chrome/browser/sync/engine/syncer_thread_timed_stop.h" -#include "chrome/test/sync/engine/mock_server_connection.h" -#include "chrome/test/sync/engine/test_directory_setter_upper.h" #include "testing/gtest/include/gtest/gtest.h" -using base::Time; -using base::TimeDelta; - namespace browser_sync { -typedef testing::Test SyncerThreadTest; - -class SyncerThreadWithSyncerTest : public testing::Test { - public: - SyncerThreadWithSyncerTest() {} - virtual void SetUp() { - metadb_.SetUp(); - connection_.reset(new MockConnectionManager(metadb_.manager(), - metadb_.name())); - allstatus_.reset(new AllStatus()); - - syncer_thread_ = SyncerThreadFactory::Create(NULL, metadb_.manager(), - connection_.get(), allstatus_.get(), new ModelSafeWorker()); - - allstatus_->WatchSyncerThread(syncer_thread_); - syncer_thread_->SetConnected(true); - } - virtual void TearDown() { - syncer_thread_ = NULL; - allstatus_.reset(); - connection_.reset(); - metadb_.TearDown(); - } +class SyncerThreadTest : public testing::Test { + protected: + SyncerThreadTest() {} + virtual ~SyncerThreadTest() {} + virtual void SetUp() {} + virtual void TearDown() {} - ManuallyOpenedTestDirectorySetterUpper* metadb() { return &metadb_; } - MockConnectionManager* connection() { return connection_.get(); } - SyncerThread* syncer_thread() { return syncer_thread_; } - private: - ManuallyOpenedTestDirectorySetterUpper metadb_; - scoped_ptr<MockConnectionManager> connection_; - scoped_ptr<AllStatus> allstatus_; - scoped_refptr<SyncerThread> syncer_thread_; - DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); -}; - -class SyncShareIntercept : public MockConnectionManager::MidCommitObserver { - public: - SyncShareIntercept() : sync_occured_(false, false) {} - virtual ~SyncShareIntercept() {} - virtual void Observe() { - times_sync_occured_.push_back(Time::NowFromSystemTime()); - sync_occured_.Signal(); - } - void WaitForSyncShare(int at_least_this_many, TimeDelta max_wait) { - while (at_least_this_many-- > 0) - sync_occured_.TimedWait(max_wait); - } - std::vector<Time> times_sync_occured() const { - return times_sync_occured_; - } private: - std::vector<Time> times_sync_occured_; - base::WaitableEvent sync_occured_; - DISALLOW_COPY_AND_ASSIGN(SyncShareIntercept); + DISALLOW_COPY_AND_ASSIGN(SyncerThreadTest); }; TEST_F(SyncerThreadTest, Construction) { - scoped_refptr<SyncerThread> syncer_thread( - SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); -} - -TEST_F(SyncerThreadTest, StartStop) { - scoped_refptr<SyncerThread> syncer_thread( - SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); - EXPECT_TRUE(syncer_thread->Start()); - EXPECT_TRUE(syncer_thread->Stop(2000)); - - // Do it again for good measure. I caught some bugs by adding this so - // I would recommend keeping it. - EXPECT_TRUE(syncer_thread->Start()); - EXPECT_TRUE(syncer_thread->Stop(2000)); + SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); } TEST_F(SyncerThreadTest, CalculateSyncWaitTime) { - scoped_refptr<SyncerThread> syncer_thread( - SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); - syncer_thread->DisableIdleDetection(); - - // Syncer_polling_interval_ is less than max poll interval. - TimeDelta syncer_polling_interval = TimeDelta::FromSeconds(1); + SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); + syncer_thread.DisableIdleDetection(); - syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); + // Syncer_polling_interval_ is less than max poll interval + int syncer_polling_interval = 1; // Needed since AssertionResult is not a + // friend of SyncerThread + syncer_thread.syncer_polling_interval_ = syncer_polling_interval; // user_idle_ms is less than 10 * (syncer_polling_interval*1000). - ASSERT_EQ(syncer_polling_interval.InMilliseconds(), - syncer_thread->CalculateSyncWaitTime(1000, 0)); - ASSERT_EQ(syncer_polling_interval.InMilliseconds(), - syncer_thread->CalculateSyncWaitTime(1000, 1)); + ASSERT_EQ(syncer_polling_interval * 1000, + syncer_thread.CalculateSyncWaitTime(1000, 0)); + ASSERT_EQ(syncer_polling_interval * 1000, + syncer_thread.CalculateSyncWaitTime(1000, 1)); // user_idle_ms is ge than 10 * (syncer_polling_interval*1000). int last_poll_time = 2000; ASSERT_LE(last_poll_time, - syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000)); + syncer_thread.CalculateSyncWaitTime(last_poll_time, 10000)); ASSERT_GE(last_poll_time*3, - syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000)); + syncer_thread.CalculateSyncWaitTime(last_poll_time, 10000)); ASSERT_LE(last_poll_time, - syncer_thread->CalculateSyncWaitTime(last_poll_time, 100000)); + syncer_thread.CalculateSyncWaitTime(last_poll_time, 100000)); ASSERT_GE(last_poll_time*3, - syncer_thread->CalculateSyncWaitTime(last_poll_time, 100000)); + syncer_thread.CalculateSyncWaitTime(last_poll_time, 100000)); // Maximum backoff time should be syncer_max_interval. int near_threshold = SyncerThread::kDefaultMaxPollIntervalMs / 2 - 1; int threshold = SyncerThread::kDefaultMaxPollIntervalMs; int over_threshold = SyncerThread::kDefaultMaxPollIntervalMs + 1; ASSERT_LE(near_threshold, - syncer_thread->CalculateSyncWaitTime(near_threshold, 10000)); + syncer_thread.CalculateSyncWaitTime(near_threshold, 10000)); ASSERT_GE(SyncerThread::kDefaultMaxPollIntervalMs, - syncer_thread->CalculateSyncWaitTime(near_threshold, 10000)); + syncer_thread.CalculateSyncWaitTime(near_threshold, 10000)); ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, - syncer_thread->CalculateSyncWaitTime(threshold, 10000)); + syncer_thread.CalculateSyncWaitTime(threshold, 10000)); ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, - syncer_thread->CalculateSyncWaitTime(over_threshold, 10000)); + syncer_thread.CalculateSyncWaitTime(over_threshold, 10000)); // Possible idle time must be capped by syncer_max_interval. int over_sync_max_interval = SyncerThread::kDefaultMaxPollIntervalMs + 1; - syncer_polling_interval = TimeDelta::FromSeconds( - over_sync_max_interval / 100); // so 1000* is right - syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); - ASSERT_EQ(syncer_polling_interval.InSeconds() * 1000, - syncer_thread->CalculateSyncWaitTime(1000, over_sync_max_interval)); - syncer_polling_interval = TimeDelta::FromSeconds(1); - syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); + syncer_polling_interval = over_sync_max_interval / 100; // so 1000* is right + syncer_thread.syncer_polling_interval_ = syncer_polling_interval; + ASSERT_EQ(syncer_polling_interval * 1000, + syncer_thread.CalculateSyncWaitTime(1000, over_sync_max_interval)); + syncer_polling_interval = 1; + syncer_thread.syncer_polling_interval_ = syncer_polling_interval; ASSERT_LE(last_poll_time, - syncer_thread->CalculateSyncWaitTime(last_poll_time, + syncer_thread.CalculateSyncWaitTime(last_poll_time, over_sync_max_interval)); ASSERT_GE(last_poll_time * 3, - syncer_thread->CalculateSyncWaitTime(last_poll_time, + syncer_thread.CalculateSyncWaitTime(last_poll_time, over_sync_max_interval)); } TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // Set up the environment. int user_idle_milliseconds_param = 0; - scoped_refptr<SyncerThread> syncer_thread( - SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); - syncer_thread->DisableIdleDetection(); + + SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); + syncer_thread.DisableIdleDetection(); // Notifications disabled should result in a polling interval of // kDefaultShortPollInterval. @@ -169,7 +100,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // No work and no backoff. ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -179,7 +110,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // In this case the continue_sync_cycle is turned off. continue_sync_cycle_param = true; ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -200,7 +131,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // No work and no backoff. ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -210,7 +141,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // In this case the continue_sync_cycle is turned off. continue_sync_cycle_param = true; ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -231,7 +162,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { status.updates_received = 0; bool continue_sync_cycle_param = false; - ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -239,19 +170,19 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { ASSERT_TRUE(continue_sync_cycle_param); continue_sync_cycle_param = false; - ASSERT_GE(3, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(3, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, &continue_sync_cycle_param)); ASSERT_TRUE(continue_sync_cycle_param); - ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, &continue_sync_cycle_param)); - ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -260,7 +191,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { status.updates_received = 1; ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 10, &user_idle_milliseconds_param, @@ -273,7 +204,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { status.unsynced_count = 1; bool continue_sync_cycle_param = false; - ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -281,7 +212,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { ASSERT_TRUE(continue_sync_cycle_param); continue_sync_cycle_param = false; - ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( status, 0, &user_idle_milliseconds_param, @@ -290,7 +221,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { status.unsynced_count = 0; ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 4, &user_idle_milliseconds_param, @@ -306,7 +237,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // Expect move from default polling interval to exponential backoff due to // unsynced_count != 0. - ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( status, 3600, &user_idle_milliseconds_param, @@ -314,7 +245,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { ASSERT_TRUE(continue_sync_cycle_param); continue_sync_cycle_param = false; - ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( status, 3600, &user_idle_milliseconds_param, @@ -322,12 +253,12 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { ASSERT_TRUE(continue_sync_cycle_param); // Expect exponential backoff. - ASSERT_LE(2, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(2, syncer_thread.CalculatePollingWaitTime( status, 2, &user_idle_milliseconds_param, &continue_sync_cycle_param)); - ASSERT_GE(6, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(6, syncer_thread.CalculatePollingWaitTime( status, 2, &user_idle_milliseconds_param, @@ -337,7 +268,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // A nudge resets the continue_sync_cycle_param value, so our backoff // should return to the minimum. continue_sync_cycle_param = false; - ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( + ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( status, 3600, &user_idle_milliseconds_param, @@ -345,7 +276,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { ASSERT_TRUE(continue_sync_cycle_param); continue_sync_cycle_param = false; - ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( + ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( status, 3600, &user_idle_milliseconds_param, @@ -355,7 +286,7 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { // Setting unsynced_count = 0 returns us to the default polling interval. status.unsynced_count = 0; ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, - syncer_thread->CalculatePollingWaitTime( + syncer_thread.CalculatePollingWaitTime( status, 4, &user_idle_milliseconds_param, @@ -364,67 +295,4 @@ TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { } } -TEST_F(SyncerThreadWithSyncerTest, Polling) { - SyncShareIntercept interceptor; - connection()->SetMidCommitObserver(&interceptor); - - const TimeDelta poll_interval = TimeDelta::FromSeconds(1); - syncer_thread()->SetSyncerShortPollInterval(poll_interval); - EXPECT_TRUE(syncer_thread()->Start()); - - // Calling Open() should cause the SyncerThread to create a Syncer. - metadb()->Open(); - - TimeDelta two_polls = poll_interval + poll_interval; - // We could theoretically return immediately from the wait if the interceptor - // was already signaled for a SyncShare (the first one comes quick). - interceptor.WaitForSyncShare(1, two_polls); - EXPECT_FALSE(interceptor.times_sync_occured().empty()); - - // Wait for at least 2 more SyncShare operations. - interceptor.WaitForSyncShare(2, two_polls); - EXPECT_TRUE(syncer_thread()->Stop(2000)); - - // Now analyze the run. - std::vector<Time> data = interceptor.times_sync_occured(); - - EXPECT_GE(data.size(), static_cast<unsigned int>(3)); - for (unsigned int i = 0; i < data.size() - 1; i++) { - Time optimal_next_sync = data[i] + poll_interval; - // The pthreads impl uses a different time impl and is slightly (~900usecs) - // off, so this expectation can fail with --syncer-thread-pthreads. - EXPECT_TRUE(data[i + 1] >= optimal_next_sync) - << "difference is " - << (data[i + 1] - optimal_next_sync).InMicroseconds() << " usecs. " - << "~900usec delta is OK with --syncer-thread-pthreads"; - // This should be reliable, as there are no blocking or I/O operations - // except the explicit 2 second wait, so if it takes longer than this - // there is a problem. - EXPECT_TRUE(data[i + 1] < optimal_next_sync + poll_interval); - } -} - -TEST_F(SyncerThreadWithSyncerTest, Nudge) { - SyncShareIntercept interceptor; - connection()->SetMidCommitObserver(&interceptor); - // We don't want a poll to happen during this test (except the first one). - const TimeDelta poll_interval = TimeDelta::FromMinutes(5); - syncer_thread()->SetSyncerShortPollInterval(poll_interval); - EXPECT_TRUE(syncer_thread()->Start()); - metadb()->Open(); - interceptor.WaitForSyncShare(1, poll_interval + poll_interval); - - EXPECT_EQ(static_cast<unsigned int>(1), - interceptor.times_sync_occured().size()); - // The SyncerThread should be waiting for the poll now. Nudge it to sync - // immediately (5ms). - syncer_thread()->NudgeSyncer(5, SyncerThread::kUnknown); - interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); - EXPECT_EQ(static_cast<unsigned int>(2), - interceptor.times_sync_occured().size()); - - // SyncerThread should be waiting again. Signal it to stop. - EXPECT_TRUE(syncer_thread()->Stop(2000)); -} - } // namespace browser_sync |