-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathdoc.js
More file actions
478 lines (437 loc) · 16.9 KB
/
doc.js
File metadata and controls
478 lines (437 loc) · 16.9 KB
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
var Backend = require('../../lib/backend');
var expect = require('chai').expect;
var async = require('async');
var errorHandler = require('../util').errorHandler;
describe('Doc', function() {
beforeEach(function() {
this.backend = new Backend();
this.connection = this.backend.connect();
});
it('getting twice returns the same doc', function() {
var doc = this.connection.get('dogs', 'fido');
var doc2 = this.connection.get('dogs', 'fido');
expect(doc).equal(doc2);
});
it('calling doc.destroy unregisters it', function(done) {
var connection = this.connection;
var doc = connection.get('dogs', 'fido');
expect(connection.getExisting('dogs', 'fido')).equal(doc);
doc.destroy(function(err) {
if (err) return done(err);
expect(connection.getExisting('dogs', 'fido')).equal(undefined);
var doc2 = connection.get('dogs', 'fido');
expect(doc).not.equal(doc2);
done();
});
// destroy is async
expect(connection.getExisting('dogs', 'fido')).equal(doc);
});
it('getting then destroying then getting returns a new doc object', function(done) {
var connection = this.connection;
var doc = connection.get('dogs', 'fido');
doc.destroy(function(err) {
if (err) return done(err);
var doc2 = connection.get('dogs', 'fido');
expect(doc).not.equal(doc2);
expect(doc).eql(doc2);
done();
});
});
it('doc.destroy() works without a callback', function() {
var doc = this.connection.get('dogs', 'fido');
doc.destroy();
});
it('errors when trying to set a very large seq', function(done) {
var connection = this.connection;
connection.seq = Number.MAX_SAFE_INTEGER;
var doc = connection.get('dogs', 'fido');
doc.create({name: 'fido'});
doc.once('error', function(error) {
expect(error.code).to.equal('ERR_CONNECTION_SEQ_INTEGER_OVERFLOW');
done();
});
});
describe('when connection closed', function() {
beforeEach(function(done) {
this.op1 = [{p: ['snacks'], oi: true}];
this.op2 = [{p: ['color'], oi: 'gray'}];
this.doc = this.connection.get('dogs', 'fido');
this.doc.create({}, function(err) {
if (err) return done(err);
done();
});
});
it('do not mutate previously inflight op', function(done) {
var doc = this.doc;
var op1 = this.op1;
var op2 = this.op2;
var connection = this.connection;
this.connection.on('send', function() {
expect(doc.pendingOps).to.have.length(0);
expect(doc.inflightOp.op).to.eql(op1);
expect(doc.inflightOp.sentAt).to.not.be.undefined;
connection.close();
expect(doc.pendingOps).to.have.length(1);
doc.submitOp(op2);
expect(doc.pendingOps).to.have.length(2);
expect(doc.pendingOps[0].op).to.eql(op1);
expect(doc.pendingOps[1].op).to.eql(op2);
done();
});
this.doc.submitOp(this.op1, function() {
done(new Error('Connection should have been closed'));
});
});
});
describe('applyStack', function() {
beforeEach(function(done) {
this.doc = this.connection.get('dogs', 'fido');
this.doc2 = this.backend.connect().get('dogs', 'fido');
this.doc3 = this.backend.connect().get('dogs', 'fido');
var doc2 = this.doc2;
this.doc.create({}, function(err) {
if (err) return done(err);
doc2.fetch(done);
});
});
function verifyConsistency(doc, doc2, doc3, handlers, callback) {
doc.whenNothingPending(function(err) {
if (err) return callback(err);
expect(handlers.length).equal(0);
doc2.fetch(function(err) {
if (err) return callback(err);
doc3.fetch(function(err) {
if (err) return callback(err);
expect(doc.data).eql(doc2.data);
expect(doc.data).eql(doc3.data);
callback();
});
});
});
}
it('single component ops emit an `op` event', function(done) {
var doc = this.doc;
var doc2 = this.doc2;
var doc3 = this.doc3;
var handlers = [
function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'white'}]);
expect(doc.data).eql({color: 'white'});
doc.submitOp({p: ['color'], oi: 'gray'});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'gray'}]);
expect(doc.data).eql({color: 'gray'});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([]);
expect(doc.data).eql({color: 'gray'});
doc.submitOp({p: ['color'], oi: 'black'});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'black'}]);
expect(doc.data).eql({color: 'black'});
}
];
doc.on('opComponent', function(op, source) {
var handler = handlers.shift();
handler(op, source);
});
doc2.submitOp([{p: ['color'], oi: 'brown'}], function(err) {
if (err) return done(err);
doc.submitOp({p: ['color'], oi: 'white'});
expect(doc.data).eql({color: 'gray'});
verifyConsistency(doc, doc2, doc3, handlers, done);
});
});
it('remote multi component ops emit individual `op` events', function(done) {
var doc = this.doc;
var doc2 = this.doc2;
var doc3 = this.doc3;
var handlers = [
function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'white'}]);
expect(doc.data).eql({color: 'white'});
doc.submitOp([{p: ['color'], oi: 'gray'}, {p: ['weight'], oi: 40}]);
expect(doc.data).eql({color: 'gray', weight: 40});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'gray'}, {p: ['weight'], oi: 40}]);
expect(doc.data).eql({color: 'gray', weight: 40});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['age'], oi: 2}]);
expect(doc.data).eql({color: 'gray', weight: 40, age: 2});
doc.submitOp([{p: ['color'], oi: 'black'}, {p: ['age'], na: 1}]);
expect(doc.data).eql({color: 'black', weight: 40, age: 5});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['color'], oi: 'black'}, {p: ['age'], na: 1}]);
expect(doc.data).eql({color: 'black', weight: 40, age: 3});
doc.submitOp({p: ['age'], na: 2});
expect(doc.data).eql({color: 'black', weight: 40, age: 5});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['age'], na: 2}]);
expect(doc.data).eql({color: 'black', weight: 40, age: 5});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['owner'], oi: 'sue'}]);
expect(doc.data).eql({color: 'black', weight: 40, age: 5, owner: 'sue'});
}
];
doc.on('opComponent', function(op, source) {
var handler = handlers.shift();
handler(op, source);
});
doc2.submitOp([{p: ['age'], oi: 2}, {p: ['owner'], oi: 'sue'}], function(err) {
if (err) return done(err);
doc.submitOp({p: ['color'], oi: 'white'});
expect(doc.data).eql({color: 'gray', weight: 40});
verifyConsistency(doc, doc2, doc3, handlers, done);
});
});
it('remote multi component ops are transformed by ops submitted in `op` event handlers', function(done) {
var doc = this.doc;
var doc2 = this.doc2;
var doc3 = this.doc3;
var handlers = [
function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['tricks'], oi: ['fetching']}]);
expect(doc.data).eql({tricks: ['fetching']});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['tricks', 0], li: 'stand'}]);
expect(doc.data).eql({tricks: ['stand', 'fetching']});
doc.submitOp([{p: ['tricks', 0], ld: 'stand'}, {p: ['tricks', 0, 8], si: ' stick'}]);
expect(doc.data).eql({tricks: ['fetching stick']});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['tricks', 0], ld: 'stand'}, {p: ['tricks', 0, 8], si: ' stick'}]);
expect(doc.data).eql({tricks: ['fetching stick']});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['tricks', 0], li: 'shake'}]);
expect(doc.data).eql({tricks: ['shake', 'fetching stick']});
doc.submitOp([{p: ['tricks', 1, 0], sd: 'fetch'}, {p: ['tricks', 1, 0], si: 'tug'}]);
expect(doc.data).eql({tricks: ['shake', 'tuging stick']});
}, function(op, source) {
expect(source).equal(true);
expect(op).eql([{p: ['tricks', 1, 0], sd: 'fetch'}, {p: ['tricks', 1, 0], si: 'tug'}]);
expect(doc.data).eql({tricks: ['shake', 'tuging stick']});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([{p: ['tricks', 1, 3], sd: 'ing'}]);
expect(doc.data).eql({tricks: ['shake', 'tug stick']});
}, function(op, source) {
expect(source).equal(false);
expect(op).eql([]);
expect(doc.data).eql({tricks: ['shake', 'tug stick']});
}
];
doc.on('opComponent', function(op, source) {
var handler = handlers.shift();
handler(op, source);
});
var remoteOp = [
{p: ['tricks'], oi: ['fetching']},
{p: ['tricks', 0], li: 'stand'},
{p: ['tricks', 1], li: 'shake'},
{p: ['tricks', 2, 5], sd: 'ing'},
{p: ['tricks', 0], lm: 2}
];
doc2.submitOp(remoteOp, function(err) {
if (err) return done(err);
doc.fetch();
verifyConsistency(doc, doc2, doc3, handlers, done);
});
});
it('emits batch op events for a multi-component local op', function(done) {
var doc = this.doc;
var beforeOpBatchCount = 0;
var submittedOp = [
{p: ['tricks'], oi: ['fetching']},
{p: ['tricks', 0], li: 'stand'}
];
doc.on('beforeOp', function(op, source) {
expect(op).to.eql(submittedOp);
expect(source).to.be.true;
beforeOpBatchCount++;
});
doc.on('op', function(op, source) {
expect(op).to.eql(submittedOp);
expect(source).to.be.true;
expect(beforeOpBatchCount).to.equal(1);
expect(doc.data).to.eql({tricks: ['stand', 'fetching']});
done();
});
doc.submitOp(submittedOp, errorHandler(done));
});
it('emits batch op events for a multi-component remote op', function(done) {
var doc = this.doc;
var doc2 = this.doc2;
var beforeOpBatchCount = 0;
var submittedOp = [
{p: ['tricks'], oi: ['fetching']},
{p: ['tricks', 0], li: 'stand'}
];
doc.on('beforeOp', function(op, source) {
expect(op).to.eql(submittedOp);
expect(source).to.be.false;
beforeOpBatchCount++;
});
doc.on('op', function(op, source) {
expect(op).to.eql(submittedOp);
expect(source).to.be.false;
expect(beforeOpBatchCount).to.equal(1);
expect(doc.data).to.eql({tricks: ['stand', 'fetching']});
done();
});
async.series([
doc.subscribe.bind(doc),
doc2.submitOp.bind(doc2, submittedOp)
], errorHandler(done));
});
});
describe('submitting ops in callbacks', function() {
beforeEach(function() {
this.doc = this.connection.get('dogs', 'scooby');
});
it('succeeds with valid op', function(done) {
var doc = this.doc;
doc.create({name: 'Scooby Doo'}, function(error) {
expect(error).to.not.exist;
// Build valid op that deletes a substring at index 0 of name.
var textOpComponents = [{p: 0, d: 'Scooby '}];
var op = [{p: ['name'], t: 'text0', o: textOpComponents}];
doc.submitOp(op, function(error) {
if (error) return done(error);
expect(doc.data).eql({name: 'Doo'});
done();
});
});
});
it('fails with invalid op', function(done) {
var doc = this.doc;
doc.create({name: 'Scooby Doo'}, function(error) {
expect(error).to.not.exist;
// Build op that tries to delete an invalid substring at index 0 of name.
var textOpComponents = [{p: 0, d: 'invalid'}];
var op = [{p: ['name'], t: 'text0', o: textOpComponents}];
doc.submitOp(op, function(error) {
expect(error).instanceOf(Error);
done();
});
});
});
});
describe('submitting an invalid op', function() {
var doc;
var invalidOp;
var validOp;
beforeEach(function(done) {
// This op is invalid because we try to perform a list deletion
// on something that isn't a list
invalidOp = {p: ['name'], ld: 'Scooby'};
validOp = {p: ['snacks'], oi: true};
doc = this.connection.get('dogs', 'scooby');
doc.create({name: 'Scooby'}, function(error) {
if (error) return done(error);
doc.whenNothingPending(done);
});
});
it('returns an error to the submitOp callback', function(done) {
doc.submitOp(invalidOp, function(error) {
expect(error.message).to.equal('Referenced element not a list');
done();
});
});
it('rolls the doc back to a usable state', function(done) {
async.series([
function(next) {
doc.submitOp(invalidOp, function(error) {
expect(error).to.be.instanceOf(Error);
next();
});
},
doc.whenNothingPending.bind(doc),
function(next) {
expect(doc.data).to.eql({name: 'Scooby'});
next();
},
doc.submitOp.bind(doc, validOp),
function(next) {
expect(doc.data).to.eql({name: 'Scooby', snacks: true});
next();
}
], done);
});
it('rescues an irreversible op collision', function(done) {
// This test case attempts to reconstruct the following corner case, with
// two independent references to the same document. We submit two simultaneous, but
// incompatible operations (eg one of them changes the data structure the other op is
// attempting to manipulate).
//
// The second document to attempt to submit should have its op rejected, and its
// state successfully rolled back to a usable state.
var doc1 = this.backend.connect().get('dogs', 'snoopy');
var doc2 = this.backend.connect().get('dogs', 'snoopy');
var pauseSubmit = false;
var fireSubmit;
this.backend.use('submit', function(request, callback) {
if (pauseSubmit) {
fireSubmit = function() {
pauseSubmit = false;
callback();
};
} else {
fireSubmit = null;
callback();
}
});
async.series([
doc1.create.bind(doc1, {colours: ['white']}),
doc1.whenNothingPending.bind(doc1),
doc2.fetch.bind(doc2),
doc2.whenNothingPending.bind(doc2),
// Both documents start off at the same v1 state, with colours as a list
function(next) {
expect(doc1.data).to.eql({colours: ['white']});
expect(doc2.data).to.eql({colours: ['white']});
next();
},
doc1.submitOp.bind(doc1, {p: ['colours'], oi: 'white,black'}),
// This next step is a little fiddly. We abuse the middleware to pause the op submission and
// ensure that we get this repeatable sequence of events:
// 1. doc2 is still on v1, where 'colours' is a list (but it's a string in v2)
// 2. doc2 submits an op that assumes 'colours' is still a list
// 3. doc2 fetches v2 before the op submission completes - 'colours' is no longer a list locally
// 4. doc2's op is rejected by the server, because 'colours' is not a list on the server
// 5. doc2 attempts to roll back the inflight op by turning a list insertion into a list deletion
// 6. doc2 applies this list deletion to a field that is no longer a list
// 7. type.apply throws, because this is an invalid op
function(next) {
pauseSubmit = true;
doc2.submitOp({p: ['colours', '0'], li: 'black'}, function(error) {
expect(error.message).to.equal('Referenced element not a list');
next();
});
doc2.fetch(function(error) {
if (error) return next(error);
fireSubmit();
});
},
// Validate that - despite the error in doc2.submitOp - doc2 has been returned to a
// workable state in v2
function(next) {
expect(doc1.data).to.eql({colours: 'white,black'});
expect(doc2.data).to.eql(doc1.data);
doc2.submitOp({p: ['colours'], oi: 'white,black,red'}, next);
}
], done);
});
});
});