-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration.js
458 lines (377 loc) · 15 KB
/
integration.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
// var jsdom = require('jsdom');
var should = require('should');
var fs = require('fs');
var http = require('http');
var util = require('util');
var State = require('./extensions/State');
var Index = require('../shared/Index');
var Table = require('../shared/Table');
var ServerState = require('../server/State');
var CloudType = require('../shared/CloudType');
var CInt = require('./extensions/CInt');
var CSet = require('./extensions/CInt');
var CloudTypeServer = require('../server/main.js');
var stubs = require('./stubs');
var CloudTypes = require('../client/main.js');
var CloudTypeClient = require('../client/CloudTypeClient.js');
var host = 'http://localhost';
var port = 8090;
var clientOptions = {
transports: ['websocket'],
'force new connection': true
};
function createClient(callback) {
var client = CloudTypes.createClient();
client.connect(host + ':' + port, clientOptions, function (state) {
callback(client, state);
});
}
function applyChanges(state) {
var changed = State.fromJSON(stubs.stateChanged);
state.replaceBy(changed);
}
function waitForTransport(callback) {
setTimeout(callback, 200);
}
describe('Integration #', function () {
var server, serverState;
beforeEach(function () {
serverState = ServerState.fromJSON(stubs.stateUnchanged);
server = CloudTypeServer.createServer(serverState);
server.publish(8090);
});
afterEach(function () {
server.close();
});
describe('Client API', function () {
describe('.createClient()', function () {
var client = CloudTypes.createClient();
it('should create a CloudTypeClient object', function () {
should.exist(client);
client.should.be.an.instanceOf(CloudTypeClient);
});
});
describe('CloudTypeClient', function () {
describe('.connect(host:port, callback)', function () {
var client = CloudTypes.createClient();
it('should call callback with state when connected to server', function (done) {
client.connect(host + ':' + port, function (state) {
should.exist(state);
client.close();
done();
});
});
});
describe('.connect(host:port, options, callback)', function () {
var client = CloudTypes.createClient();
it('should call callback when connected to server', function (done) {
client.connect(host + ':' + port, clientOptions, function () {
client.close();
done();
});
});
});
});
});
describe('when connected to server', function () {
it('should have same state as server', function (done) {
createClient(function (client, state) {
state.isEqual(server.state);
client.close();
done();
});
});
it('should have forked state of server', function (done) {
createClient(function (client, state) {
state.isForkOf(server.state);
client.close();
done();
});
});
});
describe('synchronising changes with yield (stubs.unchangedState -> stubs.changedState) (scenario 1)', function () {
it('should give eventual consistency with yield (this test could fail if too slow!)', function (done) {
createClient(function (client1, state1) {
// client1 state: unchanged -> changed
applyChanges(state1);
createClient(function (client2, state2) {
(function () { state1.isConsistent(server.state); }).should.throwError();
state2.isConsistent(server.state);
// yield1 for client1: sends revision client1 -> server
state1.yield();
(function () { state1.isConsistent(server.state); }).should.throwError();
// server synced with client1 after some ms
waitForTransport(function () {
server.state.isConsistent(state1);
(function () { state2.isConsistent(server.state); }).should.throwError();
// yield1 for client2: sends changes from client2 -> server
state2.yield();
// yield2 for client2: does nothing, because it has not received revision of server yet
state2.yield();
(function () { state2.isConsistent(server.state); }).should.throwError();
waitForTransport(function () {
(function () { state2.isConsistent(server.state); }).should.throwError();
// yield3 for client2: should have received revision of server by now
// and thus merges server state with client state. Since no changes are
// made to client2 in the meantime, states should be consistent.
state2.yield(); // (this case of yield is synchronous)
// at this point all states are consistent again.
state2.isConsistent(server.state);
state1.isConsistent(server.state);
client1.close();
client2.close();
done();
});
});
});
});
});
});
describe('synchronising changes with yield (stubs.unchangedState -> stubs.changedState) (scenario 2)', function () {
it('should give eventual consistency with yield (this test could fail if too slow!)', function (done) {
createClient(function (client1, state1) {
// replace the state by a state with the applied
applyChanges(state1);
createClient(function (client2, state2) {
(function () { state1.isConsistent(server.state); }).should.throwError();
state2.isConsistent(server.state);
// yield1 for client1: sends revision client1 -> server
state1.yield();
(function () { state1.isConsistent(server.state); }).should.throwError();
// server synced with client1 after some ms
setTimeout(function () {
state1.isConsistent(server.state);
(function () { state2.isConsistent(server.state); }).should.throwError();
// yield1 for client2: sends changes from client2 -> server
state2.yield();
// yield2 for client2: does nothing, because it has not received revision of server yet
state2.yield();
(function () { state2.isConsistent(server.state); }).should.throwError();
setTimeout(function () {
(function () { state2.isConsistent(server.state); }).should.throwError();
// yield3 for client2: should have received revision of server by now
// and thus merges server state with client state. Since no changes are
// made to client2 in the meantime, states should be consistent.
state2.yield(); // (this case of yield is synchronous)
// at this point all states are consistent again.
state2.isConsistent(server.state);
state2.isConsistent(server.state);
client1.close();
client2.close();
done();
}, 500);
}, 500);
});
});
});
});
describe('.flush(callback)', function () {
describe('syncing 1 client with server', function () {
it('should call callback when synced with server', function (done) {
createClient(function (client1, state1) {
applyChanges(state1);
state1.flush(function (error) {
should.not.exist(error);
state1.isConsistent(server.state);
client1.close();
done();
});
});
});
});
describe('syncing 2 clients through server', function () {
it('should call callback when synced with server', function (done) {
createClient(function (client1, state1) {
createClient(function (client2, state2) {
applyChanges(state1);
state1.flush(function (error) {
should.not.exist(error);
state2.flush(function (error) {
should.not.exist(error);
state1.isConsistent(server.state);
state2.isConsistent(server.state);
client1.close();
client2.close();
done();
});
});
});
});
});
});
});
describe('complete client/server usage scenario', function () {
it('should be eventually consistent', function (done) {
var array, client1, client2, carray, entry;
server.close();
// server code
server = CloudTypeServer.createServer();
array = new Index([{name: "string"}, {buyer: "string"}], {toBuy: "CInt", fromShop: "CString"});
server.declare("Product", array);
server.publish(8001);
// client setup code
client1 = CloudTypes.createClient();
client1.connect(host + ':' + 8001, clientOptions, function (state1) {
// client use data
carray = state1.get("Product");
should.exist(carray);
entry = carray.get("Napkins", "Mom");
should.exist(entry);
should.exist(entry.get('toBuy'));
entry.get('toBuy').set(10);
entry.get('fromShop').setIfEmpty("Carrefour");
// sync with server
state1.yield();
setTimeout(function () {
server.state.isConsistent(state1);
client1.close();
done();
}, 200)
});
});
});
describe('Table client/server usage scenario', function () {
it('should be eventually consistent', function (done) {
var array, client1, client2, carray, entry;
server.close();
// server code
server = CloudTypeServer.createServer();
server.declare("Customer", new Table({name: "CString"}));
// server.declare("Order", new Table([{customer: "Customer"}], {price: "CInt"}));
server.declare("Order", new Table({price: "CInt"}));
server.publish(8001);
// client setup code
client1 = CloudTypes.createClient();
client1.connect(host + ':' + 8001, clientOptions, function (state1) {
// client use data
Customer = state1.get("Customer");
Order = state1.get("Order");
should.exist(Customer);
should.exist(Order);
var customer = Customer.create();
should.exist(customer);
customer.get('name').set("Jerry");
// sync with server
state1.yield();
setTimeout(function () {
server.state.isConsistent(state1);
server.state.get("Customer").all().length.should.equal(1);
server.state.get("Customer").all()[0].get('name').get().should.equal("Jerry");
customer.delete();
(function () { server.state.isConsistent(state1); }).should.throwError();
state1.yield();
state1.yield();
setTimeout(function () {
server.state.isConsistent(state1);
done();
}, 200);
}, 200);
});
});
});
describe('Global CInt client/server semantic scenario:', function () {
it('should give eventual consistency for all states (this test could fail if too slow!)', function (done) {
var counter;
server.close();
// server code
server = CloudTypeServer.createServer();
server.declare('counter', CInt);
server.publish(port);
counter = server.state.get('counter');
// clients server interplay
createClient(function (client1, state1) {
var counter1 = state1.get('counter');
should.exist(counter1);
counter1.set(100);
counter1.get().should.equal(100);
createClient(function (client2, state2) {
var counter2 = state2.get('counter');
counter2.get().should.equal(0);
counter2.add(50);
// yield 1 for client1: sends revision client1 -> server
state1.yield();
// server synced with client1 after some ms
waitForTransport(function () {
counter.get().should.equal(100);
// yield 1 for client2: sends changes from client2 -> server
state2.yield();
waitForTransport(function () {
counter.get().should.equal(150);
// yield 2 for client 1: merges revision of server
// note: this revision was sent before client2 sent its revision, therefore
// another 2 yields will be necessary for complete synchronization.
state1.yield();
counter1.get().should.equal(100);
// yield2 for client2: merge revision of server into client2.
state2.yield();
counter2.get().should.equal(150);
state1.yield();
waitForTransport(function () {
state1.yield();
counter1.get().should.equal(150);
// at this point all states are consistent again.
state2.isConsistent(server.state);
state2.isConsistent(server.state);
client1.close();
client2.close();
done();
});
});
});
});
});
});
});
describe('Global CInt client/server semantic scenario:', function () {
it('should give eventual consistency for all states (this test could fail if too slow!)', function (done) {
var counter;
server.close();
// server code
server = CloudTypeServer.createServer();
server.declare('counter', CInt);
server.publish(port);
counter = server.state.get('counter');
// clients/server interplay
createClient(function (client1, state1) {
var counter1 = state1.get('counter');
counter1.set(100);
counter1.get().should.equal(100);
createClient(function (client2, state2) {
var counter2 = state2.get('counter');
counter2.get().should.equal(0);
counter2.add(50);
// yield1 for client2: sends revision client2 -> server
state2.yield();
// server synced with client2 after some ms
waitForTransport(function () {
counter.get().should.equal(50);
// yield1 for client1: sends changes from client1 -> server
state1.yield();
// yield2 for client2: merges revision of server with client2
state2.yield();
// still 50, because revision was sent before merge with client1.
counter2.get().should.equal(50);
waitForTransport(function () {
// the set of client1 has overwritten the add of client2.
counter.get().should.equal(100);
// nothing changes for client1 when merged
state1.yield();
counter1.get().should.equal(100);
state2.yield();
waitForTransport(function () {
state2.yield();
counter2.get().should.equal(100);
// at this point all states are consistent again.
state2.isConsistent(server.state);
state1.isConsistent(server.state);
client1.close();
client2.close();
done();
});
});
});
});
});
});
});
});