summaryrefslogtreecommitdiffstats
path: root/components/sync_driver/non_blocking_data_type_controller_unittest.cc
blob: 9514607d9a62c56f421f7caa5918bec19015972e (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2014 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 "base/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "components/sync_driver/non_blocking_data_type_controller.h"
#include "sync/engine/model_type_sync_proxy_impl.h"
#include "sync/engine/model_type_sync_worker.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/sync_context_proxy.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sync_driver {

namespace {

// A useless instance of ModelTypeSyncWorker.
class NullModelTypeSyncWorker : public syncer_v2::ModelTypeSyncWorker {
 public:
  NullModelTypeSyncWorker();
  ~NullModelTypeSyncWorker() override;

  void EnqueueForCommit(const syncer_v2::CommitRequestDataList& list) override;
};

NullModelTypeSyncWorker::NullModelTypeSyncWorker() {
}

NullModelTypeSyncWorker::~NullModelTypeSyncWorker() {
}

void NullModelTypeSyncWorker::EnqueueForCommit(
    const syncer_v2::CommitRequestDataList& list) {
  NOTREACHED() << "Not implemented.";
}

// A class that pretends to be the sync backend.
class MockSyncContext {
 public:
  void Connect(
      syncer::ModelType type,
      const scoped_refptr<base::SingleThreadTaskRunner>& model_task_runner,
      const base::WeakPtr<syncer_v2::ModelTypeSyncProxyImpl>& type_proxy) {
    enabled_types_.Put(type);
    model_task_runner->PostTask(
        FROM_HERE,
        base::Bind(&syncer_v2::ModelTypeSyncProxyImpl::OnConnect, type_proxy,
                   base::Passed(scoped_ptr<syncer_v2::ModelTypeSyncWorker>(
                                    new NullModelTypeSyncWorker())
                                    .Pass())));
  }

  void Disconnect(syncer::ModelType type) {
    DCHECK(enabled_types_.Has(type));
    enabled_types_.Remove(type);
  }

 private:
  std::list<base::Closure> tasks_;
  syncer::ModelTypeSet enabled_types_;
};

// A proxy to the MockSyncContext that implements SyncContextProxy.
class MockSyncContextProxy : public syncer_v2::SyncContextProxy {
 public:
  MockSyncContextProxy(
      MockSyncContext* sync_context,
      const scoped_refptr<base::TestSimpleTaskRunner>& model_task_runner,
      const scoped_refptr<base::TestSimpleTaskRunner>& sync_task_runner)
      : mock_sync_context_(sync_context),
        model_task_runner_(model_task_runner),
        sync_task_runner_(sync_task_runner) {}
  ~MockSyncContextProxy() override {}

  void ConnectTypeToSync(
      syncer::ModelType type,
      const syncer_v2::DataTypeState& data_type_state,
      const syncer_v2::UpdateResponseDataList& saved_pending_updates,
      const base::WeakPtr<syncer_v2::ModelTypeSyncProxyImpl>& type_proxy)
      override {
    // Normally we'd use ThreadTaskRunnerHandle::Get() as the TaskRunner
    // argument
    // to Connect().  That won't work here in this test, so we use the
    // model_task_runner_ that was injected for this purpose instead.
    sync_task_runner_->PostTask(FROM_HERE,
                                base::Bind(&MockSyncContext::Connect,
                                           base::Unretained(mock_sync_context_),
                                           type,
                                           model_task_runner_,
                                           type_proxy));
  }

  void Disconnect(syncer::ModelType type) override {
    sync_task_runner_->PostTask(FROM_HERE,
                                base::Bind(&MockSyncContext::Disconnect,
                                           base::Unretained(mock_sync_context_),
                                           type));
  }

  scoped_ptr<syncer_v2::SyncContextProxy> Clone() const override {
    return scoped_ptr<SyncContextProxy>(new MockSyncContextProxy(
        mock_sync_context_, model_task_runner_, sync_task_runner_));
  }

 private:
  MockSyncContext* mock_sync_context_;
  scoped_refptr<base::TestSimpleTaskRunner> model_task_runner_;
  scoped_refptr<base::TestSimpleTaskRunner> sync_task_runner_;
};

}  // namespace

class NonBlockingDataTypeControllerTest : public testing::Test {
 public:
  NonBlockingDataTypeControllerTest()
      : type_sync_proxy_(syncer::DICTIONARY),
        model_thread_(new base::TestSimpleTaskRunner()),
        sync_thread_(new base::TestSimpleTaskRunner()),
        controller_(syncer::DICTIONARY, true),
        mock_context_proxy_(&mock_sync_context_, model_thread_, sync_thread_),
        auto_run_tasks_(true) {}

  ~NonBlockingDataTypeControllerTest() override {}

  // Connects the sync type proxy to the NonBlockingDataTypeController.
  void InitTypeSyncProxy() {
    controller_.InitializeType(model_thread_,
                               type_sync_proxy_.AsWeakPtrForUI());
    if (auto_run_tasks_) {
      RunAllTasks();
    }
  }

  // Connects the sync backend to the NonBlockingDataTypeController.
  void InitSyncBackend() {
    controller_.InitializeSyncContext(mock_context_proxy_.Clone());
    if (auto_run_tasks_) {
      RunAllTasks();
    }
  }

  // Disconnects the sync backend from the NonBlockingDataTypeController.
  void UninitializeSyncBackend() {
    controller_.ClearSyncContext();
    if (auto_run_tasks_) {
      RunAllTasks();
    }
  }

  // Toggles the user's preference for syncing this type.
  void SetIsPreferred(bool preferred) {
    controller_.SetIsPreferred(preferred);
    if (auto_run_tasks_) {
      RunAllTasks();
    }
  }

  // These threads can ping-pong for a bit so we run the model thread twice.
  void RunAllTasks() {
    RunQueuedModelThreadTasks();
    RunQueuedSyncThreadTasks();
    RunQueuedModelThreadTasks();
  }

  // The sync type proxy pretends to run tasks on a different thread.
  // This function runs any posted tasks.
  void RunQueuedModelThreadTasks() { model_thread_->RunUntilIdle(); }

  // Processes any pending connect or disconnect requests and sends
  // responses synchronously.
  void RunQueuedSyncThreadTasks() { sync_thread_->RunUntilIdle(); }

  void SetAutoRunTasks(bool auto_run_tasks) {
    auto_run_tasks_ = auto_run_tasks;
  }

 protected:
  syncer_v2::ModelTypeSyncProxyImpl type_sync_proxy_;
  scoped_refptr<base::TestSimpleTaskRunner> model_thread_;
  scoped_refptr<base::TestSimpleTaskRunner> sync_thread_;

  NonBlockingDataTypeController controller_;

  MockSyncContext mock_sync_context_;
  MockSyncContextProxy mock_context_proxy_;

  bool auto_run_tasks_;
};

// Initialization when the user has disabled syncing for this type.
TEST_F(NonBlockingDataTypeControllerTest, UserDisabled) {
  SetIsPreferred(false);
  InitTypeSyncProxy();
  InitSyncBackend();

  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  UninitializeSyncBackend();

  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Init the sync backend then the type sync proxy.
TEST_F(NonBlockingDataTypeControllerTest, Enabled_SyncFirst) {
  SetIsPreferred(true);
  InitSyncBackend();
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  InitTypeSyncProxy();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  UninitializeSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Init the type sync proxy then the sync backend.
TEST_F(NonBlockingDataTypeControllerTest, Enabled_ProcessorFirst) {
  SetIsPreferred(true);
  InitTypeSyncProxy();
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  InitSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  UninitializeSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Initialize sync then disable it with a pref change.
TEST_F(NonBlockingDataTypeControllerTest, PreferThenNot) {
  SetIsPreferred(true);
  InitTypeSyncProxy();
  InitSyncBackend();

  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  SetIsPreferred(false);
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Connect type sync proxy and sync backend, then toggle prefs repeatedly.
TEST_F(NonBlockingDataTypeControllerTest, RepeatedTogglePreference) {
  SetIsPreferred(false);
  InitTypeSyncProxy();
  InitSyncBackend();
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  SetIsPreferred(true);
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  SetIsPreferred(false);
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  SetIsPreferred(true);
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  SetIsPreferred(false);
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Test sync backend getting restarted while processor is connected.
TEST_F(NonBlockingDataTypeControllerTest, RestartSyncBackend) {
  SetIsPreferred(true);
  InitTypeSyncProxy();
  InitSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());

  // Shutting down sync backend should disconnect but not disable the type.
  UninitializeSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  // Brining the backend back should reconnect the type.
  InitSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());
}

// Test sync backend being restarted before processor connects.
TEST_F(NonBlockingDataTypeControllerTest, RestartSyncBackendEarly) {
  SetIsPreferred(true);

  // Toggle sync off and on before the type sync proxy is available.
  InitSyncBackend();
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  UninitializeSyncBackend();
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  InitSyncBackend();
  EXPECT_FALSE(type_sync_proxy_.IsConnected());

  // Introduce the processor.
  InitTypeSyncProxy();
  EXPECT_TRUE(type_sync_proxy_.IsConnected());
}

// Test pref toggling before the sync backend has connected.
TEST_F(NonBlockingDataTypeControllerTest, TogglePreferenceWithoutBackend) {
  SetIsPreferred(true);
  InitTypeSyncProxy();

  // This should emit a disable signal.
  SetIsPreferred(false);
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());

  // This won't enable us, since we don't have a sync backend.
  SetIsPreferred(true);
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());

  // Only now do we start sending enable signals.
  InitSyncBackend();
  EXPECT_TRUE(type_sync_proxy_.IsConnected());
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
}

// Turns off auto-task-running to test the effects of delaying a connection
// response.
//
// This is mostly a test of the test framework.  It's not very interesting on
// its own, but it provides a useful "control" against some of the more
// complicated race tests below.
TEST_F(NonBlockingDataTypeControllerTest, DelayedConnect) {
  SetAutoRunTasks(false);

  SetIsPreferred(true);
  InitTypeSyncProxy();
  InitSyncBackend();

  // Allow the model to emit the request.
  RunQueuedModelThreadTasks();

  // That should result in a request to connect, but it won't be
  // executed right away.
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());

  // Let the sync thread process the request and the model thread handle its
  // response.
  RunQueuedSyncThreadTasks();
  RunQueuedModelThreadTasks();

  EXPECT_TRUE(type_sync_proxy_.IsConnected());
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
}

// Send Disable signal while a connection request is in progress.
TEST_F(NonBlockingDataTypeControllerTest, DisableRacesWithOnConnect) {
  SetAutoRunTasks(false);

  SetIsPreferred(true);
  InitTypeSyncProxy();
  InitSyncBackend();

  // Allow the model to emit the request.
  RunQueuedModelThreadTasks();

  // That should result in a request to connect, but it won't be
  // executed right away.
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());

  // Send and execute a disable signal before the OnConnect callback returns.
  SetIsPreferred(false);

  // Now we let sync process the initial request and the disable request,
  // both of which should be sitting in its queue.
  RunQueuedSyncThreadTasks();

  // Let the model thread process any responses received from the sync thread.
  // A plausible error would be that the sync thread returns a "connection OK"
  // message, and this message overrides the request to disable that arrived
  // from the UI thread earlier.  We need to make sure that doesn't happen.
  RunQueuedModelThreadTasks();

  EXPECT_FALSE(type_sync_proxy_.IsPreferred());
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
}

// Send a request to enable, then disable, then re-enable the data type.
//
// To make it more interesting, we stall the sync thread until all three
// requests have been passed to the model thread.
TEST_F(NonBlockingDataTypeControllerTest, EnableDisableEnableRace) {
  SetAutoRunTasks(false);

  SetIsPreferred(true);
  InitTypeSyncProxy();
  InitSyncBackend();
  RunQueuedModelThreadTasks();

  // That was the first enable.
  EXPECT_FALSE(type_sync_proxy_.IsConnected());
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());

  // Now disable.
  SetIsPreferred(false);
  RunQueuedModelThreadTasks();
  EXPECT_FALSE(type_sync_proxy_.IsPreferred());

  // And re-enable.
  SetIsPreferred(true);
  RunQueuedModelThreadTasks();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());

  // The sync thread has three messages related to those enables and
  // disables sittin in its queue.  Let's allow it to process them.
  RunQueuedSyncThreadTasks();

  // Let the model thread process any messages from the sync thread.
  RunQueuedModelThreadTasks();
  EXPECT_TRUE(type_sync_proxy_.IsPreferred());
  EXPECT_TRUE(type_sync_proxy_.IsConnected());
}

}  // namespace sync_driver