forked from pouchdb/pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.issue221.js
114 lines (105 loc) · 3.69 KB
/
test.issue221.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
/*globals initTestDB: false, emit: true, generateAdapterUrl: false */
/*globals PERSIST_DATABASES: false, initDBPair: false, utils: true */
/*globals ajax: true, LevelPouch: true */
"use strict";
var adapters = [
['local-1', 'http-1']
];
var qunit = module;
if (typeof module !== undefined && module.exports) {
Pouch = require('../src/pouch.js');
LevelPouch = require('../src/adapters/pouch.leveldb.js');
utils = require('./test.utils.js');
ajax = Pouch.utils.ajax;
for (var k in utils) {
global[k] = global[k] || utils[k];
}
qunit = QUnit.module;
}
adapters.map(function(adapters) {
qunit('replication + compaction', {
setup: function() {
this.local = generateAdapterUrl(adapters[0]);
this.remote = generateAdapterUrl(adapters[1]);
},
teardown: function() {
if (!PERSIST_DATABASES) {
Pouch.destroy(this.local);
Pouch.destroy(this.remote);
}
}
});
var doc = { _id: '0', integer: 0 };
asyncTest('Testing issue #221', function() {
var self = this;
// Create databases.
initDBPair(this.local, this.remote, function(local, remote) {
// Write a doc in CouchDB.
remote.put(doc, {}, function(err, results) {
// Update the doc.
doc._rev = results.rev;
doc.integer = 1;
remote.put(doc, {}, function(err, results) {
// Compact the db.
remote.compact(function(data, status, jqXHR) {
// Wait until compaction has affected the doc.
var interval;
var checkDoc = function() {
remote.get(doc._id,{revs_info:true},function(err, data) {
var correctRev = data._revs_info[0];
if (data._revs_info[1].status === 'missing') {
// We already got a successful compaction, but did a whole
// new request before we figured it out, yay races
if (!interval) {
return;
}
clearInterval(interval);
interval = null;
// Replicate to PouchDB.
local.replicate.from(remote, function(err, results) {
// Check the PouchDB doc.
local.get(doc._id, function(err, results) {
ok(results._rev === correctRev.rev,
'correct rev stored after replication');
ok(results.integer === 1,
'correct content stored after replication');
start();
});
});
}
});
};
interval = setInterval(checkDoc, 100);
});
});
});
});
});
asyncTest('Testing issue #221 again', function() {
var self = this;
// Create databases.
initDBPair(this.local, this.remote, function(local, remote) {
// Write a doc in CouchDB.
remote.put(doc, {}, function(err, results) {
doc._rev = results.rev;
// Second doc so we get 2 revisions from replicate.
remote.put(doc, {}, function(err, results) {
doc._rev = results.rev;
local.replicate.from(remote, function(err, results) {
doc.integer = 1;
// One more change
remote.put(doc, {}, function(err, results) {
// Testing if second replications fails now
local.replicate.from(remote, function(err, results) {
local.get(doc._id, function(err, results) {
ok(results.integer === 1, 'correct content stored after replication');
start();
});
});
});
});
});
});
});
});
});