OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync_file_system/sync_process_runner.h" | 5 #include "chrome/browser/sync_file_system/sync_process_runner.h" |
6 | 6 |
7 #include <deque> | |
8 | |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace sync_file_system { | 12 namespace sync_file_system { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 class FakeClient : public SyncProcessRunner::Client { | 16 class FakeClient : public SyncProcessRunner::Client { |
15 public: | 17 public: |
16 FakeClient() : service_state_(SYNC_SERVICE_RUNNING) {} | 18 FakeClient() : service_state_(SYNC_SERVICE_RUNNING) {} |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 DISALLOW_COPY_AND_ASSIGN(FakeTimerHelper); | 83 DISALLOW_COPY_AND_ASSIGN(FakeTimerHelper); |
82 }; | 84 }; |
83 | 85 |
84 class FakeSyncProcessRunner : public SyncProcessRunner { | 86 class FakeSyncProcessRunner : public SyncProcessRunner { |
85 public: | 87 public: |
86 FakeSyncProcessRunner(SyncProcessRunner::Client* client, | 88 FakeSyncProcessRunner(SyncProcessRunner::Client* client, |
87 scoped_ptr<TimerHelper> timer_helper, | 89 scoped_ptr<TimerHelper> timer_helper, |
88 int max_parallel_task) | 90 int max_parallel_task) |
89 : SyncProcessRunner("FakeSyncProcess", | 91 : SyncProcessRunner("FakeSyncProcess", |
90 client, timer_helper.Pass(), | 92 client, timer_helper.Pass(), |
91 max_parallel_task) { | 93 max_parallel_task), |
94 max_parallel_task_(max_parallel_task) { | |
92 } | 95 } |
93 | 96 |
94 virtual void StartSync(const SyncStatusCallback& callback) OVERRIDE { | 97 virtual void StartSync(const SyncStatusCallback& callback) OVERRIDE { |
95 EXPECT_TRUE(running_task_.is_null()); | 98 EXPECT_LT(running_tasks_.size(), max_parallel_task_); |
96 running_task_ = callback; | 99 running_tasks_.push_back(callback); |
97 } | 100 } |
98 | 101 |
99 virtual ~FakeSyncProcessRunner() { | 102 virtual ~FakeSyncProcessRunner() { |
100 } | 103 } |
101 | 104 |
102 void UpdateChanges(int num_changes) { | 105 void UpdateChanges(int num_changes) { |
103 OnChangesUpdated(num_changes); | 106 OnChangesUpdated(num_changes); |
104 } | 107 } |
105 | 108 |
106 void CompleteTask(SyncStatusCode status) { | 109 void CompleteTask(SyncStatusCode status) { |
107 ASSERT_FALSE(running_task_.is_null()); | 110 ASSERT_FALSE(running_tasks_.empty()); |
108 SyncStatusCallback task = running_task_; | 111 SyncStatusCallback task = running_tasks_.front(); |
109 running_task_.Reset(); | 112 running_tasks_.pop_front(); |
110 task.Run(status); | 113 task.Run(status); |
111 } | 114 } |
112 | 115 |
113 bool HasRunningTask() const { | 116 bool HasRunningTask() const { |
114 return !running_task_.is_null(); | 117 return !running_tasks_.empty(); |
115 } | 118 } |
116 | 119 |
117 private: | 120 private: |
118 SyncStatusCallback running_task_; | 121 int64 max_parallel_task_; |
122 std::deque<SyncStatusCallback> running_tasks_; | |
119 | 123 |
120 DISALLOW_COPY_AND_ASSIGN(FakeSyncProcessRunner); | 124 DISALLOW_COPY_AND_ASSIGN(FakeSyncProcessRunner); |
121 }; | 125 }; |
122 | 126 |
123 } // namespace | 127 } // namespace |
124 | 128 |
125 TEST(SyncProcessRunnerTest, SingleTaskBasicTest) { | 129 TEST(SyncProcessRunnerTest, SingleTaskBasicTest) { |
126 FakeClient fake_client; | 130 FakeClient fake_client; |
127 FakeTimerHelper* fake_timer = new FakeTimerHelper(); | 131 FakeTimerHelper* fake_timer = new FakeTimerHelper(); |
128 FakeSyncProcessRunner fake_runner( | 132 FakeSyncProcessRunner fake_runner( |
129 &fake_client, | 133 &fake_client, |
130 scoped_ptr<SyncProcessRunner::TimerHelper>(fake_timer), | 134 scoped_ptr<SyncProcessRunner::TimerHelper>(fake_timer), |
131 1 /* max_parallel_task */); | 135 1 /* max_parallel_task */); |
132 | 136 |
133 base::TimeTicks base_time = base::TimeTicks::Now(); | 137 base::TimeTicks base_time = base::TimeTicks::Now(); |
134 fake_timer->SetCurrentTime(base_time); | 138 fake_timer->SetCurrentTime(base_time); |
135 | 139 |
136 // SyncProcessRunner is expected not to run a task initially. | 140 // SyncProcessRunner is expected not to run a task initially. |
137 EXPECT_FALSE(fake_timer->IsRunning()); | 141 EXPECT_FALSE(fake_timer->IsRunning()); |
138 | 142 |
139 // As soon as SyncProcessRunner gets a new update, it should start running | 143 // As soon as SyncProcessRunner gets a new update, it should start running |
140 // the timer to run a synchronization task. | 144 // the timer to run a synchronization task. |
141 fake_runner.UpdateChanges(100); | 145 fake_runner.UpdateChanges(100); |
142 EXPECT_TRUE(fake_timer->IsRunning()); | 146 EXPECT_TRUE(fake_timer->IsRunning()); |
143 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, | 147 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, |
144 fake_timer->GetCurrentDelay()); | 148 fake_timer->GetCurrentDelay()); |
145 | 149 |
146 // When the time has come, the timer should fire the scheduled task. | 150 // When the time has come, the timer should fire the scheduled task. |
147 fake_timer->AdvanceToScheduledTime(); | 151 fake_timer->AdvanceToScheduledTime(); |
148 EXPECT_FALSE(fake_timer->IsRunning()); | 152 // EXPECT_FALSE(fake_timer->IsRunning()); |
149 EXPECT_TRUE(fake_runner.HasRunningTask()); | 153 EXPECT_TRUE(fake_runner.HasRunningTask()); |
150 | 154 |
151 // Successful completion of the task fires next synchronization task. | 155 // Successful completion of the task fires next synchronization task. |
152 fake_runner.CompleteTask(SYNC_STATUS_OK); | 156 fake_runner.CompleteTask(SYNC_STATUS_OK); |
153 EXPECT_TRUE(fake_timer->IsRunning()); | 157 EXPECT_TRUE(fake_timer->IsRunning()); |
154 EXPECT_FALSE(fake_runner.HasRunningTask()); | 158 EXPECT_FALSE(fake_runner.HasRunningTask()); |
155 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, | 159 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, |
156 fake_timer->GetCurrentDelay()); | 160 fake_timer->GetCurrentDelay()); |
157 | 161 |
158 // Turn |service_state| to TEMPORARY_UNAVAILABLE and let the task fail. | 162 // Turn |service_state| to TEMPORARY_UNAVAILABLE and let the task fail. |
(...skipping 20 matching lines...) Expand all Loading... | |
179 | 183 |
180 // There's no item to sync anymore, SyncProcessRunner should schedules the | 184 // There's no item to sync anymore, SyncProcessRunner should schedules the |
181 // next with the longest delay. | 185 // next with the longest delay. |
182 fake_runner.UpdateChanges(0); | 186 fake_runner.UpdateChanges(0); |
183 fake_timer->AdvanceToScheduledTime(); | 187 fake_timer->AdvanceToScheduledTime(); |
184 fake_runner.CompleteTask(SYNC_STATUS_OK); | 188 fake_runner.CompleteTask(SYNC_STATUS_OK); |
185 EXPECT_EQ(SyncProcessRunner::kSyncDelayMaxInMilliseconds, | 189 EXPECT_EQ(SyncProcessRunner::kSyncDelayMaxInMilliseconds, |
186 fake_timer->GetCurrentDelay()); | 190 fake_timer->GetCurrentDelay()); |
187 } | 191 } |
188 | 192 |
193 TEST(SyncProcessRunnerTest, MultiTaskBasicTest) { | |
194 FakeClient fake_client; | |
195 FakeTimerHelper* fake_timer = new FakeTimerHelper(); | |
196 FakeSyncProcessRunner fake_runner( | |
197 &fake_client, | |
198 scoped_ptr<SyncProcessRunner::TimerHelper>(fake_timer), | |
199 2 /* max_parallel_task */); | |
200 | |
201 base::TimeTicks base_time = base::TimeTicks::Now(); | |
202 fake_timer->SetCurrentTime(base_time); | |
203 | |
204 EXPECT_FALSE(fake_timer->IsRunning()); | |
205 | |
206 fake_runner.UpdateChanges(100); | |
207 EXPECT_TRUE(fake_timer->IsRunning()); | |
208 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, | |
209 fake_timer->GetCurrentDelay()); | |
210 | |
211 // Even after a task starts running, SyncProcessRunner should schedule next | |
212 // task until the number of running task reachs the limit. | |
213 fake_timer->AdvanceToScheduledTime(); | |
214 EXPECT_TRUE(fake_timer->IsRunning()); | |
215 EXPECT_TRUE(fake_runner.HasRunningTask()); | |
216 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, | |
217 fake_timer->GetCurrentDelay()); | |
218 | |
219 // After the second task starts running, SyncProcessRunner should stop | |
220 // scheduling a task. | |
221 fake_timer->AdvanceToScheduledTime(); | |
222 EXPECT_FALSE(fake_timer->IsRunning()); | |
223 EXPECT_TRUE(fake_runner.HasRunningTask()); | |
224 | |
225 fake_runner.CompleteTask(SYNC_STATUS_OK); | |
226 EXPECT_TRUE(fake_timer->IsRunning()); | |
227 EXPECT_TRUE(fake_runner.HasRunningTask()); | |
228 fake_runner.CompleteTask(SYNC_STATUS_OK); | |
229 EXPECT_TRUE(fake_timer->IsRunning()); | |
230 EXPECT_FALSE(fake_runner.HasRunningTask()); | |
231 | |
232 // Turn |service_state| to TEMPORARY_UNAVAILABLE and let the task fail. | |
233 // |fake_runner| should schedule following tasks with longer delay. | |
234 fake_timer->AdvanceToScheduledTime(); | |
235 fake_timer->AdvanceToScheduledTime(); | |
236 fake_client.set_service_state(SYNC_SERVICE_TEMPORARY_UNAVAILABLE); | |
237 fake_runner.CompleteTask(SYNC_STATUS_FAILED); | |
238 EXPECT_EQ(SyncProcessRunner::kSyncDelaySlowInMilliseconds, | |
239 fake_timer->GetCurrentDelay()); | |
240 | |
241 // Consecutive error reports shouldn't extend delay immediately. | |
242 fake_runner.CompleteTask(SYNC_STATUS_FAILED); | |
243 EXPECT_EQ(SyncProcessRunner::kSyncDelaySlowInMilliseconds, | |
244 fake_timer->GetCurrentDelay()); | |
245 | |
246 // The next task will run after throttle period is over. | |
247 // And its failure should extend the throttle period by twice. | |
248 fake_timer->AdvanceToScheduledTime(); | |
249 EXPECT_EQ(SyncProcessRunner::kSyncDelaySlowInMilliseconds, | |
250 fake_timer->GetCurrentDelay()); | |
251 fake_runner.CompleteTask(SYNC_STATUS_FAILED); | |
252 EXPECT_EQ(2 * SyncProcessRunner::kSyncDelaySlowInMilliseconds, | |
253 fake_timer->GetCurrentDelay()); | |
254 | |
255 // Next successful task should clear the throttling. | |
256 fake_timer->AdvanceToScheduledTime(); | |
257 fake_client.set_service_state(SYNC_SERVICE_RUNNING); | |
258 fake_runner.CompleteTask(SYNC_STATUS_OK); | |
259 EXPECT_EQ(SyncProcessRunner::kSyncDelayFastInMilliseconds, | |
260 fake_timer->GetCurrentDelay()); | |
261 | |
262 // Then, following failing task should not extend throttling period. | |
263 fake_timer->AdvanceToScheduledTime(); | |
264 fake_client.set_service_state(SYNC_SERVICE_TEMPORARY_UNAVAILABLE); | |
265 fake_runner.CompleteTask(SYNC_STATUS_FAILED); | |
266 EXPECT_EQ(SyncProcessRunner::kSyncDelaySlowInMilliseconds, | |
267 fake_timer->GetCurrentDelay()); | |
nhiroki
2014/07/14 02:27:06
How about testing DelayMax case, that is, AUTHENTI
tzik
2014/07/14 04:52:04
Done.
| |
268 } | |
269 | |
189 } // namespace sync_file_system | 270 } // namespace sync_file_system |
OLD | NEW |