OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <list> | 5 #include <list> |
6 #include <map> | 6 #include <map> |
7 #include <set> | 7 #include <set> |
8 #include <strstream> | 8 #include <strstream> |
9 | 9 |
| 10 #include "base/command_line.h" |
10 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/time.h" |
| 13 #include "chrome/browser/sync/engine/model_safe_worker.h" |
11 #include "chrome/browser/sync/engine/syncer_thread.h" | 14 #include "chrome/browser/sync/engine/syncer_thread.h" |
| 15 #include "chrome/browser/sync/engine/syncer_thread_timed_stop.h" |
| 16 #include "chrome/test/sync/engine/mock_server_connection.h" |
| 17 #include "chrome/test/sync/engine/test_directory_setter_upper.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
13 | 19 |
| 20 using base::Time; |
| 21 using base::TimeDelta; |
| 22 |
14 namespace browser_sync { | 23 namespace browser_sync { |
15 | 24 |
16 class SyncerThreadTest : public testing::Test { | 25 typedef testing::Test SyncerThreadTest; |
17 protected: | |
18 SyncerThreadTest() {} | |
19 virtual ~SyncerThreadTest() {} | |
20 virtual void SetUp() {} | |
21 virtual void TearDown() {} | |
22 | 26 |
| 27 class SyncerThreadWithSyncerTest : public testing::Test { |
| 28 public: |
| 29 SyncerThreadWithSyncerTest() {} |
| 30 virtual void SetUp() { |
| 31 metadb_.SetUp(); |
| 32 connection_.reset(new MockConnectionManager(metadb_.manager(), |
| 33 metadb_.name())); |
| 34 allstatus_.reset(new AllStatus()); |
| 35 |
| 36 syncer_thread_ = SyncerThreadFactory::Create(NULL, metadb_.manager(), |
| 37 connection_.get(), allstatus_.get(), new ModelSafeWorker()); |
| 38 |
| 39 allstatus_->WatchSyncerThread(syncer_thread_); |
| 40 syncer_thread_->SetConnected(true); |
| 41 } |
| 42 virtual void TearDown() { |
| 43 syncer_thread_ = NULL; |
| 44 allstatus_.reset(); |
| 45 connection_.reset(); |
| 46 metadb_.TearDown(); |
| 47 } |
| 48 |
| 49 ManuallyOpenedTestDirectorySetterUpper* metadb() { return &metadb_; } |
| 50 MockConnectionManager* connection() { return connection_.get(); } |
| 51 SyncerThread* syncer_thread() { return syncer_thread_; } |
23 private: | 52 private: |
24 DISALLOW_COPY_AND_ASSIGN(SyncerThreadTest); | 53 ManuallyOpenedTestDirectorySetterUpper metadb_; |
| 54 scoped_ptr<MockConnectionManager> connection_; |
| 55 scoped_ptr<AllStatus> allstatus_; |
| 56 scoped_refptr<SyncerThread> syncer_thread_; |
| 57 DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); |
| 58 }; |
| 59 |
| 60 class SyncShareIntercept : public MockConnectionManager::MidCommitObserver { |
| 61 public: |
| 62 SyncShareIntercept() : sync_occured_(false, false) {} |
| 63 virtual ~SyncShareIntercept() {} |
| 64 virtual void Observe() { |
| 65 times_sync_occured_.push_back(Time::NowFromSystemTime()); |
| 66 sync_occured_.Signal(); |
| 67 } |
| 68 void WaitForSyncShare(int at_least_this_many, TimeDelta max_wait) { |
| 69 while (at_least_this_many-- > 0) |
| 70 sync_occured_.TimedWait(max_wait); |
| 71 } |
| 72 std::vector<Time> times_sync_occured() const { |
| 73 return times_sync_occured_; |
| 74 } |
| 75 private: |
| 76 std::vector<Time> times_sync_occured_; |
| 77 base::WaitableEvent sync_occured_; |
| 78 DISALLOW_COPY_AND_ASSIGN(SyncShareIntercept); |
25 }; | 79 }; |
26 | 80 |
27 TEST_F(SyncerThreadTest, Construction) { | 81 TEST_F(SyncerThreadTest, Construction) { |
28 SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); | 82 scoped_refptr<SyncerThread> syncer_thread( |
| 83 SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); |
| 84 } |
| 85 |
| 86 TEST_F(SyncerThreadTest, StartStop) { |
| 87 scoped_refptr<SyncerThread> syncer_thread( |
| 88 SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); |
| 89 EXPECT_TRUE(syncer_thread->Start()); |
| 90 EXPECT_TRUE(syncer_thread->Stop(2000)); |
| 91 |
| 92 // Do it again for good measure. I caught some bugs by adding this so |
| 93 // I would recommend keeping it. |
| 94 EXPECT_TRUE(syncer_thread->Start()); |
| 95 EXPECT_TRUE(syncer_thread->Stop(2000)); |
29 } | 96 } |
30 | 97 |
31 TEST_F(SyncerThreadTest, CalculateSyncWaitTime) { | 98 TEST_F(SyncerThreadTest, CalculateSyncWaitTime) { |
32 SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); | 99 scoped_refptr<SyncerThread> syncer_thread( |
33 syncer_thread.DisableIdleDetection(); | 100 SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); |
| 101 syncer_thread->DisableIdleDetection(); |
34 | 102 |
35 // Syncer_polling_interval_ is less than max poll interval | 103 // Syncer_polling_interval_ is less than max poll interval. |
36 int syncer_polling_interval = 1; // Needed since AssertionResult is not a | 104 TimeDelta syncer_polling_interval = TimeDelta::FromSeconds(1); |
37 // friend of SyncerThread | 105 |
38 syncer_thread.syncer_polling_interval_ = syncer_polling_interval; | 106 syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); |
39 | 107 |
40 // user_idle_ms is less than 10 * (syncer_polling_interval*1000). | 108 // user_idle_ms is less than 10 * (syncer_polling_interval*1000). |
41 ASSERT_EQ(syncer_polling_interval * 1000, | 109 ASSERT_EQ(syncer_polling_interval.InMilliseconds(), |
42 syncer_thread.CalculateSyncWaitTime(1000, 0)); | 110 syncer_thread->CalculateSyncWaitTime(1000, 0)); |
43 ASSERT_EQ(syncer_polling_interval * 1000, | 111 ASSERT_EQ(syncer_polling_interval.InMilliseconds(), |
44 syncer_thread.CalculateSyncWaitTime(1000, 1)); | 112 syncer_thread->CalculateSyncWaitTime(1000, 1)); |
45 | 113 |
46 // user_idle_ms is ge than 10 * (syncer_polling_interval*1000). | 114 // user_idle_ms is ge than 10 * (syncer_polling_interval*1000). |
47 int last_poll_time = 2000; | 115 int last_poll_time = 2000; |
48 ASSERT_LE(last_poll_time, | 116 ASSERT_LE(last_poll_time, |
49 syncer_thread.CalculateSyncWaitTime(last_poll_time, 10000)); | 117 syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000)); |
50 ASSERT_GE(last_poll_time*3, | 118 ASSERT_GE(last_poll_time*3, |
51 syncer_thread.CalculateSyncWaitTime(last_poll_time, 10000)); | 119 syncer_thread->CalculateSyncWaitTime(last_poll_time, 10000)); |
52 ASSERT_LE(last_poll_time, | 120 ASSERT_LE(last_poll_time, |
53 syncer_thread.CalculateSyncWaitTime(last_poll_time, 100000)); | 121 syncer_thread->CalculateSyncWaitTime(last_poll_time, 100000)); |
54 ASSERT_GE(last_poll_time*3, | 122 ASSERT_GE(last_poll_time*3, |
55 syncer_thread.CalculateSyncWaitTime(last_poll_time, 100000)); | 123 syncer_thread->CalculateSyncWaitTime(last_poll_time, 100000)); |
56 | 124 |
57 // Maximum backoff time should be syncer_max_interval. | 125 // Maximum backoff time should be syncer_max_interval. |
58 int near_threshold = SyncerThread::kDefaultMaxPollIntervalMs / 2 - 1; | 126 int near_threshold = SyncerThread::kDefaultMaxPollIntervalMs / 2 - 1; |
59 int threshold = SyncerThread::kDefaultMaxPollIntervalMs; | 127 int threshold = SyncerThread::kDefaultMaxPollIntervalMs; |
60 int over_threshold = SyncerThread::kDefaultMaxPollIntervalMs + 1; | 128 int over_threshold = SyncerThread::kDefaultMaxPollIntervalMs + 1; |
61 ASSERT_LE(near_threshold, | 129 ASSERT_LE(near_threshold, |
62 syncer_thread.CalculateSyncWaitTime(near_threshold, 10000)); | 130 syncer_thread->CalculateSyncWaitTime(near_threshold, 10000)); |
63 ASSERT_GE(SyncerThread::kDefaultMaxPollIntervalMs, | 131 ASSERT_GE(SyncerThread::kDefaultMaxPollIntervalMs, |
64 syncer_thread.CalculateSyncWaitTime(near_threshold, 10000)); | 132 syncer_thread->CalculateSyncWaitTime(near_threshold, 10000)); |
65 ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, | 133 ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, |
66 syncer_thread.CalculateSyncWaitTime(threshold, 10000)); | 134 syncer_thread->CalculateSyncWaitTime(threshold, 10000)); |
67 ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, | 135 ASSERT_EQ(SyncerThread::kDefaultMaxPollIntervalMs, |
68 syncer_thread.CalculateSyncWaitTime(over_threshold, 10000)); | 136 syncer_thread->CalculateSyncWaitTime(over_threshold, 10000)); |
69 | 137 |
70 // Possible idle time must be capped by syncer_max_interval. | 138 // Possible idle time must be capped by syncer_max_interval. |
71 int over_sync_max_interval = | 139 int over_sync_max_interval = |
72 SyncerThread::kDefaultMaxPollIntervalMs + 1; | 140 SyncerThread::kDefaultMaxPollIntervalMs + 1; |
73 syncer_polling_interval = over_sync_max_interval / 100; // so 1000* is right | 141 syncer_polling_interval = TimeDelta::FromSeconds( |
74 syncer_thread.syncer_polling_interval_ = syncer_polling_interval; | 142 over_sync_max_interval / 100); // so 1000* is right |
75 ASSERT_EQ(syncer_polling_interval * 1000, | 143 syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); |
76 syncer_thread.CalculateSyncWaitTime(1000, over_sync_max_interval)); | 144 ASSERT_EQ(syncer_polling_interval.InSeconds() * 1000, |
77 syncer_polling_interval = 1; | 145 syncer_thread->CalculateSyncWaitTime(1000, over_sync_max_interval)); |
78 syncer_thread.syncer_polling_interval_ = syncer_polling_interval; | 146 syncer_polling_interval = TimeDelta::FromSeconds(1); |
| 147 syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); |
79 ASSERT_LE(last_poll_time, | 148 ASSERT_LE(last_poll_time, |
80 syncer_thread.CalculateSyncWaitTime(last_poll_time, | 149 syncer_thread->CalculateSyncWaitTime(last_poll_time, |
81 over_sync_max_interval)); | 150 over_sync_max_interval)); |
82 ASSERT_GE(last_poll_time * 3, | 151 ASSERT_GE(last_poll_time * 3, |
83 syncer_thread.CalculateSyncWaitTime(last_poll_time, | 152 syncer_thread->CalculateSyncWaitTime(last_poll_time, |
84 over_sync_max_interval)); | 153 over_sync_max_interval)); |
85 } | 154 } |
86 | 155 |
87 TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { | 156 TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { |
88 // Set up the environment. | 157 // Set up the environment. |
89 int user_idle_milliseconds_param = 0; | 158 int user_idle_milliseconds_param = 0; |
90 | 159 scoped_refptr<SyncerThread> syncer_thread( |
91 SyncerThread syncer_thread(NULL, NULL, NULL, NULL, NULL); | 160 SyncerThreadFactory::Create(NULL, NULL, NULL, NULL, NULL)); |
92 syncer_thread.DisableIdleDetection(); | 161 syncer_thread->DisableIdleDetection(); |
93 | 162 |
94 // Notifications disabled should result in a polling interval of | 163 // Notifications disabled should result in a polling interval of |
95 // kDefaultShortPollInterval. | 164 // kDefaultShortPollInterval. |
96 { | 165 { |
97 AllStatus::Status status = {}; | 166 AllStatus::Status status = {}; |
98 status.notifications_enabled = 0; | 167 status.notifications_enabled = 0; |
99 bool continue_sync_cycle_param = false; | 168 bool continue_sync_cycle_param = false; |
100 | 169 |
101 // No work and no backoff. | 170 // No work and no backoff. |
102 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 171 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
103 syncer_thread.CalculatePollingWaitTime( | 172 syncer_thread->CalculatePollingWaitTime( |
104 status, | 173 status, |
105 0, | 174 0, |
106 &user_idle_milliseconds_param, | 175 &user_idle_milliseconds_param, |
107 &continue_sync_cycle_param)); | 176 &continue_sync_cycle_param)); |
108 ASSERT_FALSE(continue_sync_cycle_param); | 177 ASSERT_FALSE(continue_sync_cycle_param); |
109 | 178 |
110 // In this case the continue_sync_cycle is turned off. | 179 // In this case the continue_sync_cycle is turned off. |
111 continue_sync_cycle_param = true; | 180 continue_sync_cycle_param = true; |
112 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 181 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
113 syncer_thread.CalculatePollingWaitTime( | 182 syncer_thread->CalculatePollingWaitTime( |
114 status, | 183 status, |
115 0, | 184 0, |
116 &user_idle_milliseconds_param, | 185 &user_idle_milliseconds_param, |
117 &continue_sync_cycle_param)); | 186 &continue_sync_cycle_param)); |
118 ASSERT_FALSE(continue_sync_cycle_param); | 187 ASSERT_FALSE(continue_sync_cycle_param); |
119 | 188 |
120 // TODO(brg) : Find a way to test exponential backoff is inoperable. | 189 // TODO(brg) : Find a way to test exponential backoff is inoperable. |
121 // Exponential backoff should be turned on when notifications are disabled | 190 // Exponential backoff should be turned on when notifications are disabled |
122 // but this can not be tested since we can not set the last input info. | 191 // but this can not be tested since we can not set the last input info. |
123 } | 192 } |
124 | 193 |
125 // Notifications enabled should result in a polling interval of | 194 // Notifications enabled should result in a polling interval of |
126 // SyncerThread::kDefaultLongPollIntervalSeconds. | 195 // SyncerThread::kDefaultLongPollIntervalSeconds. |
127 { | 196 { |
128 AllStatus::Status status = {}; | 197 AllStatus::Status status = {}; |
129 status.notifications_enabled = 1; | 198 status.notifications_enabled = 1; |
130 bool continue_sync_cycle_param = false; | 199 bool continue_sync_cycle_param = false; |
131 | 200 |
132 // No work and no backoff. | 201 // No work and no backoff. |
133 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 202 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
134 syncer_thread.CalculatePollingWaitTime( | 203 syncer_thread->CalculatePollingWaitTime( |
135 status, | 204 status, |
136 0, | 205 0, |
137 &user_idle_milliseconds_param, | 206 &user_idle_milliseconds_param, |
138 &continue_sync_cycle_param)); | 207 &continue_sync_cycle_param)); |
139 ASSERT_FALSE(continue_sync_cycle_param); | 208 ASSERT_FALSE(continue_sync_cycle_param); |
140 | 209 |
141 // In this case the continue_sync_cycle is turned off. | 210 // In this case the continue_sync_cycle is turned off. |
142 continue_sync_cycle_param = true; | 211 continue_sync_cycle_param = true; |
143 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 212 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
144 syncer_thread.CalculatePollingWaitTime( | 213 syncer_thread->CalculatePollingWaitTime( |
145 status, | 214 status, |
146 0, | 215 0, |
147 &user_idle_milliseconds_param, | 216 &user_idle_milliseconds_param, |
148 &continue_sync_cycle_param)); | 217 &continue_sync_cycle_param)); |
149 ASSERT_FALSE(continue_sync_cycle_param); | 218 ASSERT_FALSE(continue_sync_cycle_param); |
150 | 219 |
151 // TODO(brg) : Find a way to test exponential backoff. | 220 // TODO(brg) : Find a way to test exponential backoff. |
152 // Exponential backoff should be turned off when notifications are enabled, | 221 // Exponential backoff should be turned off when notifications are enabled, |
153 // but this can not be tested since we can not set the last input info. | 222 // but this can not be tested since we can not set the last input info. |
154 } | 223 } |
155 | 224 |
156 // There are two states which can cause a continuation, either the updates | 225 // There are two states which can cause a continuation, either the updates |
157 // available do not match the updates received, or the unsynced count is | 226 // available do not match the updates received, or the unsynced count is |
158 // non-zero. | 227 // non-zero. |
159 { | 228 { |
160 AllStatus::Status status = {}; | 229 AllStatus::Status status = {}; |
161 status.updates_available = 1; | 230 status.updates_available = 1; |
162 status.updates_received = 0; | 231 status.updates_received = 0; |
163 bool continue_sync_cycle_param = false; | 232 bool continue_sync_cycle_param = false; |
164 | 233 |
165 ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( | 234 ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( |
166 status, | 235 status, |
167 0, | 236 0, |
168 &user_idle_milliseconds_param, | 237 &user_idle_milliseconds_param, |
169 &continue_sync_cycle_param)); | 238 &continue_sync_cycle_param)); |
170 ASSERT_TRUE(continue_sync_cycle_param); | 239 ASSERT_TRUE(continue_sync_cycle_param); |
171 | 240 |
172 continue_sync_cycle_param = false; | 241 continue_sync_cycle_param = false; |
173 ASSERT_GE(3, syncer_thread.CalculatePollingWaitTime( | 242 ASSERT_GE(3, syncer_thread->CalculatePollingWaitTime( |
174 status, | 243 status, |
175 0, | 244 0, |
176 &user_idle_milliseconds_param, | 245 &user_idle_milliseconds_param, |
177 &continue_sync_cycle_param)); | 246 &continue_sync_cycle_param)); |
178 ASSERT_TRUE(continue_sync_cycle_param); | 247 ASSERT_TRUE(continue_sync_cycle_param); |
179 | 248 |
180 ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( | 249 ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( |
181 status, | 250 status, |
182 0, | 251 0, |
183 &user_idle_milliseconds_param, | 252 &user_idle_milliseconds_param, |
184 &continue_sync_cycle_param)); | 253 &continue_sync_cycle_param)); |
185 ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( | 254 ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( |
186 status, | 255 status, |
187 0, | 256 0, |
188 &user_idle_milliseconds_param, | 257 &user_idle_milliseconds_param, |
189 &continue_sync_cycle_param)); | 258 &continue_sync_cycle_param)); |
190 ASSERT_TRUE(continue_sync_cycle_param); | 259 ASSERT_TRUE(continue_sync_cycle_param); |
191 | 260 |
192 status.updates_received = 1; | 261 status.updates_received = 1; |
193 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 262 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
194 syncer_thread.CalculatePollingWaitTime( | 263 syncer_thread->CalculatePollingWaitTime( |
195 status, | 264 status, |
196 10, | 265 10, |
197 &user_idle_milliseconds_param, | 266 &user_idle_milliseconds_param, |
198 &continue_sync_cycle_param)); | 267 &continue_sync_cycle_param)); |
199 ASSERT_FALSE(continue_sync_cycle_param); | 268 ASSERT_FALSE(continue_sync_cycle_param); |
200 } | 269 } |
201 | 270 |
202 { | 271 { |
203 AllStatus::Status status = {}; | 272 AllStatus::Status status = {}; |
204 status.unsynced_count = 1; | 273 status.unsynced_count = 1; |
205 bool continue_sync_cycle_param = false; | 274 bool continue_sync_cycle_param = false; |
206 | 275 |
207 ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( | 276 ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( |
208 status, | 277 status, |
209 0, | 278 0, |
210 &user_idle_milliseconds_param, | 279 &user_idle_milliseconds_param, |
211 &continue_sync_cycle_param)); | 280 &continue_sync_cycle_param)); |
212 ASSERT_TRUE(continue_sync_cycle_param); | 281 ASSERT_TRUE(continue_sync_cycle_param); |
213 | 282 |
214 continue_sync_cycle_param = false; | 283 continue_sync_cycle_param = false; |
215 ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( | 284 ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( |
216 status, | 285 status, |
217 0, | 286 0, |
218 &user_idle_milliseconds_param, | 287 &user_idle_milliseconds_param, |
219 &continue_sync_cycle_param)); | 288 &continue_sync_cycle_param)); |
220 ASSERT_TRUE(continue_sync_cycle_param); | 289 ASSERT_TRUE(continue_sync_cycle_param); |
221 | 290 |
222 status.unsynced_count = 0; | 291 status.unsynced_count = 0; |
223 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 292 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
224 syncer_thread.CalculatePollingWaitTime( | 293 syncer_thread->CalculatePollingWaitTime( |
225 status, | 294 status, |
226 4, | 295 4, |
227 &user_idle_milliseconds_param, | 296 &user_idle_milliseconds_param, |
228 &continue_sync_cycle_param)); | 297 &continue_sync_cycle_param)); |
229 ASSERT_FALSE(continue_sync_cycle_param); | 298 ASSERT_FALSE(continue_sync_cycle_param); |
230 } | 299 } |
231 | 300 |
232 // Regression for exponential backoff reset when the syncer is nudged. | 301 // Regression for exponential backoff reset when the syncer is nudged. |
233 { | 302 { |
234 AllStatus::Status status = {}; | 303 AllStatus::Status status = {}; |
235 status.unsynced_count = 1; | 304 status.unsynced_count = 1; |
236 bool continue_sync_cycle_param = false; | 305 bool continue_sync_cycle_param = false; |
237 | 306 |
238 // Expect move from default polling interval to exponential backoff due to | 307 // Expect move from default polling interval to exponential backoff due to |
239 // unsynced_count != 0. | 308 // unsynced_count != 0. |
240 ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( | 309 ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( |
241 status, | 310 status, |
242 3600, | 311 3600, |
243 &user_idle_milliseconds_param, | 312 &user_idle_milliseconds_param, |
244 &continue_sync_cycle_param)); | 313 &continue_sync_cycle_param)); |
245 ASSERT_TRUE(continue_sync_cycle_param); | 314 ASSERT_TRUE(continue_sync_cycle_param); |
246 | 315 |
247 continue_sync_cycle_param = false; | 316 continue_sync_cycle_param = false; |
248 ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( | 317 ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( |
249 status, | 318 status, |
250 3600, | 319 3600, |
251 &user_idle_milliseconds_param, | 320 &user_idle_milliseconds_param, |
252 &continue_sync_cycle_param)); | 321 &continue_sync_cycle_param)); |
253 ASSERT_TRUE(continue_sync_cycle_param); | 322 ASSERT_TRUE(continue_sync_cycle_param); |
254 | 323 |
255 // Expect exponential backoff. | 324 // Expect exponential backoff. |
256 ASSERT_LE(2, syncer_thread.CalculatePollingWaitTime( | 325 ASSERT_LE(2, syncer_thread->CalculatePollingWaitTime( |
257 status, | 326 status, |
258 2, | 327 2, |
259 &user_idle_milliseconds_param, | 328 &user_idle_milliseconds_param, |
260 &continue_sync_cycle_param)); | 329 &continue_sync_cycle_param)); |
261 ASSERT_GE(6, syncer_thread.CalculatePollingWaitTime( | 330 ASSERT_GE(6, syncer_thread->CalculatePollingWaitTime( |
262 status, | 331 status, |
263 2, | 332 2, |
264 &user_idle_milliseconds_param, | 333 &user_idle_milliseconds_param, |
265 &continue_sync_cycle_param)); | 334 &continue_sync_cycle_param)); |
266 ASSERT_TRUE(continue_sync_cycle_param); | 335 ASSERT_TRUE(continue_sync_cycle_param); |
267 | 336 |
268 // A nudge resets the continue_sync_cycle_param value, so our backoff | 337 // A nudge resets the continue_sync_cycle_param value, so our backoff |
269 // should return to the minimum. | 338 // should return to the minimum. |
270 continue_sync_cycle_param = false; | 339 continue_sync_cycle_param = false; |
271 ASSERT_LE(0, syncer_thread.CalculatePollingWaitTime( | 340 ASSERT_LE(0, syncer_thread->CalculatePollingWaitTime( |
272 status, | 341 status, |
273 3600, | 342 3600, |
274 &user_idle_milliseconds_param, | 343 &user_idle_milliseconds_param, |
275 &continue_sync_cycle_param)); | 344 &continue_sync_cycle_param)); |
276 ASSERT_TRUE(continue_sync_cycle_param); | 345 ASSERT_TRUE(continue_sync_cycle_param); |
277 | 346 |
278 continue_sync_cycle_param = false; | 347 continue_sync_cycle_param = false; |
279 ASSERT_GE(2, syncer_thread.CalculatePollingWaitTime( | 348 ASSERT_GE(2, syncer_thread->CalculatePollingWaitTime( |
280 status, | 349 status, |
281 3600, | 350 3600, |
282 &user_idle_milliseconds_param, | 351 &user_idle_milliseconds_param, |
283 &continue_sync_cycle_param)); | 352 &continue_sync_cycle_param)); |
284 ASSERT_TRUE(continue_sync_cycle_param); | 353 ASSERT_TRUE(continue_sync_cycle_param); |
285 | 354 |
286 // Setting unsynced_count = 0 returns us to the default polling interval. | 355 // Setting unsynced_count = 0 returns us to the default polling interval. |
287 status.unsynced_count = 0; | 356 status.unsynced_count = 0; |
288 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 357 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
289 syncer_thread.CalculatePollingWaitTime( | 358 syncer_thread->CalculatePollingWaitTime( |
290 status, | 359 status, |
291 4, | 360 4, |
292 &user_idle_milliseconds_param, | 361 &user_idle_milliseconds_param, |
293 &continue_sync_cycle_param)); | 362 &continue_sync_cycle_param)); |
294 ASSERT_FALSE(continue_sync_cycle_param); | 363 ASSERT_FALSE(continue_sync_cycle_param); |
295 } | 364 } |
296 } | 365 } |
297 | 366 |
| 367 TEST_F(SyncerThreadWithSyncerTest, Polling) { |
| 368 SyncShareIntercept interceptor; |
| 369 connection()->SetMidCommitObserver(&interceptor); |
| 370 |
| 371 const TimeDelta poll_interval = TimeDelta::FromSeconds(1); |
| 372 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
| 373 EXPECT_TRUE(syncer_thread()->Start()); |
| 374 |
| 375 // Calling Open() should cause the SyncerThread to create a Syncer. |
| 376 metadb()->Open(); |
| 377 |
| 378 TimeDelta two_polls = poll_interval + poll_interval; |
| 379 // We could theoretically return immediately from the wait if the interceptor |
| 380 // was already signaled for a SyncShare (the first one comes quick). |
| 381 interceptor.WaitForSyncShare(1, two_polls); |
| 382 EXPECT_FALSE(interceptor.times_sync_occured().empty()); |
| 383 |
| 384 // Wait for at least 2 more SyncShare operations. |
| 385 interceptor.WaitForSyncShare(2, two_polls); |
| 386 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 387 |
| 388 // Now analyze the run. |
| 389 std::vector<Time> data = interceptor.times_sync_occured(); |
| 390 |
| 391 EXPECT_GE(data.size(), static_cast<unsigned int>(3)); |
| 392 for (unsigned int i = 0; i < data.size() - 1; i++) { |
| 393 Time optimal_next_sync = data[i] + poll_interval; |
| 394 // The pthreads impl uses a different time impl and is slightly (~900usecs) |
| 395 // off, so this expectation can fail with --syncer-thread-pthreads. |
| 396 EXPECT_TRUE(data[i + 1] >= optimal_next_sync) |
| 397 << "difference is " |
| 398 << (data[i + 1] - optimal_next_sync).InMicroseconds() << " usecs. " |
| 399 << "~900usec delta is OK with --syncer-thread-pthreads"; |
| 400 // This should be reliable, as there are no blocking or I/O operations |
| 401 // except the explicit 2 second wait, so if it takes longer than this |
| 402 // there is a problem. |
| 403 EXPECT_TRUE(data[i + 1] < optimal_next_sync + poll_interval); |
| 404 } |
| 405 } |
| 406 |
| 407 TEST_F(SyncerThreadWithSyncerTest, Nudge) { |
| 408 SyncShareIntercept interceptor; |
| 409 connection()->SetMidCommitObserver(&interceptor); |
| 410 // We don't want a poll to happen during this test (except the first one). |
| 411 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 412 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
| 413 EXPECT_TRUE(syncer_thread()->Start()); |
| 414 metadb()->Open(); |
| 415 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 416 |
| 417 EXPECT_EQ(static_cast<unsigned int>(1), |
| 418 interceptor.times_sync_occured().size()); |
| 419 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 420 // immediately (5ms). |
| 421 syncer_thread()->NudgeSyncer(5, SyncerThread::kUnknown); |
| 422 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 423 EXPECT_EQ(static_cast<unsigned int>(2), |
| 424 interceptor.times_sync_occured().size()); |
| 425 |
| 426 // SyncerThread should be waiting again. Signal it to stop. |
| 427 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 428 } |
| 429 |
298 } // namespace browser_sync | 430 } // namespace browser_sync |
OLD | NEW |