1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright 2013 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/basictypes.h"
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/sync_file_system/sync_task_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sync_file_system {
namespace {
void DumbTask(SyncStatusCode status,
const SyncStatusCallback& callback) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, status));
}
void IncrementAndAssign(int expected_before_counter,
int* counter,
SyncStatusCode* status_out,
SyncStatusCode status) {
EXPECT_EQ(expected_before_counter, *counter);
++(*counter);
*status_out = status;
}
template <typename T>
void IncrementAndAssignWithOwnedPointer(T* object,
int* counter,
SyncStatusCode* status_out,
SyncStatusCode status) {
++(*counter);
*status_out = status;
}
class TaskManagerClient
: public SyncTaskManager::Client,
public base::SupportsWeakPtr<TaskManagerClient> {
public:
TaskManagerClient()
: maybe_schedule_next_task_count_(0),
task_scheduled_count_(0),
idle_task_scheduled_count_(0),
last_operation_status_(SYNC_STATUS_OK) {
task_manager_.reset(new SyncTaskManager(AsWeakPtr()));
task_manager_->Initialize(SYNC_STATUS_OK);
maybe_schedule_next_task_count_ = 0;
}
virtual ~TaskManagerClient() {}
// DriveFileSyncManager::Client overrides.
virtual void MaybeScheduleNextTask() OVERRIDE {
++maybe_schedule_next_task_count_;
}
virtual void NotifyLastOperationStatus(
SyncStatusCode last_operation_status,
bool last_operation_used_network) OVERRIDE {
last_operation_status_ = last_operation_status;
}
void ScheduleTask(SyncStatusCode status_to_return,
const SyncStatusCallback& callback) {
task_manager_->ScheduleTask(
base::Bind(&TaskManagerClient::DoTask, AsWeakPtr(),
status_to_return, false /* idle */),
callback);
}
void ScheduleTaskIfIdle(SyncStatusCode status_to_return) {
task_manager_->ScheduleTaskIfIdle(
base::Bind(&TaskManagerClient::DoTask, AsWeakPtr(),
status_to_return, true /* idle */),
SyncStatusCallback());
}
int maybe_schedule_next_task_count() const {
return maybe_schedule_next_task_count_;
}
int task_scheduled_count() const { return task_scheduled_count_; }
int idle_task_scheduled_count() const { return idle_task_scheduled_count_; }
SyncStatusCode last_operation_status() const {
return last_operation_status_;
}
private:
void DoTask(SyncStatusCode status_to_return,
bool is_idle_task,
const SyncStatusCallback& callback) {
++task_scheduled_count_;
if (is_idle_task)
++idle_task_scheduled_count_;
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, status_to_return));
}
scoped_ptr<SyncTaskManager> task_manager_;
int maybe_schedule_next_task_count_;
int task_scheduled_count_;
int idle_task_scheduled_count_;
SyncStatusCode last_operation_status_;
DISALLOW_COPY_AND_ASSIGN(TaskManagerClient);
};
class MultihopSyncTask : public SyncTask {
public:
MultihopSyncTask(bool* task_started,
bool* task_completed)
: task_started_(task_started),
task_completed_(task_completed),
weak_ptr_factory_(this) {
DCHECK(task_started_);
DCHECK(task_completed_);
}
virtual ~MultihopSyncTask() {}
virtual void Run(const SyncStatusCallback& callback) OVERRIDE {
DCHECK(!*task_started_);
*task_started_ = true;
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&MultihopSyncTask::CompleteTask,
weak_ptr_factory_.GetWeakPtr(), callback));
}
private:
void CompleteTask(const SyncStatusCallback& callback) {
DCHECK(*task_started_);
DCHECK(!*task_completed_);
*task_completed_ = true;
callback.Run(SYNC_STATUS_OK);
}
bool* task_started_;
bool* task_completed_;
base::WeakPtrFactory<MultihopSyncTask> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MultihopSyncTask);
};
// Arbitrary non-default status values for testing.
const SyncStatusCode kStatus1 = static_cast<SyncStatusCode>(-1);
const SyncStatusCode kStatus2 = static_cast<SyncStatusCode>(-2);
const SyncStatusCode kStatus3 = static_cast<SyncStatusCode>(-3);
const SyncStatusCode kStatus4 = static_cast<SyncStatusCode>(-4);
const SyncStatusCode kStatus5 = static_cast<SyncStatusCode>(-5);
} // namespace
TEST(SyncTaskManagerTest, ScheduleTask) {
base::MessageLoop message_loop;
TaskManagerClient client;
int callback_count = 0;
SyncStatusCode callback_status = SYNC_STATUS_OK;
client.ScheduleTask(kStatus1, base::Bind(&IncrementAndAssign, 0,
&callback_count,
&callback_status));
message_loop.RunUntilIdle();
EXPECT_EQ(kStatus1, callback_status);
EXPECT_EQ(kStatus1, client.last_operation_status());
EXPECT_EQ(1, callback_count);
EXPECT_EQ(1, client.maybe_schedule_next_task_count());
EXPECT_EQ(1, client.task_scheduled_count());
EXPECT_EQ(0, client.idle_task_scheduled_count());
}
TEST(SyncTaskManagerTest, ScheduleTwoTasks) {
base::MessageLoop message_loop;
TaskManagerClient client;
int callback_count = 0;
SyncStatusCode callback_status = SYNC_STATUS_OK;
client.ScheduleTask(kStatus1, base::Bind(&IncrementAndAssign, 0,
&callback_count,
&callback_status));
client.ScheduleTask(kStatus2, base::Bind(&IncrementAndAssign, 1,
&callback_count,
&callback_status));
message_loop.RunUntilIdle();
EXPECT_EQ(kStatus2, callback_status);
EXPECT_EQ(kStatus2, client.last_operation_status());
EXPECT_EQ(2, callback_count);
EXPECT_EQ(1, client.maybe_schedule_next_task_count());
EXPECT_EQ(2, client.task_scheduled_count());
EXPECT_EQ(0, client.idle_task_scheduled_count());
}
TEST(SyncTaskManagerTest, ScheduleIdleTask) {
base::MessageLoop message_loop;
TaskManagerClient client;
client.ScheduleTaskIfIdle(kStatus1);
message_loop.RunUntilIdle();
EXPECT_EQ(kStatus1, client.last_operation_status());
EXPECT_EQ(1, client.maybe_schedule_next_task_count());
EXPECT_EQ(1, client.task_scheduled_count());
EXPECT_EQ(1, client.idle_task_scheduled_count());
}
TEST(SyncTaskManagerTest, ScheduleIdleTaskWhileNotIdle) {
base::MessageLoop message_loop;
TaskManagerClient client;
int callback_count = 0;
SyncStatusCode callback_status = SYNC_STATUS_OK;
client.ScheduleTask(kStatus1, base::Bind(&IncrementAndAssign, 0,
&callback_count,
&callback_status));
client.ScheduleTaskIfIdle(kStatus2);
message_loop.RunUntilIdle();
// Idle task must not have run.
EXPECT_EQ(kStatus1, callback_status);
EXPECT_EQ(kStatus1, client.last_operation_status());
EXPECT_EQ(1, callback_count);
EXPECT_EQ(1, client.maybe_schedule_next_task_count());
EXPECT_EQ(1, client.task_scheduled_count());
EXPECT_EQ(0, client.idle_task_scheduled_count());
}
TEST(SyncTaskManagerTest, ScheduleAndCancelSyncTask) {
base::MessageLoop message_loop;
int callback_count = 0;
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
bool task_started = false;
bool task_completed = false;
{
SyncTaskManager task_manager((base::WeakPtr<SyncTaskManager::Client>()));
task_manager.Initialize(SYNC_STATUS_OK);
task_manager.ScheduleSyncTask(
scoped_ptr<SyncTask>(new MultihopSyncTask(
&task_started, &task_completed)),
base::Bind(&IncrementAndAssign, 0, &callback_count, &status));
}
message_loop.RunUntilIdle();
EXPECT_EQ(0, callback_count);
EXPECT_EQ(SYNC_STATUS_UNKNOWN, status);
EXPECT_TRUE(task_started);
EXPECT_FALSE(task_completed);
}
TEST(SyncTaskManagerTest, ScheduleAndCancelTask) {
base::MessageLoop message_loop;
int callback_count = 0;
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
bool task_started = false;
bool task_completed = false;
{
SyncTaskManager task_manager((base::WeakPtr<SyncTaskManager::Client>()));
task_manager.Initialize(SYNC_STATUS_OK);
MultihopSyncTask* task = new MultihopSyncTask(
&task_started, &task_completed);
task_manager.ScheduleTask(
base::Bind(&MultihopSyncTask::Run, base::Unretained(task)),
base::Bind(&IncrementAndAssignWithOwnedPointer<MultihopSyncTask>,
base::Owned(task), &callback_count, &status));
}
message_loop.RunUntilIdle();
EXPECT_EQ(0, callback_count);
EXPECT_EQ(SYNC_STATUS_UNKNOWN, status);
EXPECT_TRUE(task_started);
EXPECT_FALSE(task_completed);
}
TEST(SyncTaskManagerTest, ScheduleTaskAtPriority) {
base::MessageLoop message_loop;
SyncTaskManager task_manager((base::WeakPtr<SyncTaskManager::Client>()));
task_manager.Initialize(SYNC_STATUS_OK);
int callback_count = 0;
SyncStatusCode callback_status1 = SYNC_STATUS_OK;
SyncStatusCode callback_status2 = SYNC_STATUS_OK;
SyncStatusCode callback_status3 = SYNC_STATUS_OK;
SyncStatusCode callback_status4 = SYNC_STATUS_OK;
SyncStatusCode callback_status5 = SYNC_STATUS_OK;
// This will run first even if its priority is low, since there're no
// pending tasks.
task_manager.ScheduleTaskAtPriority(
base::Bind(&DumbTask, kStatus1),
SyncTaskManager::PRIORITY_LOW,
base::Bind(&IncrementAndAssign, 0, &callback_count, &callback_status1));
// This runs last (expected counter == 4).
task_manager.ScheduleTaskAtPriority(
base::Bind(&DumbTask, kStatus2),
SyncTaskManager::PRIORITY_LOW,
base::Bind(&IncrementAndAssign, 4, &callback_count, &callback_status2));
// This runs second (expected counter == 1).
task_manager.ScheduleTaskAtPriority(
base::Bind(&DumbTask, kStatus3),
SyncTaskManager::PRIORITY_HIGH,
base::Bind(&IncrementAndAssign, 1, &callback_count, &callback_status3));
// This runs fourth (expected counter == 3).
task_manager.ScheduleTaskAtPriority(
base::Bind(&DumbTask, kStatus4),
SyncTaskManager::PRIORITY_MED,
base::Bind(&IncrementAndAssign, 3, &callback_count, &callback_status4));
// This runs third (expected counter == 2).
task_manager.ScheduleTaskAtPriority(
base::Bind(&DumbTask, kStatus5),
SyncTaskManager::PRIORITY_HIGH,
base::Bind(&IncrementAndAssign, 2, &callback_count, &callback_status5));
message_loop.RunUntilIdle();
EXPECT_EQ(kStatus1, callback_status1);
EXPECT_EQ(kStatus2, callback_status2);
EXPECT_EQ(kStatus3, callback_status3);
EXPECT_EQ(kStatus4, callback_status4);
EXPECT_EQ(kStatus5, callback_status5);
EXPECT_EQ(5, callback_count);
}
} // namespace sync_file_system
|