-
Notifications
You must be signed in to change notification settings - Fork 84
/
yorkie_test.ts
584 lines (507 loc) · 17.2 KB
/
yorkie_test.ts
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
/*
* Copyright 2020 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from 'chai';
import { EventEmitter } from 'events';
import * as sinon from 'sinon';
import {
Client,
ClientEvent,
ClientEventType,
DocumentSyncResultType,
} from '../src/core/client';
import { Document, DocEvent, DocEventType } from '../src/document/document';
import yorkie from '../src/yorkie';
const __karma__ = (global as any).__karma__;
const testRPCAddr = __karma__.config.testRPCAddr || 'https://yorkie.dev/api';
const testCollection = 'test-col';
async function withTwoClientsAndDocuments(
callback: (
c1: Client,
d1: Document,
c2: Client,
d2: Document,
) => Promise<void>,
title: string,
): Promise<void> {
const client1 = yorkie.createClient(testRPCAddr);
const client2 = yorkie.createClient(testRPCAddr);
await client1.activate();
await client2.activate();
const docKey = `${title}-${new Date().getTime()}`;
const doc1 = yorkie.createDocument(testCollection, docKey);
const doc2 = yorkie.createDocument(testCollection, docKey);
await client1.attach(doc1, true);
await client2.attach(doc2, true);
await callback(client1, doc1, client2, doc2);
await client1.detach(doc1);
await client2.detach(doc2);
await client1.deactivate();
await client2.deactivate();
}
function waitFor(eventName: string, listener: EventEmitter): Promise<void> {
return new Promise((resolve) => listener.on(eventName, resolve));
}
function createSpy(emitter: EventEmitter) {
return (event: ClientEvent | DocEvent) => emitter.emit(event.name);
}
// NOTE: In particular, we uses general functions, not arrow functions
// to access test title in test codes.
describe('Yorkie', function () {
it('Can be activated, deactivated', async function () {
const docKey = `${this.test.title}-${new Date().getTime()}`;
const clientWithKey = yorkie.createClient(testRPCAddr, {
key: docKey,
syncLoopDuration: 50,
reconnectStreamDelay: 1000,
});
assert.isFalse(clientWithKey.isActive());
await clientWithKey.activate();
assert.isTrue(clientWithKey.isActive());
assert.equal(docKey, clientWithKey.getKey());
await clientWithKey.deactivate();
assert.isFalse(clientWithKey.isActive());
const clientWithoutKey = yorkie.createClient(testRPCAddr);
assert.isFalse(clientWithoutKey.isActive());
await clientWithoutKey.activate();
assert.isTrue(clientWithoutKey.isActive());
assert.isString(clientWithoutKey.getKey());
assert.lengthOf(clientWithoutKey.getKey(), 36);
await clientWithoutKey.deactivate();
assert.isFalse(clientWithoutKey.isActive());
});
it('Can attach/detach documents', async function () {
const docKey = `${this.test.title}-${new Date().getTime()}`;
const doc1 = yorkie.createDocument(testCollection, docKey);
const doc2 = yorkie.createDocument(testCollection, docKey);
const client1 = yorkie.createClient(testRPCAddr);
const client2 = yorkie.createClient(testRPCAddr);
await client1.activate();
await client2.activate();
await client1.attach(doc1, true);
doc1.update((root) => {
root['k1'] = { 'k1-1': 'v1' };
root['k2'] = ['1', '2'];
}, 'set v1, v2');
await client1.sync();
assert.equal('{"k1":{"k1-1":"v1"},"k2":["1","2"]}', doc1.toSortedJSON());
await client2.attach(doc2, true);
assert.equal('{"k1":{"k1-1":"v1"},"k2":["1","2"]}', doc2.toSortedJSON());
await client1.detach(doc1);
await client2.detach(doc2);
await client1.deactivate();
await client2.deactivate();
});
it('Can handle sync', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
const spy = sinon.spy();
const unsub = d2.subscribe(spy);
assert.equal(0, spy.callCount);
d1.update((root) => {
root['k1'] = 'v1';
});
await c1.sync();
await c2.sync();
assert.equal(1, spy.callCount);
d1.update((root) => {
root['k2'] = 'v2';
});
await c1.sync();
await c2.sync();
assert.equal(2, spy.callCount);
unsub();
d1.update((root) => {
root['k3'] = 'v3';
});
await c1.sync();
await c2.sync();
assert.equal(2, spy.callCount);
}, this.test.title);
});
it('Can watch documents', async function () {
const c1 = yorkie.createClient(testRPCAddr);
const c2 = yorkie.createClient(testRPCAddr);
await c1.activate();
await c2.activate();
const docKey = `${this.test.title}-${new Date().getTime()}`;
const d1 = yorkie.createDocument(testCollection, docKey);
const d2 = yorkie.createDocument(testCollection, docKey);
await c1.attach(d1);
await c2.attach(d2);
const listener1 = new EventEmitter();
const listener2 = new EventEmitter();
const spy1 = createSpy(listener1);
const spy2 = createSpy(listener2);
const unsub1 = d1.subscribe(spy1);
const unsub2 = d2.subscribe(spy2);
d2.update((root) => {
root['k1'] = 'v1';
});
await waitFor(DocEventType.LocalChange, listener2);
await waitFor(DocEventType.RemoteChange, listener1);
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
unsub1();
unsub2();
await c1.detach(d1);
await c2.detach(d2);
await c1.deactivate();
await c2.deactivate();
});
it('Can handle primitive types', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root['k1'] = true;
root['k2'] = 2147483647;
root['k3'] = yorkie.Long.fromString('9223372036854775807');
root['k4'] = 1.79;
root['k5'] = '4';
root['k6'] = new Uint8Array([65, 66]);
root['k7'] = new Date();
});
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can handle concurrent set/delete operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root['k1'] = 'v1';
});
d2.update((root) => {
root['k1'] = 'v2';
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k2'] = {};
});
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k2'] = 'v2';
});
d2.update((root) => {
root['k2']['k2.1'] = { 'k2.1.1': 'v3' };
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k3'] = 'v4';
});
d2.update((root) => {
root['k4'] = 'v5';
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
delete root['k3'];
});
d2.update((root) => {
root['k3'] = 'v6';
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can handle concurrent add operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root['k1'] = ['1'];
});
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k1'].push('2');
});
d2.update((root) => {
root['k1'].push('3');
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can handle concurrent insertAfter operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
let prev;
d1.update((root) => {
root['k1'] = [1, 2, 3, 4];
prev = root['k1'].getElementByIndex(1);
});
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k1'].deleteByID(prev.getID());
assert.equal('{"k1":[1,3,4]}', root.toJSON());
});
d2.update((root) => {
root['k1'].insertAfter(prev.getID(), 2);
assert.equal('{"k1":[1,2,2,3,4]}', root.toJSON());
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
assert.equal('{"k1":[1,2,3,4]}', d1.toJSON());
d1.update((root) => {
const prev = root['k1'].getElementByIndex(1);
root['k1'].insertAfter(prev.getID(), '2.1');
assert.equal('{"k1":[1,2,"2.1",3,4]}', root.toJSON());
});
d2.update((root) => {
const prev = root['k1'].getElementByIndex(1);
root['k1'].insertAfter(prev.getID(), '2.2');
assert.equal('{"k1":[1,2,"2.2",3,4]}', root.toJSON());
});
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can handle concurrent moveBefore operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root['k1'] = [0, 1, 2];
assert.equal('{"k1":[0,1,2]}', root.toJSON());
});
await c1.sync();
await c2.sync();
assert.equal(d1.toJSON(), d2.toJSON());
d1.update((root) => {
const next = root['k1'].getElementByIndex(0);
const item = root['k1'].getElementByIndex(2);
root['k1'].moveBefore(next.getID(), item.getID());
assert.equal('{"k1":[2,0,1]}', root.toJSON());
});
d2.update((root) => {
const next = root['k1'].getElementByIndex(1);
const item = root['k1'].getElementByIndex(2);
root['k1'].moveBefore(next.getID(), item.getID());
assert.equal('{"k1":[0,2,1]}', root.toJSON());
});
await c1.sync();
await c2.sync();
await c1.sync();
}, this.test.title);
});
it('should handle edit operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root.createText('k1');
root['k1'].edit(0, 0, 'ABCD');
}, 'set new text by c1');
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), `{"k1":"ABCD"}`);
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root.createText('k1');
root['k1'].edit(0, 0, '1234');
}, 'edit 0,0 1234 by c1');
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), `{"k1":"1234"}`);
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('should handle concurrent edit operations', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
d1.update((root) => {
root.createText('k1');
}, 'set new text by c1');
await c1.sync();
await c2.sync();
assert.equal(d1.toSortedJSON(), `{"k1":""}`);
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k1'].edit(0, 0, 'ABCD');
}, 'edit 0,0 ABCD by c1');
assert.equal(d1.toSortedJSON(), `{"k1":"ABCD"}`);
d2.update((root) => {
root['k1'].edit(0, 0, '1234');
}, 'edit 0,0 1234 by c2');
assert.equal(d2.toSortedJSON(), `{"k1":"1234"}`);
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k1'].edit(2, 3, 'XX');
}, 'edit 2,3 XX by c1');
d2.update((root) => {
root['k1'].edit(2, 3, 'YY');
}, 'edit 2,3 YY by c1');
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
d1.update((root) => {
root['k1'].edit(4, 5, 'ZZ');
}, 'edit 4,5 ZZ by c1');
d2.update((root) => {
root['k1'].edit(2, 3, 'TT');
}, 'edit 2,3 TT by c1');
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('should handle snapshot', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
// 01. Updates 700 changes over snapshot threshold.
for (let idx = 0; idx < 700; idx++) {
d1.update((root) => {
root[`${idx}`] = idx;
});
}
await c1.sync();
// 02. Makes local changes then pull a snapshot from the agent.
d2.update((root) => {
root['key'] = 'value';
});
await c2.sync();
assert.equal(d2.getRootObject()['key'], 'value');
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can recover from temporary disconnect (manual sync)', async function () {
await withTwoClientsAndDocuments(async (c1, d1, c2, d2) => {
// Normal Condition
d2.update((root) => {
root['k1'] = 'undefined';
});
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
// Simulate network error
const xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = (req) => {
req.respond(
400,
{
'Content-Type': 'application/grpc-web-text+proto',
},
null,
);
};
d2.update((root) => {
root['k1'] = 'v1';
});
await c2.sync().catch((err) => {
assert.equal(err.message, 'INVALID_STATE_ERR - 0');
});
await c1.sync().catch((err) => {
assert.equal(err.message, 'INVALID_STATE_ERR - 0');
});
assert.equal(d1.toSortedJSON(), '{"k1":"undefined"}');
assert.equal(d2.toSortedJSON(), '{"k1":"v1"}');
// Back to normal condition
xhr.restore();
await c2.sync();
await c1.sync();
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
}, this.test.title);
});
it('Can recover from temporary disconnect (realtime sync)', async function () {
const c1 = yorkie.createClient(testRPCAddr);
const c2 = yorkie.createClient(testRPCAddr);
await c1.activate();
await c2.activate();
const docKey = `${this.test.title}-${new Date().getTime()}`;
const d1 = yorkie.createDocument(testCollection, docKey);
const d2 = yorkie.createDocument(testCollection, docKey);
await c1.attach(d1);
await c2.attach(d2);
const listener1 = new EventEmitter();
const listener2 = new EventEmitter();
const customSpy = (emitter: EventEmitter) => {
return (event: ClientEvent | DocEvent) => {
if (event.name == ClientEventType.DocumentSyncResult) {
emitter.emit(event.value);
} else {
emitter.emit(event.name);
}
};
};
const spy1 = customSpy(listener1);
const spy2 = customSpy(listener2);
const unsub1 = {
client: c1.subscribe(spy1),
doc: d1.subscribe(spy1),
};
const unsub2 = {
client: c2.subscribe(spy2),
doc: d2.subscribe(spy2),
};
// Normal Condition
d2.update((root) => {
root['k1'] = 'undefined';
});
await waitFor(DocEventType.LocalChange, listener2); // d2 should be able to update
await waitFor(DocEventType.RemoteChange, listener1); // d1 should be able to receive d2's update
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
// Simulate network error
const xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = (req) => {
req.respond(
400,
{
'Content-Type': 'application/grpc-web-text+proto',
},
null,
);
};
d2.update((root) => {
root['k1'] = 'v1';
});
await waitFor(DocEventType.LocalChange, listener2); // d2 should be able to update
await waitFor(DocumentSyncResultType.SyncFailed, listener2); // c2 should fail to sync
c1.sync();
await waitFor(DocumentSyncResultType.SyncFailed, listener1); // c1 should also fail to sync
assert.equal(d1.toSortedJSON(), '{"k1":"undefined"}');
assert.equal(d2.toSortedJSON(), '{"k1":"v1"}');
// Back to normal condition
xhr.restore();
await waitFor(DocEventType.RemoteChange, listener1); // d1 should be able to receive d2's update
assert.equal(d1.toSortedJSON(), d2.toSortedJSON());
unsub1.client();
unsub2.client();
unsub1.doc();
unsub2.doc();
await c1.detach(d1);
await c2.detach(d2);
await c1.deactivate();
await c2.deactivate();
});
});