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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
// Copyright (c) 2009 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 <list>
#include <map>
#include <set>
#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::TimeTicks;
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();
}
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(TimeTicks::Now());
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<TimeTicks> times_sync_occured() const {
return times_sync_occured_;
}
private:
std::vector<TimeTicks> times_sync_occured_;
base::WaitableEvent sync_occured_;
DISALLOW_COPY_AND_ASSIGN(SyncShareIntercept);
};
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));
}
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);
syncer_thread->SetSyncerPollingInterval(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));
// user_idle_ms is ge than 10 * (syncer_polling_interval*1000).
int last_poll_time = 2000;
ASSERT_TRUE(last_poll_time <=
syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000));
ASSERT_TRUE(last_poll_time * 3 >=
syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000));
ASSERT_TRUE(last_poll_time <=
syncer_thread->CalculateSyncWaitTime(last_poll_time, 100000));
ASSERT_TRUE(last_poll_time * 3 >=
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_TRUE(near_threshold <=
syncer_thread->CalculateSyncWaitTime(near_threshold, 10000));
ASSERT_TRUE(SyncerThread::kDefaultMaxPollIntervalMs >=
syncer_thread->CalculateSyncWaitTime(near_threshold, 10000));
ASSERT_TRUE(SyncerThread::kDefaultMaxPollIntervalMs ==
syncer_thread->CalculateSyncWaitTime(threshold, 10000));
ASSERT_TRUE(SyncerThread::kDefaultMaxPollIntervalMs ==
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);
ASSERT_TRUE(last_poll_time <=
syncer_thread->CalculateSyncWaitTime(last_poll_time,
over_sync_max_interval));
ASSERT_TRUE(last_poll_time * 3 >=
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();
// Notifications disabled should result in a polling interval of
// kDefaultShortPollInterval.
{
AllStatus::Status status = {};
status.notifications_enabled = 0;
bool continue_sync_cycle_param = false;
// No work and no backoff.
ASSERT_TRUE(SyncerThread::kDefaultShortPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
// In this case the continue_sync_cycle is turned off.
continue_sync_cycle_param = true;
ASSERT_TRUE(SyncerThread::kDefaultShortPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
// TODO(brg) : Find a way to test exponential backoff is inoperable.
// Exponential backoff should be turned on when notifications are disabled
// but this can not be tested since we can not set the last input info.
}
// Notifications enabled should result in a polling interval of
// SyncerThread::kDefaultLongPollIntervalSeconds.
{
AllStatus::Status status = {};
status.notifications_enabled = 1;
bool continue_sync_cycle_param = false;
// No work and no backoff.
ASSERT_TRUE(SyncerThread::kDefaultLongPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
// In this case the continue_sync_cycle is turned off.
continue_sync_cycle_param = true;
ASSERT_TRUE(SyncerThread::kDefaultLongPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
// TODO(brg) : Find a way to test exponential backoff.
// Exponential backoff should be turned off when notifications are enabled,
// but this can not be tested since we can not set the last input info.
}
// There are two states which can cause a continuation, either the updates
// available do not match the updates received, or the unsynced count is
// non-zero.
{
AllStatus::Status status = {};
status.updates_available = 1;
status.updates_received = 0;
bool continue_sync_cycle_param = false;
ASSERT_TRUE(0 <= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
continue_sync_cycle_param = false;
ASSERT_TRUE(3 >= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
ASSERT_TRUE(0 <= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(2 >= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
status.updates_received = 1;
ASSERT_TRUE(SyncerThread::kDefaultShortPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
10,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
}
{
AllStatus::Status status = {};
status.unsynced_count = 1;
bool continue_sync_cycle_param = false;
ASSERT_TRUE(0 <= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
continue_sync_cycle_param = false;
ASSERT_TRUE(2 >= syncer_thread->CalculatePollingWaitTime(
status,
0,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
status.unsynced_count = 0;
ASSERT_TRUE(SyncerThread::kDefaultShortPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
4,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
}
// Regression for exponential backoff reset when the syncer is nudged.
{
AllStatus::Status status = {};
status.unsynced_count = 1;
bool continue_sync_cycle_param = false;
// Expect move from default polling interval to exponential backoff due to
// unsynced_count != 0.
ASSERT_TRUE(0 <= syncer_thread->CalculatePollingWaitTime(
status,
3600,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
continue_sync_cycle_param = false;
ASSERT_TRUE(2 >= syncer_thread->CalculatePollingWaitTime(
status,
3600,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
// Expect exponential backoff.
ASSERT_TRUE(2 <= syncer_thread->CalculatePollingWaitTime(
status,
2,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(6 >= syncer_thread->CalculatePollingWaitTime(
status,
2,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
// A nudge resets the continue_sync_cycle_param value, so our backoff
// should return to the minimum.
continue_sync_cycle_param = false;
ASSERT_TRUE(0 <= syncer_thread->CalculatePollingWaitTime(
status,
3600,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
continue_sync_cycle_param = false;
ASSERT_TRUE(2 >= syncer_thread->CalculatePollingWaitTime(
status,
3600,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_TRUE(continue_sync_cycle_param);
// Setting unsynced_count = 0 returns us to the default polling interval.
status.unsynced_count = 0;
ASSERT_TRUE(SyncerThread::kDefaultShortPollIntervalSeconds ==
syncer_thread->CalculatePollingWaitTime(
status,
4,
&user_idle_milliseconds_param,
&continue_sync_cycle_param));
ASSERT_FALSE(continue_sync_cycle_param);
}
}
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<TimeTicks> data = interceptor.times_sync_occured();
EXPECT_GE(data.size(), static_cast<unsigned int>(3));
for (unsigned int i = 0; i < data.size() - 1; i++) {
TimeTicks optimal_next_sync = data[i] + poll_interval;
EXPECT_TRUE(data[i + 1] >= optimal_next_sync);
// 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
|