OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync_file_system/sync_process_runner.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace sync_file_system { |
| 11 |
| 12 namespace { |
| 13 |
| 14 class FakeClient : public SyncProcessRunner::Client { |
| 15 public: |
| 16 FakeClient() : service_state_(SYNC_SERVICE_RUNNING) {} |
| 17 virtual ~FakeClient() {} |
| 18 |
| 19 virtual SyncServiceState GetSyncServiceState() OVERRIDE { |
| 20 return service_state_; |
| 21 } |
| 22 |
| 23 virtual SyncFileSystemService* GetSyncService() OVERRIDE { |
| 24 return NULL; |
| 25 } |
| 26 |
| 27 void set_service_state(SyncServiceState service_state) { |
| 28 service_state_ = service_state; |
| 29 } |
| 30 |
| 31 private: |
| 32 SyncServiceState service_state_; |
| 33 |
| 34 DISALLOW_COPY_AND_ASSIGN(FakeClient); |
| 35 }; |
| 36 |
| 37 class FakeTimerHelper : public SyncProcessRunner::TimerHelper { |
| 38 public: |
| 39 FakeTimerHelper() {} |
| 40 virtual ~FakeTimerHelper() {} |
| 41 |
| 42 virtual bool IsRunning() OVERRIDE { |
| 43 return !timer_task_.is_null(); |
| 44 } |
| 45 |
| 46 virtual void Start(const tracked_objects::Location& from_here, |
| 47 const base::TimeDelta& delay, |
| 48 const base::Closure& closure) OVERRIDE { |
| 49 scheduled_time_ = current_time_ + delay; |
| 50 timer_task_ = closure; |
| 51 } |
| 52 |
| 53 virtual base::TimeTicks Now() const OVERRIDE { |
| 54 return current_time_; |
| 55 } |
| 56 |
| 57 void SetCurrentTime(const base::TimeTicks& current_time) { |
| 58 current_time_ = current_time; |
| 59 if (current_time_ < scheduled_time_ || timer_task_.is_null()) |
| 60 return; |
| 61 |
| 62 base::Closure task = timer_task_; |
| 63 timer_task_.Reset(); |
| 64 task.Run(); |
| 65 } |
| 66 |
| 67 void AdvanceToScheduledTime() { |
| 68 SetCurrentTime(scheduled_time_); |
| 69 } |
| 70 |
| 71 int64 GetCurrentDelay() { |
| 72 EXPECT_FALSE(timer_task_.is_null()); |
| 73 return (scheduled_time_ - current_time_).InMilliseconds(); |
| 74 } |
| 75 |
| 76 private: |
| 77 base::TimeTicks current_time_; |
| 78 base::TimeTicks scheduled_time_; |
| 79 base::Closure timer_task_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(FakeTimerHelper); |
| 82 }; |
| 83 |
| 84 class FakeSyncProcessRunner : public SyncProcessRunner { |
| 85 public: |
| 86 FakeSyncProcessRunner(SyncProcessRunner::Client* client, |
| 87 scoped_ptr<TimerHelper> timer_helper, |
| 88 int max_parallel_task) |
| 89 : SyncProcessRunner("FakeSyncProcess", |
| 90 client, timer_helper.Pass(), |
| 91 max_parallel_task) { |
| 92 } |
| 93 |
| 94 virtual void StartSync(const SyncStatusCallback& callback) OVERRIDE { |
| 95 EXPECT_TRUE(running_task_.is_null()); |
| 96 running_task_ = callback; |
| 97 } |
| 98 |
| 99 virtual ~FakeSyncProcessRunner() { |
| 100 } |
| 101 |
| 102 void UpdateChanges(int num_changes) { |
| 103 OnChangesUpdated(num_changes); |
| 104 } |
| 105 |
| 106 void CompleteTask(SyncStatusCode status) { |
| 107 ASSERT_FALSE(running_task_.is_null()); |
| 108 SyncStatusCallback task = running_task_; |
| 109 running_task_.Reset(); |
| 110 task.Run(status); |
| 111 } |
| 112 |
| 113 bool HasRunningTask() const { |
| 114 return !running_task_.is_null(); |
| 115 } |
| 116 |
| 117 private: |
| 118 SyncStatusCallback running_task_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(FakeSyncProcessRunner); |
| 121 }; |
| 122 |
| 123 } // namespace |
| 124 |
| 125 TEST(SyncProcessRunnerTest, SingleTaskBasicTest) { |
| 126 FakeClient fake_client; |
| 127 FakeTimerHelper* fake_timer = new FakeTimerHelper(); |
| 128 FakeSyncProcessRunner fake_runner( |
| 129 &fake_client, |
| 130 scoped_ptr<SyncProcessRunner::TimerHelper>(fake_timer), |
| 131 1 /* max_parallel_task */); |
| 132 |
| 133 base::TimeTicks base_time = base::TimeTicks::Now(); |
| 134 fake_timer->SetCurrentTime(base_time); |
| 135 |
| 136 // SyncProcessRunner is expected not to run a task initially. |
| 137 EXPECT_FALSE(fake_timer->IsRunning()); |
| 138 |
| 139 // As soon as SyncProcessRunner gets a new update, it should start running |
| 140 // the timer to run a synchronization task. |
| 141 fake_runner.UpdateChanges(100); |
| 142 EXPECT_TRUE(fake_timer->IsRunning()); |
| 143 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, |
| 144 fake_timer->GetCurrentDelay()); |
| 145 |
| 146 // When the time has come, the timer should fire the scheduled task. |
| 147 fake_timer->AdvanceToScheduledTime(); |
| 148 EXPECT_FALSE(fake_timer->IsRunning()); |
| 149 EXPECT_TRUE(fake_runner.HasRunningTask()); |
| 150 |
| 151 // Successful completion of the task fires next synchronization task. |
| 152 fake_runner.CompleteTask(SYNC_STATUS_OK); |
| 153 EXPECT_TRUE(fake_timer->IsRunning()); |
| 154 EXPECT_FALSE(fake_runner.HasRunningTask()); |
| 155 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, |
| 156 fake_timer->GetCurrentDelay()); |
| 157 |
| 158 // Turn |service_state| to TEMPORARY_UNAVAILABLE and let the task fail. |
| 159 // |fake_runner| should schedule following tasks with longer delay. |
| 160 fake_timer->AdvanceToScheduledTime(); |
| 161 fake_client.set_service_state(SYNC_SERVICE_TEMPORARY_UNAVAILABLE); |
| 162 fake_runner.CompleteTask(SYNC_STATUS_FAILED); |
| 163 EXPECT_EQ(SyncProcessRunner::kSyncDelaySlowInMilliseconds, |
| 164 fake_timer->GetCurrentDelay()); |
| 165 |
| 166 // Repeated failure makes the task delay back off. |
| 167 fake_timer->AdvanceToScheduledTime(); |
| 168 fake_runner.CompleteTask(SYNC_STATUS_FAILED); |
| 169 EXPECT_EQ(2 * SyncProcessRunner::kSyncDelaySlowInMilliseconds, |
| 170 fake_timer->GetCurrentDelay()); |
| 171 |
| 172 // After |service_state| gets back to normal state, SyncProcessRunner should |
| 173 // restart rapid task invocation. |
| 174 fake_client.set_service_state(SYNC_SERVICE_RUNNING); |
| 175 fake_timer->AdvanceToScheduledTime(); |
| 176 fake_runner.CompleteTask(SYNC_STATUS_OK); |
| 177 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, |
| 178 fake_timer->GetCurrentDelay()); |
| 179 |
| 180 // There's no item to sync anymore, SyncProcessRunner should schedules the |
| 181 // next with the longest delay. |
| 182 fake_runner.UpdateChanges(0); |
| 183 fake_timer->AdvanceToScheduledTime(); |
| 184 fake_runner.CompleteTask(SYNC_STATUS_OK); |
| 185 EXPECT_EQ(SyncProcessRunner::kSyncDelayMaxInMilliseconds, |
| 186 fake_timer->GetCurrentDelay()); |
| 187 } |
| 188 |
| 189 } // namespace sync_file_system |
OLD | NEW |