forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivedata_connection_tests.js
1880 lines (1559 loc) · 63.7 KB
/
livedata_connection_tests.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var newConnection = function (stream) {
// Some of these tests leave outstanding methods with no result yet
// returned. This should not block us from re-running tests when sources
// change.
return new LivedataTest.Connection(stream, {reloadWithOutstanding: true});
};
var makeConnectMessage = function (session) {
var msg = {
msg: 'connect',
version: LivedataTest.SUPPORTED_DDP_VERSIONS[0],
support: LivedataTest.SUPPORTED_DDP_VERSIONS
};
if (session)
msg.session = session;
return msg;
}
// Tests that stream got a message that matches expected.
// Expected is normally an object, and allows a wildcard value of '*',
// which will then match any value.
// Returns the message (parsed as a JSON object if expected is an object);
// which is particularly handy if you want to extract a value that was
// matched as a wildcard.
var testGotMessage = function (test, stream, expected) {
if (stream.sent.length === 0) {
test.fail({error: 'no message received', expected: expected});
return undefined;
}
var got = stream.sent.shift();
if (typeof got === 'string' && typeof expected === 'object')
got = JSON.parse(got);
// An expected value of '*' matches any value, and the matching value (or
// array of matching values, if there are multiple) is returned from this
// function.
if (typeof expected === 'object') {
var keysWithStarValues = [];
_.each(expected, function (v, k) {
if (v === '*')
keysWithStarValues.push(k);
});
_.each(keysWithStarValues, function (k) {
expected[k] = got[k];
});
}
test.equal(got, expected);
return got;
};
var startAndConnect = function(test, stream) {
stream.reset(); // initial connection start.
testGotMessage(test, stream, makeConnectMessage());
test.length(stream.sent, 0);
stream.receive({msg: 'connected', session: SESSION_ID});
test.length(stream.sent, 0);
};
var SESSION_ID = '17';
Tinytest.add("livedata stub - receive data", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
// data comes in for unknown collection.
var coll_name = Random.id();
stream.receive({msg: 'added', collection: coll_name, id: '1234',
fields: {a: 1}});
// break throught the black box and test internal state
test.length(conn._updatesForUnknownStores[coll_name], 1);
// XXX: Test that the old signature of passing manager directly instead of in
// options works.
var coll = new Mongo.Collection(coll_name, conn);
// queue has been emptied and doc is in db.
test.isUndefined(conn._updatesForUnknownStores[coll_name]);
test.equal(coll.find({}).fetch(), [{_id:'1234', a:1}]);
// second message. applied directly to the db.
stream.receive({msg: 'changed', collection: coll_name, id: '1234',
fields: {a:2}});
test.equal(coll.find({}).fetch(), [{_id:'1234', a:2}]);
test.isUndefined(conn._updatesForUnknownStores[coll_name]);
});
Tinytest.add("livedata stub - subscribe", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
// subscribe
var callback_fired = false;
var sub = conn.subscribe('my_data', function () {
callback_fired = true;
});
test.isFalse(callback_fired);
test.length(stream.sent, 1);
var message = JSON.parse(stream.sent.shift());
var id = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'my_data', params: []});
var reactivelyReady = false;
var autorunHandle = Tracker.autorun(function () {
reactivelyReady = sub.ready();
});
test.isFalse(reactivelyReady);
// get the sub satisfied. callback fires.
stream.receive({msg: 'ready', 'subs': [id]});
test.isTrue(callback_fired);
Tracker.flush();
test.isTrue(reactivelyReady);
// Unsubscribe.
sub.stop();
test.length(stream.sent, 1);
message = JSON.parse(stream.sent.shift());
test.equal(message, {msg: 'unsub', id: id});
Tracker.flush();
test.isFalse(reactivelyReady);
// Resubscribe.
conn.subscribe('my_data');
test.length(stream.sent, 1);
message = JSON.parse(stream.sent.shift());
var id2 = message.id;
test.notEqual(id, id2);
delete message.id;
test.equal(message, {msg: 'sub', name: 'my_data', params: []});
});
Tinytest.add("livedata stub - reactive subscribe", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
var rFoo = new ReactiveVar('foo1');
var rBar = new ReactiveVar('bar1');
var onReadyCount = {};
var onReady = function (tag) {
return function () {
if (_.has(onReadyCount, tag))
++onReadyCount[tag];
else
onReadyCount[tag] = 1;
};
};
// Subscribe to some subs.
var stopperHandle, completerHandle;
var autorunHandle = Tracker.autorun(function () {
conn.subscribe("foo", rFoo.get(), onReady(rFoo.get()));
conn.subscribe("bar", rBar.get(), onReady(rBar.get()));
completerHandle = conn.subscribe("completer", onReady("completer"));
stopperHandle = conn.subscribe("stopper", onReady("stopper"));
});
var completerReady;
var readyAutorunHandle = Tracker.autorun(function() {
completerReady = completerHandle.ready();
});
// Check sub messages. (Assume they are sent in the order executed.)
test.length(stream.sent, 4);
var message = JSON.parse(stream.sent.shift());
var idFoo1 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'foo', params: ['foo1']});
message = JSON.parse(stream.sent.shift());
var idBar1 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'bar', params: ['bar1']});
message = JSON.parse(stream.sent.shift());
var idCompleter = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'completer', params: []});
message = JSON.parse(stream.sent.shift());
var idStopper = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'stopper', params: []});
// Haven't hit onReady yet.
test.equal(onReadyCount, {});
Tracker.flush();
test.isFalse(completerReady);
// "completer" gets ready now. its callback should fire.
stream.receive({msg: 'ready', 'subs': [idCompleter]});
test.equal(onReadyCount, {completer: 1});
test.length(stream.sent, 0);
Tracker.flush();
test.isTrue(completerReady);
// Stop 'stopper'.
stopperHandle.stop();
test.length(stream.sent, 1);
message = JSON.parse(stream.sent.shift());
test.equal(message, {msg: 'unsub', id: idStopper});
test.equal(onReadyCount, {completer: 1});
Tracker.flush();
test.isTrue(completerReady);
// Change the foo subscription and flush. We should sub to the new foo
// subscription, re-sub to the stopper subscription, and then unsub from the old
// foo subscription. The bar subscription should be unaffected. The completer
// subscription should *NOT* call its new onReady callback, because we only
// call at most one onReady for a given reactively-saved subscription.
// The completerHandle should have been reestablished to the ready handle.
rFoo.set("foo2");
Tracker.flush();
test.length(stream.sent, 3);
message = JSON.parse(stream.sent.shift());
var idFoo2 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'foo', params: ['foo2']});
message = JSON.parse(stream.sent.shift());
var idStopperAgain = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'stopper', params: []});
message = JSON.parse(stream.sent.shift());
test.equal(message, {msg: 'unsub', id: idFoo1});
test.equal(onReadyCount, {completer: 1});
test.isTrue(completerReady);
// Ready the stopper and bar subs. Completing stopper should call only the
// onReady from the new subscription because they were separate subscriptions
// started at different times and the first one was explicitly torn down by
// the client; completing bar should call only the onReady from the new
// subscription because we only call at most one onReady per reactively-saved
// subscription.
stream.receive({msg: 'ready', 'subs': [idStopperAgain, idBar1]});
test.equal(onReadyCount, {completer: 1, bar1: 1, stopper: 1});
// Shut down the autorun. This should unsub us from all current subs at flush
// time.
autorunHandle.stop();
Tracker.flush();
test.isFalse(completerReady);
readyAutorunHandle.stop();
test.length(stream.sent, 4);
// The order of unsubs here is not important.
var unsubMessages = _.map(stream.sent, JSON.parse);
stream.sent.length = 0;
test.equal(_.unique(_.pluck(unsubMessages, 'msg')), ['unsub']);
var actualIds = _.pluck(unsubMessages, 'id');
var expectedIds = [idFoo2, idBar1, idCompleter, idStopperAgain];
actualIds.sort();
expectedIds.sort();
test.equal(actualIds, expectedIds);
});
Tinytest.add("livedata stub - reactive subscribe handle correct", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
var rFoo = new ReactiveVar('foo1');
// Subscribe to some subs.
var fooHandle, fooReady;
var autorunHandle = Tracker.autorun(function () {
fooHandle = conn.subscribe("foo", rFoo.get());
Tracker.autorun(function() {
fooReady = fooHandle.ready();
});
});
var message = JSON.parse(stream.sent.shift());
var idFoo1 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'foo', params: ['foo1']});
// Not ready yet
Tracker.flush();
test.isFalse(fooHandle.ready());
test.isFalse(fooReady);
// change the argument to foo. This will make a new handle, which isn't ready
// the ready autorun should invalidate, reading the new false value, and
// setting up a new dep which goes true soon
rFoo.set("foo2");
Tracker.flush();
test.length(stream.sent, 2);
message = JSON.parse(stream.sent.shift());
var idFoo2 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'foo', params: ['foo2']});
message = JSON.parse(stream.sent.shift());
test.equal(message, {msg: 'unsub', id: idFoo1});
Tracker.flush();
test.isFalse(fooHandle.ready());
test.isFalse(fooReady);
// "foo" gets ready now. The handle should be ready and the autorun rerun
stream.receive({msg: 'ready', 'subs': [idFoo2]});
test.length(stream.sent, 0);
Tracker.flush();
test.isTrue(fooHandle.ready());
test.isTrue(fooReady);
// change the argument to foo. This will make a new handle, which isn't ready
// the ready autorun should invalidate, making fooReady false too
rFoo.set("foo3");
Tracker.flush();
test.length(stream.sent, 2);
message = JSON.parse(stream.sent.shift());
var idFoo3 = message.id;
delete message.id;
test.equal(message, {msg: 'sub', name: 'foo', params: ['foo3']});
message = JSON.parse(stream.sent.shift());
test.equal(message, {msg: 'unsub', id: idFoo2});
Tracker.flush();
test.isFalse(fooHandle.ready());
test.isFalse(fooReady);
// "foo" gets ready again
stream.receive({msg: 'ready', 'subs': [idFoo3]});
test.length(stream.sent, 0);
Tracker.flush();
test.isTrue(fooHandle.ready());
test.isTrue(fooReady);
autorunHandle.stop();
});
Tinytest.add("livedata stub - this", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
conn.methods({test_this: function() {
test.isTrue(this.isSimulation);
this.unblock(); // should be a no-op
}});
// should throw no exceptions
conn.call('test_this', _.identity);
// satisfy method, quiesce connection
var message = JSON.parse(stream.sent.shift());
test.isUndefined(message.randomSeed);
test.equal(message, {msg: 'method', method: 'test_this',
params: [], id:message.id});
test.length(stream.sent, 0);
stream.receive({msg: 'result', id:message.id, result:null});
stream.receive({msg: 'updated', 'methods': [message.id]});
});
if (Meteor.isClient) {
Tinytest.add("livedata stub - methods", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
var collName = Random.id();
var coll = new Mongo.Collection(collName, {connection: conn});
// setup method
conn.methods({do_something: function (x) {
coll.insert({value: x});
}});
// setup observers
var counts = {added: 0, removed: 0, changed: 0, moved: 0};
var handle = coll.find({}).observe(
{ addedAt: function () { counts.added += 1; },
removedAt: function () { counts.removed += 1; },
changedAt: function () { counts.changed += 1; },
movedTo: function () { counts.moved += 1; }
});
// call method with results callback
var callback1Fired = false;
conn.call('do_something', 'friday!', function (err, res) {
test.isUndefined(err);
test.equal(res, '1234');
callback1Fired = true;
});
test.isFalse(callback1Fired);
// observers saw the method run.
test.equal(counts, {added: 1, removed: 0, changed: 0, moved: 0});
// get response from server
var message = testGotMessage(test, stream, {msg: 'method',
method: 'do_something',
params: ['friday!'],
id: '*',
randomSeed: '*'});
test.equal(coll.find({}).count(), 1);
test.equal(coll.find({value: 'friday!'}).count(), 1);
var docId = coll.findOne({value: 'friday!'})._id;
// results does not yet result in callback, because data is not
// ready.
stream.receive({msg: 'result', id:message.id, result: "1234"});
test.isFalse(callback1Fired);
// result message doesn't affect data
test.equal(coll.find({}).count(), 1);
test.equal(coll.find({value: 'friday!'}).count(), 1);
test.equal(counts, {added: 1, removed: 0, changed: 0, moved: 0});
// data methods do not show up (not quiescent yet)
stream.receive({msg: 'added', collection: collName, id: MongoID.idStringify(docId),
fields: {value: 'tuesday'}});
test.equal(coll.find({}).count(), 1);
test.equal(coll.find({value: 'friday!'}).count(), 1);
test.equal(counts, {added: 1, removed: 0, changed: 0, moved: 0});
// send another methods (unknown on client)
var callback2Fired = false;
conn.call('do_something_else', 'monday', function (err, res) {
callback2Fired = true;
});
test.isFalse(callback1Fired);
test.isFalse(callback2Fired);
// test we still send a method request to server
var message2 = JSON.parse(stream.sent.shift());
test.isUndefined(message2.randomSeed);
test.equal(message2, {msg: 'method', method: 'do_something_else',
params: ['monday'], id: message2.id});
// get the first data satisfied message. changes are applied to database even
// though another method is outstanding, because the other method didn't have
// a stub. and its callback is called.
stream.receive({msg: 'updated', 'methods': [message.id]});
test.isTrue(callback1Fired);
test.isFalse(callback2Fired);
test.equal(coll.find({}).count(), 1);
test.equal(coll.find({value: 'tuesday'}).count(), 1);
test.equal(counts, {added: 1, removed: 0, changed: 1, moved: 0});
// second result
stream.receive({msg: 'result', id:message2.id, result:"bupkis"});
test.isFalse(callback2Fired);
// get second satisfied; no new changes are applied.
stream.receive({msg: 'updated', 'methods': [message2.id]});
test.isTrue(callback2Fired);
test.equal(coll.find({}).count(), 1);
test.equal(coll.find({value: 'tuesday', _id: docId}).count(), 1);
test.equal(counts, {added: 1, removed: 0, changed: 1, moved: 0});
handle.stop();
});
}
Tinytest.add("livedata stub - mutating method args", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
conn.methods({mutateArgs: function (arg) {
arg.foo = 42;
}});
conn.call('mutateArgs', {foo: 50}, _.identity);
// Method should be called with original arg, not mutated arg.
var message = JSON.parse(stream.sent.shift());
test.isUndefined(message.randomSeed);
test.equal(message, {msg: 'method', method: 'mutateArgs',
params: [{foo: 50}], id: message.id});
test.length(stream.sent, 0);
});
var observeCursor = function (test, cursor) {
var counts = {added: 0, removed: 0, changed: 0, moved: 0};
var expectedCounts = _.clone(counts);
var handle = cursor.observe(
{ addedAt: function () { counts.added += 1; },
removedAt: function () { counts.removed += 1; },
changedAt: function () { counts.changed += 1; },
movedTo: function () { counts.moved += 1; }
});
return {
stop: _.bind(handle.stop, handle),
expectCallbacks: function (delta) {
_.each(delta, function (mod, field) {
expectedCounts[field] += mod;
});
test.equal(counts, expectedCounts);
}
};
};
// method calls another method in simulation. see not sent.
if (Meteor.isClient) {
Tinytest.add("livedata stub - methods calling methods", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
var coll_name = Random.id();
var coll = new Mongo.Collection(coll_name, {connection: conn});
// setup methods
conn.methods({
do_something: function () {
conn.call('do_something_else');
},
do_something_else: function () {
coll.insert({a: 1});
}
});
var o = observeCursor(test, coll.find());
// call method.
conn.call('do_something', _.identity);
// see we only send message for outer methods
var message = testGotMessage(test, stream, {msg: 'method',
method: 'do_something',
params: [],
id: '*',
randomSeed: '*'});
test.length(stream.sent, 0);
// but inner method runs locally.
o.expectCallbacks({added: 1});
test.equal(coll.find().count(), 1);
var docId = coll.findOne()._id;
test.equal(coll.findOne(), {_id: docId, a: 1});
// we get the results
stream.receive({msg: 'result', id:message.id, result:"1234"});
// get data from the method. data from this doc does not show up yet, but data
// from another doc does.
stream.receive({msg: 'added', collection: coll_name, id: MongoID.idStringify(docId),
fields: {value: 'tuesday'}});
o.expectCallbacks();
test.equal(coll.findOne(docId), {_id: docId, a: 1});
stream.receive({msg: 'added', collection: coll_name, id: 'monkey',
fields: {value: 'bla'}});
o.expectCallbacks({added: 1});
test.equal(coll.findOne(docId), {_id: docId, a: 1});
var newDoc = coll.findOne({value: 'bla'});
test.isTrue(newDoc);
test.equal(newDoc, {_id: newDoc._id, value: 'bla'});
// get method satisfied. all data shows up. the 'a' field is reverted and
// 'value' field is set.
stream.receive({msg: 'updated', 'methods': [message.id]});
o.expectCallbacks({changed: 1});
test.equal(coll.findOne(docId), {_id: docId, value: 'tuesday'});
test.equal(coll.findOne(newDoc._id), {_id: newDoc._id, value: 'bla'});
o.stop();
});
}
Tinytest.add("livedata stub - method call before connect", function (test) {
var stream = new StubStream;
var conn = newConnection(stream);
var callbackOutput = [];
conn.call('someMethod', function (err, result) {
callbackOutput.push(result);
});
test.equal(callbackOutput, []);
// the real stream drops all output pre-connection
stream.sent.length = 0;
// Now connect.
stream.reset();
testGotMessage(test, stream, makeConnectMessage());
testGotMessage(test, stream, {msg: 'method', method: 'someMethod',
params: [], id: '*'});
});
Tinytest.add("livedata stub - reconnect", function (test) {
var stream = new StubStream();
var conn = newConnection(stream);
startAndConnect(test, stream);
var collName = Random.id();
var coll = new Mongo.Collection(collName, {connection: conn});
var o = observeCursor(test, coll.find());
// subscribe
var subCallbackFired = false;
var sub = conn.subscribe('my_data', function () {
subCallbackFired = true;
});
test.isFalse(subCallbackFired);
var subMessage = JSON.parse(stream.sent.shift());
test.equal(subMessage, {msg: 'sub', name: 'my_data', params: [],
id: subMessage.id});
// get some data. it shows up.
stream.receive({msg: 'added', collection: collName,
id: '1234', fields: {a:1}});
test.equal(coll.find({}).count(), 1);
o.expectCallbacks({added: 1});
test.isFalse(subCallbackFired);
stream.receive({msg: 'changed', collection: collName,
id: '1234', fields: {b:2}});
stream.receive({msg: 'ready',
subs: [subMessage.id] // satisfy sub
});
test.isTrue(subCallbackFired);
subCallbackFired = false; // re-arm for test that it doesn't fire again.
test.equal(coll.find({a:1, b:2}).count(), 1);
o.expectCallbacks({changed: 1});
// call method.
var methodCallbackFired = false;
conn.call('do_something', function () {
methodCallbackFired = true;
});
conn.apply('do_something_else', [], {wait: true}, _.identity);
conn.apply('do_something_later', [], _.identity);
test.isFalse(methodCallbackFired);
// The non-wait method should send, but not the wait method.
var methodMessage = JSON.parse(stream.sent.shift());
test.isUndefined(methodMessage.randomSeed);
test.equal(methodMessage, {msg: 'method', method: 'do_something',
params: [], id:methodMessage.id});
test.equal(stream.sent.length, 0);
// more data. shows up immediately because there was no relevant method stub.
stream.receive({msg: 'changed', collection: collName,
id: '1234', fields: {c:3}});
test.equal(coll.findOne('1234'), {_id: '1234', a: 1, b: 2, c: 3});
o.expectCallbacks({changed: 1});
// stream reset. reconnect! we send a connect, our pending method, and our
// sub. The wait method still is blocked.
stream.reset();
testGotMessage(test, stream, makeConnectMessage(SESSION_ID));
testGotMessage(test, stream, methodMessage);
testGotMessage(test, stream, subMessage);
// reconnect with different session id
stream.receive({msg: 'connected', session: SESSION_ID + 1});
// resend data. doesn't show up: we're in reconnect quiescence.
stream.receive({msg: 'added', collection: collName,
id: '1234', fields: {a:1, b:2, c:3, d: 4}});
stream.receive({msg: 'added', collection: collName,
id: '2345', fields: {e: 5}});
test.equal(coll.findOne('1234'), {_id: '1234', a: 1, b: 2, c: 3});
test.isFalse(coll.findOne('2345'));
o.expectCallbacks();
// satisfy and return the method
stream.receive({msg: 'updated',
methods: [methodMessage.id]});
test.isFalse(methodCallbackFired);
stream.receive({msg: 'result', id:methodMessage.id, result:"bupkis"});
// The callback still doesn't fire (and we don't send the wait method): we're
// still in global quiescence
test.isFalse(methodCallbackFired);
test.equal(stream.sent.length, 0);
// still no update.
test.equal(coll.findOne('1234'), {_id: '1234', a: 1, b: 2, c: 3});
test.isFalse(coll.findOne('2345'));
o.expectCallbacks();
// re-satisfy sub
stream.receive({msg: 'ready', subs: [subMessage.id]});
// now the doc changes and method callback is called, and the wait method is
// sent. the sub callback isn't re-called.
test.isTrue(methodCallbackFired);
test.isFalse(subCallbackFired);
test.equal(coll.findOne('1234'), {_id: '1234', a: 1, b: 2, c: 3, d: 4});
test.equal(coll.findOne('2345'), {_id: '2345', e: 5});
o.expectCallbacks({added: 1, changed: 1});
var waitMethodMessage = JSON.parse(stream.sent.shift());
test.isUndefined(waitMethodMessage.randomSeed);
test.equal(waitMethodMessage, {msg: 'method', method: 'do_something_else',
params: [], id: waitMethodMessage.id});
test.equal(stream.sent.length, 0);
stream.receive({msg: 'result', id: waitMethodMessage.id, result: "bupkis"});
test.equal(stream.sent.length, 0);
stream.receive({msg: 'updated', methods: [waitMethodMessage.id]});
// wait method done means we can send the third method
test.equal(stream.sent.length, 1);
var laterMethodMessage = JSON.parse(stream.sent.shift());
test.isUndefined(laterMethodMessage.randomSeed);
test.equal(laterMethodMessage, {msg: 'method', method: 'do_something_later',
params: [], id: laterMethodMessage.id});
o.stop();
});
if (Meteor.isClient) {
Tinytest.add("livedata stub - reconnect method which only got result", function (test) {
var stream = new StubStream;
var conn = newConnection(stream);
startAndConnect(test, stream);
var collName = Random.id();
var coll = new Mongo.Collection(collName, {connection: conn});
var o = observeCursor(test, coll.find());
conn.methods({writeSomething: function () {
// stub write
coll.insert({foo: 'bar'});
}});
test.equal(coll.find({foo: 'bar'}).count(), 0);
// Call a method. We'll get the result but not data-done before reconnect.
var callbackOutput = [];
var onResultReceivedOutput = [];
conn.apply('writeSomething', [],
{onResultReceived: function (err, result) {
onResultReceivedOutput.push(result);
}},
function (err, result) {
callbackOutput.push(result);
});
// Stub write is visible.
test.equal(coll.find({foo: 'bar'}).count(), 1);
var stubWrittenId = coll.findOne({foo: 'bar'})._id;
o.expectCallbacks({added: 1});
// Callback not called.
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, []);
// Method sent.
var methodId = testGotMessage(
test, stream, {msg: 'method', method: 'writeSomething',
params: [], id: '*', randomSeed: '*'}).id;
test.equal(stream.sent.length, 0);
// Get some data.
stream.receive({msg: 'added', collection: collName,
id: MongoID.idStringify(stubWrittenId), fields: {baz: 42}});
// It doesn't show up yet.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne(stubWrittenId), {_id: stubWrittenId, foo: 'bar'});
o.expectCallbacks();
// Get the result.
stream.receive({msg: 'result', id: methodId, result: 'bla'});
// Data unaffected.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne(stubWrittenId), {_id: stubWrittenId, foo: 'bar'});
o.expectCallbacks();
// Callback not called, but onResultReceived is.
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, ['bla']);
// Reset stream. Method does NOT get resent, because its result is already
// in. Reconnect quiescence happens as soon as 'connected' is received because
// there are no pending methods or subs in need of revival.
stream.reset();
testGotMessage(test, stream, makeConnectMessage(SESSION_ID));
// Still holding out hope for session resumption, so nothing updated yet.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne(stubWrittenId), {_id: stubWrittenId, foo: 'bar'});
o.expectCallbacks();
test.equal(callbackOutput, []);
// Receive 'connected': time for reconnect quiescence! Data gets updated
// locally (ie, data is reset) and callback gets called.
stream.receive({msg: 'connected', session: SESSION_ID + 1});
test.equal(coll.find().count(), 0);
o.expectCallbacks({removed: 1});
test.equal(callbackOutput, ['bla']);
test.equal(onResultReceivedOutput, ['bla']);
stream.receive({msg: 'added', collection: collName,
id: MongoID.idStringify(stubWrittenId), fields: {baz: 42}});
test.equal(coll.findOne(stubWrittenId), {_id: stubWrittenId, baz: 42});
o.expectCallbacks({added: 1});
// Run method again. We're going to do the same thing this time, except we're
// also going to use an onReconnect to insert another method at reconnect
// time, which will delay reconnect quiescence.
conn.apply('writeSomething', [],
{onResultReceived: function (err, result) {
onResultReceivedOutput.push(result);
}},
function (err, result) {
callbackOutput.push(result);
});
// Stub write is visible.
test.equal(coll.find({foo: 'bar'}).count(), 1);
var stubWrittenId2 = coll.findOne({foo: 'bar'})._id;
o.expectCallbacks({added: 1});
// Callback not called.
test.equal(callbackOutput, ['bla']);
test.equal(onResultReceivedOutput, ['bla']);
// Method sent.
var methodId2 = testGotMessage(
test, stream, {msg: 'method', method: 'writeSomething',
params: [], id: '*', randomSeed: '*'}).id;
test.equal(stream.sent.length, 0);
// Get some data.
stream.receive({msg: 'added', collection: collName,
id: MongoID.idStringify(stubWrittenId2), fields: {baz: 42}});
// It doesn't show up yet.
test.equal(coll.find().count(), 2);
test.equal(coll.findOne(stubWrittenId2), {_id: stubWrittenId2, foo: 'bar'});
o.expectCallbacks();
// Get the result.
stream.receive({msg: 'result', id: methodId2, result: 'blab'});
// Data unaffected.
test.equal(coll.find().count(), 2);
test.equal(coll.findOne(stubWrittenId2), {_id: stubWrittenId2, foo: 'bar'});
o.expectCallbacks();
// Callback not called, but onResultReceived is.
test.equal(callbackOutput, ['bla']);
test.equal(onResultReceivedOutput, ['bla', 'blab']);
conn.onReconnect = function () {
conn.call('slowMethod', function (err, result) {
callbackOutput.push(result);
});
};
// Reset stream. Method does NOT get resent, because its result is already in,
// but slowMethod gets called via onReconnect. Reconnect quiescence is now
// blocking on slowMethod.
stream.reset();
testGotMessage(test, stream, makeConnectMessage(SESSION_ID + 1));
var slowMethodId = testGotMessage(
test, stream,
{msg: 'method', method: 'slowMethod', params: [], id: '*'}).id;
// Still holding out hope for session resumption, so nothing updated yet.
test.equal(coll.find().count(), 2);
test.equal(coll.findOne(stubWrittenId2), {_id: stubWrittenId2, foo: 'bar'});
o.expectCallbacks();
test.equal(callbackOutput, ['bla']);
// Receive 'connected'... but no reconnect quiescence yet due to slowMethod.
stream.receive({msg: 'connected', session: SESSION_ID + 2});
test.equal(coll.find().count(), 2);
test.equal(coll.findOne(stubWrittenId2), {_id: stubWrittenId2, foo: 'bar'});
o.expectCallbacks();
test.equal(callbackOutput, ['bla']);
// Receive data matching our stub. It doesn't take effect yet.
stream.receive({msg: 'added', collection: collName,
id: MongoID.idStringify(stubWrittenId2), fields: {foo: 'bar'}});
o.expectCallbacks();
// slowMethod is done writing, so we get full reconnect quiescence (but no
// slowMethod callback)... ie, a reset followed by applying the data we just
// got, as well as calling the callback from the method that half-finished
// before reset. The net effect is deleting doc 'stubWrittenId'.
stream.receive({msg: 'updated', methods: [slowMethodId]});
test.equal(coll.find().count(), 1);
test.equal(coll.findOne(stubWrittenId2), {_id: stubWrittenId2, foo: 'bar'});
o.expectCallbacks({removed: 1});
test.equal(callbackOutput, ['bla', 'blab']);
// slowMethod returns a value now.
stream.receive({msg: 'result', id: slowMethodId, result: 'slow'});
o.expectCallbacks();
test.equal(callbackOutput, ['bla', 'blab', 'slow']);
o.stop();
});
}
Tinytest.add("livedata stub - reconnect method which only got data", function (test) {
var stream = new StubStream;
var conn = newConnection(stream);
startAndConnect(test, stream);
var collName = Random.id();
var coll = new Mongo.Collection(collName, {connection: conn});
var o = observeCursor(test, coll.find());
// Call a method. We'll get the data-done message but not the result before
// reconnect.
var callbackOutput = [];
var onResultReceivedOutput = [];
conn.apply('doLittle', [],
{onResultReceived: function (err, result) {
onResultReceivedOutput.push(result);
}},
function (err, result) {
callbackOutput.push(result);
});
// Callbacks not called.
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, []);
// Method sent.
var methodId = testGotMessage(
test, stream, {msg: 'method', method: 'doLittle',
params: [], id: '*'}).id;
test.equal(stream.sent.length, 0);
// Get some data.
stream.receive({msg: 'added', collection: collName,
id: 'photo', fields: {baz: 42}});
// It shows up instantly because the stub didn't write anything.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne('photo'), {_id: 'photo', baz: 42});
o.expectCallbacks({added: 1});
// Get the data-done message.
stream.receive({msg: 'updated', methods: [methodId]});
// Data still here.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne('photo'), {_id: 'photo', baz: 42});
o.expectCallbacks();
// Method callback not called yet (no result yet).
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, []);
// Reset stream. Method gets resent (with same ID), and blocks reconnect
// quiescence.
stream.reset();
testGotMessage(test, stream, makeConnectMessage(SESSION_ID));
testGotMessage(
test, stream, {msg: 'method', method: 'doLittle',
params: [], id: methodId});
// Still holding out hope for session resumption, so nothing updated yet.
test.equal(coll.find().count(), 1);
test.equal(coll.findOne('photo'), {_id: 'photo', baz: 42});
o.expectCallbacks();
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, []);
// Receive 'connected'. Still blocking on reconnect quiescence.
stream.receive({msg: 'connected', session: SESSION_ID + 1});
test.equal(coll.find().count(), 1);
test.equal(coll.findOne('photo'), {_id: 'photo', baz: 42});
o.expectCallbacks();
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, []);
// Receive method result. onResultReceived is called but the main callback
// isn't (ie, we don't get confused by the fact that we got data-done the
// *FIRST* time through).
stream.receive({msg: 'result', id: methodId, result: 'res'});
test.equal(callbackOutput, []);
test.equal(onResultReceivedOutput, ['res']);
// Now we get data-done. Collection is reset and callback is called.
stream.receive({msg: 'updated', methods: [methodId]});
test.equal(coll.find().count(), 0);
o.expectCallbacks({removed: 1});
test.equal(callbackOutput, ['res']);