forked from pouchdb/pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.replication.js
613 lines (558 loc) · 19.7 KB
/
test.replication.js
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*globals initTestDB: false, emit: true, generateAdapterUrl: false */
/*globals PERSIST_DATABASES: false, initDBPair: false, openTestDB: false */
"use strict";
var adapters = [
['local-1', 'http-1'],
['http-1', 'http-2'],
['http-1', 'local-1'],
['local-1', 'local-2']];
var qunit = module;
var downAdapters = ['local-1'];
var deletedDocAdapters = [['local-1', 'http-1']];
// if we are running under node.js, set things up
// a little differently, and only test the leveldb adapter
if (typeof module !== undefined && module.exports) {
var Pouch = require('../src/pouch.js');
var LevelPouch = require('../src/adapters/pouch.leveldb.js');
var utils = require('./test.utils.js');
for (var k in utils) {
global[k] = global[k] || utils[k];
}
qunit = QUnit.module;
downAdapters = [];
}
adapters.map(function(adapters) {
qunit('replication: ' + adapters[0] + ':' + adapters[1], {
setup : function () {
this.name = generateAdapterUrl(adapters[0]);
this.remote = generateAdapterUrl(adapters[1]);
},
teardown: function() {
if (!PERSIST_DATABASES) {
Pouch.destroy(this.name);
Pouch.destroy(this.remote);
}
}
});
var docs = [
{_id: "0", integer: 0, string: '0'},
{_id: "1", integer: 1, string: '1'},
{_id: "2", integer: 2, string: '2'}
];
asyncTest("Test basic pull replication", function() {
console.info('Starting Test: Test basic pull replication');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, results) {
db.replicate.from(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
ok(result.docs_written === docs.length, 'correct # docs written');
start();
});
});
});
});
asyncTest("Local DB contains documents", function() {
console.info('Starting Test: Local DB contains documents');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, _) {
db.bulkDocs({docs: docs}, {}, function(err, _) {
db.replicate.from(self.remote, function(err, _) {
db.allDocs(function(err, result) {
ok(result.rows.length === docs.length, 'correct # docs exist');
start();
});
});
});
});
});
});
asyncTest("Test basic push replication", function() {
console.info('Starting Test: Test basic push replication');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
db.bulkDocs({docs: docs}, {}, function(err, results) {
db.replicate.to(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
ok(result.docs_written === docs.length, 'correct # docs written');
start();
});
});
});
});
asyncTest("Test basic push replication take 2", function() {
console.info('Starting Test: Test basic push replication take 2');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
db.bulkDocs({docs: docs}, {}, function(err, _) {
db.replicate.to(self.remote, function(err, _) {
remote.allDocs(function(err, result) {
console.log(result);
ok(result.rows.length === docs.length, 'correct # docs written');
start();
});
});
});
});
});
asyncTest("Test basic push replication sequence tracking", function() {
console.info('Starting Test: Test basic push replication sequence tracking');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
var doc1 = {_id: 'adoc', foo:'bar'};
db.put(doc1, function(err, result) {
db.replicate.to(self.remote, function(err, result) {
ok(result.docs_read === 1, 'correct # changed docs read (1) on first replication');
db.replicate.to(self.remote, function(err, result) {
ok(result.docs_read === 0, 'correct # changed docs read (0) on second replication');
db.replicate.to(self.remote, function(err, result) {
ok(result.docs_read === 0, 'correct # changed docs read (0) on third replication');
start();
});
});
});
});
});
});
asyncTest("Test checkpoint", function() {
console.info('Starting Test: Test checkpoint');
var self = this;
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, results) {
db.replicate.from(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
ok(result.docs_written === docs.length, 'correct # docs written');
db.replicate.from(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
equal(result.docs_written, 0, 'correct # docs written');
equal(result.docs_read, 0, 'no docs read');
start();
});
});
});
});
});
asyncTest("Test checkpoint 2", function() {
console.info('Starting Test: Test checkpoint 2');
var self = this;
var doc = {_id: "3", count: 0};
initDBPair(this.name, this.remote, function(db, remote) {
remote.put(doc, {}, function(err, results) {
db.replicate.from(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
doc._rev = results.rev;
doc.count++;
remote.put(doc, {}, function(err, results) {
doc._rev = results.rev;
doc.count++;
remote.put(doc, {}, function(err, results) {
db.replicate.from(self.remote, function(err, result) {
ok(result.ok, 'replication was ok');
equal(result.docs_written, 1, 'correct # docs written');
start();
});
});
});
});
});
});
});
asyncTest("Test checkpoint 3 :)", function() {
console.info('Starting Test: Test checkpoint 3 :)');
var self = this;
var doc = {_id: "3", count: 0};
initDBPair(this.name, this.remote, function(db, remote) {
db.put(doc, {}, function(err, results) {
Pouch.replicate(db, remote, {}, function(err, result) {
ok(result.ok, 'replication was ok');
doc._rev = results.rev;
doc.count++;
db.put(doc, {}, function(err, results) {
doc._rev = results.rev;
doc.count++;
db.put(doc, {}, function(err, results) {
Pouch.replicate(db, remote, {}, function(err, result) {
ok(result.ok, 'replication was ok');
ok(result.docs_written === 1, 'correct # docs written');
start();
});
});
});
});
});
});
});
// CouchDB will not generate a conflict here, it uses a deteministic
// method to generate the revision number, however we cannot copy its
// method as it depends on erlangs internal data representation
asyncTest("Test basic conflict", function() {
console.info('Starting Test: Test basic conflict');
var self = this;
var doc1 = {_id: 'adoc', foo:'bar'};
var doc2 = {_id: 'adoc', bar:'baz'};
initDBPair(this.name, this.remote, function(db, remote) {
db.put(doc1, function(err, localres) {
remote.put(doc2, function(err, remoteres) {
db.replicate.to(self.remote, function(err, _) {
remote.get('adoc', {conflicts: true}, function(err, result) {
ok(result._conflicts, 'result has a conflict');
start();
});
});
});
});
});
});
asyncTest("Test _conflicts key", function() {
console.info("Starting Test: Testing _conflicts key");
var self = this;
var doc1 = {_id: 'adoc', foo:'bar'};
var doc2 = {_id: 'adoc', bar:'baz'};
initDBPair(this.name, this.remote, function(db, remote) {
db.put(doc1, function(err, localres) {
remote.put(doc2, function(err, remoteres) {
db.replicate.to(self.remote, function(err, _) {
var queryFun = {
map: function(doc) {
if (doc._conflicts) {
emit(doc._id, [doc._rev].concat(doc._conflicts));
}
}
};
remote.query(queryFun, {reduce: false, conflicts: true}, function(_, res) {
equal(res.rows.length, 1, "_conflict key exists");
start();
});
});
});
});
});
});
asyncTest("Test basic continous pull replication", function() {
console.info('Starting Test: Test basic continous pull replication');
var self = this;
var doc1 = {_id: 'adoc', foo:'bar'};
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, results) {
var count = 0;
var rep = db.replicate.from(self.remote, {continuous: true});
var changes = db.changes({
onChange: function(change) {
++count;
if (count === 3) {
return remote.put(doc1);
}
if (count === 4) {
ok(true, 'Got all the changes');
rep.cancel();
changes.cancel();
start();
}
},
continuous: true
});
});
});
});
asyncTest("Test basic continous push replication", function() {
console.info('Starting Test: Test basic continous push replication');
var self = this;
var doc1 = {_id: 'adoc', foo:'bar'};
initDBPair(this.name, this.remote, function(db, remote) {
db.bulkDocs({docs: docs}, {}, function(err, results) {
var count = 0;
var rep = remote.replicate.from(db, {continuous: true});
var changes = remote.changes({
onChange: function(change) {
++count;
if (count === 3) {
return db.put(doc1);
}
if (count === 4) {
ok(true, 'Got all the changes');
rep.cancel();
changes.cancel();
start();
}
},
continuous: true
});
});
});
});
asyncTest("Test cancel pull replication", function() {
console.info('Starting Test: Test cancel pull replication');
var self = this;
var doc1 = {_id: 'adoc', foo:'bar'};
var doc2 = {_id: 'anotherdoc', foo:'baz'};
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, results) {
var count = 0;
var replicate = db.replicate.from(self.remote, {continuous: true});
var changes = db.changes({
continuous: true,
onChange: function(change) {
++count;
if (count === 3) {
remote.put(doc1);
}
if (count === 4) {
replicate.cancel();
remote.put(doc2);
// This setTimeout is needed to ensure no further changes come through
setTimeout(function() {
ok(count === 4, 'got no more docs');
changes.cancel();
start();
}, 500);
}
}
});
});
});
});
asyncTest("Replication filter", function() {
console.info('Starting Test: Replication filter');
var docs1 = [
{_id: "0", integer: 0},
{_id: "1", integer: 1},
{_id: "2", integer: 2},
{_id: "3", integer: 3}
];
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs1}, function(err, info) {
var replicate = db.replicate.from(remote, {
filter: function(doc) { return doc.integer % 2 === 0; }
}, function() {
db.allDocs(function(err, docs) {
equal(docs.rows.length, 2);
replicate.cancel();
start();
});
});
});
});
});
asyncTest("Replication with different filters", function() {
console.info('Starting Test: Replication with different filters');
var more_docs = [
{_id: '3', integer: 3, string: '3'},
{_id: '4', integer: 4, string: '4'}
];
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, function(err, info) {
db.replicate.from(remote, {
filter: function(doc) { return doc.integer % 2 === 0; }
}, function(err, response){
remote.bulkDocs({docs:more_docs}, function(err, info) {
db.replicate.from(remote, {}, function(err, response) {
ok(response.docs_written === 3,'correct # of docs replicated');
start();
});
});
});
});
});
});
asyncTest("Replication with same filters", function() {
console.info('Starting Test: Replication with same filters');
var more_docs = [
{_id: '3', integer: 3, string: '3'},
{_id: '4', integer: 4, string: '4'}
];
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, function(err, info) {
db.replicate.from(remote, {
filter: function(doc) { return doc.integer % 2 === 0; }
}, function(err, response){
db.allDocs(function(err, docs) {console.log(docs);});
remote.bulkDocs({docs:more_docs}, function(err, info) {
db.replicate.from(remote, {
filter: function(doc) { return doc.integer % 2 === 0; }
}, function(err, response) {
ok(response.docs_written === 1,'correct # of docs replicated');
start();
});
});
});
});
});
});
asyncTest("Replication with deleted doc", function() {
console.info('Starting Test: Replication with deleted doc');
var docs1 = [
{_id: "0", integer: 0},
{_id: "1", integer: 1},
{_id: "2", integer: 2},
{_id: "3", integer: 3},
{_id: "4", integer: 4, _deleted: true}
];
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs1}, function(err, info) {
var replicate = db.replicate.from(remote, function() {
db.allDocs(function(err, res) {
equal(res.total_rows, 4, 'Replication with deleted docs');
start();
});
});
});
});
});
asyncTest("Replication notifications", function() {
console.info('Starting Test: replication notifications');
var self = this;
var changes = 0;
var onChange = function(c) {
changes++;
ok(true, 'Got change notification');
if (changes === 3) {
ok(true, 'Got all change notification');
start();
}
};
initDBPair(this.name, this.remote, function(db, remote) {
remote.bulkDocs({docs: docs}, {}, function(err, results) {
db.replicate.from(self.remote, {onChange: onChange});
});
});
});
asyncTest("Replication with remote conflict", function() {
var doc = {_id: 'test', test: "Remote 1"},
winningRev;
initDBPair(this.name, this.remote, function(db, remote) {
remote.post(doc, function(err, resp) {
doc._rev = resp.rev;
Pouch.replicate(remote, db, function(err, resp) {
doc.test = "Local 1";
db.put(doc, function(err, resp) {
doc.test = "Remote 2";
remote.put(doc, function(err, resp) {
doc._rev = resp.rev;
doc.test = "Remote 3";
remote.put(doc, function(err, resp) {
winningRev = resp.rev;
Pouch.replicate(db, remote, function(err, resp) {
Pouch.replicate(remote, db, function(err, resp) {
remote.get('test', {revs_info: true}, function(err, remotedoc) {
db.get('test', {revs_info: true}, function(err, localdoc) {
equal(localdoc._rev, winningRev, "Local chose correct winning revision");
equal(remotedoc._rev, winningRev, "Remote chose winning revision");
start();
});
});
});
});
});
});
});
});
});
});
});
});
// test a basic "initialize pouch" scenario when couch instance contains deleted revisions
// currently testing idb-http only
deletedDocAdapters.map(function(adapters) {
qunit('replication: ' + adapters[0] + ':' + adapters[1], {
setup : function () {
this.name = generateAdapterUrl(adapters[0]);
this.remote = generateAdapterUrl(adapters[1]);
}
});
asyncTest("doc count after multiple replications with deleted revisions.", function() {
var self = this;
var runs = 2;
// helper. remove each document in db and bulk load docs into same
function rebuildDocuments(db, docs, callback) {
db.allDocs({include_docs:true}, function (err, response) {
var count = 0;
var limit = response.rows.length;
if (limit === 0) {
bulkLoad(db, docs, callback);
}
response.rows.forEach(function(doc) {
db.remove(doc, function(err, response) {
if (err) {
console.error(err);
}
++count;
if (count === limit){
bulkLoad(db, docs, callback);
}
});
});
});
}
// helper.
function bulkLoad(db, docs, callback){
db.bulkDocs({docs:docs}, function (err, results) {
if (err) {
console.error("Unable to bulk load docs. Err: " + JSON.stringify(err));
return;
}
callback(results);
});
}
// a basic map function to mimic our testing situation
function map(doc) {
if (doc.common === true) {
emit(doc._id, doc.rev);
}
}
// The number of workflow cycles to perform. 2+ was always failing
// reason for this test.
var workflow = function(name, remote, x){
// some documents. note that the variable Date component,
//thisVaries, makes a difference.
// when the document is otherwise static, couch gets the same hash
// when calculating revision.
// and the revisions get messed up in pouch
var docs = [
{_id: "0", integer: 0, thisVaries: new Date(), common: true},
{_id: "1", integer: 1, thisVaries: new Date(), common: true},
{_id: "2", integer: 2, thisVaries: new Date(), common: true},
{_id: "3", integer: 3, thisVaries: new Date(), common: true}
];
openTestDB(remote, function(err, dbr){
rebuildDocuments(dbr, docs, function(){
openTestDB(name, function(err, db){
db.replicate.from(remote, function(err, result) {
db.query({map:map}, {reduce: false}, function (err, result) {
equal(result.rows.length, docs.length, "correct # docs replicated");
if (--x) {
workflow(name, remote, x);
} else {
start();
}
});
});
});
});
});
};
// new pouch and couch
initDBPair(self.name, self.remote, function(){
// Rinse, repeat our workflow...
workflow(self.name, self.remote, runs);
});
});
});
// This test only needs to run for one configuration, and it slows stuff
// down
downAdapters.map(function(adapter) {
qunit('replication: ' + adapter, {
setup : function () {
this.name = generateAdapterUrl(adapter);
}
});
asyncTest("replicate from down server test", function (){
expect(1);
initTestDB(this.name, function(err, db) {
db.replicate.to('http://infiniterequest.com', function (err, changes) {
ok(err);
start();
});
});
});
});