Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit c210adf

Browse files
authored
Fix flakey test that causes issues with sharedb-mongo in Travis (share#324)
* Fix flakey test * pass errors from doc to done() in tests * MemoryPubSub: remove check for callback that is always provided
1 parent 66e8866 commit c210adf

File tree

2 files changed

+67
-62
lines changed

2 files changed

+67
-62
lines changed

lib/pubsub/memory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ module.exports = MemoryPubSub;
1717
MemoryPubSub.prototype = Object.create(PubSub.prototype);
1818

1919
MemoryPubSub.prototype._subscribe = function(channel, callback) {
20-
if (callback) process.nextTick(callback);
20+
process.nextTick(callback);
2121
};
2222

2323
MemoryPubSub.prototype._unsubscribe = function(channel, callback) {
24-
if (callback) process.nextTick(callback);
24+
process.nextTick(callback);
2525
};
2626

2727
MemoryPubSub.prototype._publish = function(channels, data, callback) {
@@ -33,6 +33,6 @@ MemoryPubSub.prototype._publish = function(channels, data, callback) {
3333
pubsub._emit(channel, data);
3434
}
3535
}
36-
if (callback) callback();
36+
callback();
3737
});
3838
};

test/client/subscribe.js

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = function() {
1111

1212
['fetch', 'subscribe'].forEach(function(method) {
1313
it(method + ' gets initial data', function(done) {
14-
var doc = this.backend.connect().get('dogs', 'fido');
15-
var doc2 = this.backend.connect().get('dogs', 'fido');
14+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
15+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
1616
doc.create({age: 3}, function(err) {
1717
if (err) return done(err);
1818
doc2[method](function(err) {
@@ -25,8 +25,8 @@ module.exports = function() {
2525
});
2626

2727
it(method + ' twice simultaneously calls back', function(done) {
28-
var doc = this.backend.connect().get('dogs', 'fido');
29-
var doc2 = this.backend.connect().get('dogs', 'fido');
28+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
29+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
3030
doc.create({age: 3}, function(err) {
3131
if (err) return done(err);
3232
async.parallel([
@@ -46,8 +46,8 @@ module.exports = function() {
4646
});
4747

4848
it(method + ' twice in bulk simultaneously calls back', function(done) {
49-
var doc = this.backend.connect().get('dogs', 'fido');
50-
var doc2 = this.backend.connect().get('dogs', 'fido');
49+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
50+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
5151
doc.create({age: 3}, function(err) {
5252
if (err) return done(err);
5353
doc2.connection.startBulk();
@@ -83,9 +83,9 @@ module.exports = function() {
8383
}
8484
], function(err) {
8585
if (err) return done(err);
86-
var fido = connection2.get('dogs', 'fido');
87-
var spot = connection2.get('dogs', 'spot');
88-
var finn = connection2.get('cats', 'finn');
86+
var fido = connection2.get('dogs', 'fido').on('error', done);
87+
var spot = connection2.get('dogs', 'spot').on('error', done);
88+
var finn = connection2.get('cats', 'finn').on('error', done);
8989
connection2.startBulk();
9090
async.parallel([
9191
function(cb) {
@@ -111,9 +111,9 @@ module.exports = function() {
111111
it(method + ' bulk on same collection from known version', function(done) {
112112
var connection = this.backend.connect();
113113
var connection2 = this.backend.connect();
114-
var fido = connection2.get('dogs', 'fido');
115-
var spot = connection2.get('dogs', 'spot');
116-
var finn = connection2.get('cats', 'finn');
114+
var fido = connection2.get('dogs', 'fido').on('error', done);
115+
var spot = connection2.get('dogs', 'spot').on('error', done);
116+
var finn = connection2.get('cats', 'finn').on('error', done);
117117
connection2.startBulk();
118118
async.parallel([
119119
function(cb) {
@@ -221,8 +221,8 @@ module.exports = function() {
221221
});
222222

223223
it(method + ' gets new ops', function(done) {
224-
var doc = this.backend.connect().get('dogs', 'fido');
225-
var doc2 = this.backend.connect().get('dogs', 'fido');
224+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
225+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
226226
doc.create({age: 3}, function(err) {
227227
if (err) return done(err);
228228
doc2.fetch(function(err) {
@@ -240,8 +240,8 @@ module.exports = function() {
240240

241241
it(method + ' calls back after reconnect', function(done) {
242242
var backend = this.backend;
243-
var doc = this.backend.connect().get('dogs', 'fido');
244-
var doc2 = this.backend.connect().get('dogs', 'fido');
243+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
244+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
245245
doc.create({age: 3}, function(err) {
246246
if (err) return done(err);
247247
doc2[method](function(err) {
@@ -261,8 +261,8 @@ module.exports = function() {
261261
this.backend.use('doc', function(request, next) {
262262
next({message: 'Reject doc read'});
263263
});
264-
var doc = this.backend.connect().get('dogs', 'fido');
265-
var doc2 = this.backend.connect().get('dogs', 'fido');
264+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
265+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
266266
doc.create({age: 3}, function(err) {
267267
if (err) return done(err);
268268
doc2[method](function(err) {
@@ -293,7 +293,7 @@ module.exports = function() {
293293
});
294294

295295
it(method + ' will call back when ops are pending', function(done) {
296-
var doc = this.backend.connect().get('dogs', 'fido');
296+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
297297
doc.create({age: 3}, function(err) {
298298
if (err) return done(err);
299299
doc.pause();
@@ -303,7 +303,7 @@ module.exports = function() {
303303
});
304304

305305
it(method + ' will not call back when creating the doc is pending', function(done) {
306-
var doc = this.backend.connect().get('dogs', 'fido');
306+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
307307
doc.pause();
308308
doc.create({age: 3});
309309
doc[method](done);
@@ -312,7 +312,7 @@ module.exports = function() {
312312
});
313313

314314
it(method + ' will wait for write when doc is locally created', function(done) {
315-
var doc = this.backend.connect().get('dogs', 'fido');
315+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
316316
doc.pause();
317317
var calls = 0;
318318
doc.create({age: 3}, function(err) {
@@ -332,8 +332,8 @@ module.exports = function() {
332332
});
333333

334334
it(method + ' will wait for write when doc is locally created and will fail to submit', function(done) {
335-
var doc = this.backend.connect().get('dogs', 'fido');
336-
var doc2 = this.backend.connect().get('dogs', 'fido');
335+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
336+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
337337
doc2.create({age: 5}, function(err) {
338338
if (err) return done(err);
339339
doc.pause();
@@ -357,7 +357,7 @@ module.exports = function() {
357357
});
358358

359359
it('unsubscribe calls back immediately on disconnect', function(done) {
360-
var doc = this.backend.connect().get('dogs', 'fido');
360+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
361361
doc.subscribe(function(err) {
362362
if (err) return done(err);
363363
doc.unsubscribe(done);
@@ -366,7 +366,7 @@ module.exports = function() {
366366
});
367367

368368
it('unsubscribe calls back immediately when already disconnected', function(done) {
369-
var doc = this.backend.connect().get('dogs', 'fido');
369+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
370370
doc.subscribe(function(err) {
371371
if (err) return done(err);
372372
doc.connection.close();
@@ -375,8 +375,8 @@ module.exports = function() {
375375
});
376376

377377
it('subscribed client gets create from other client', function(done) {
378-
var doc = this.backend.connect().get('dogs', 'fido');
379-
var doc2 = this.backend.connect().get('dogs', 'fido');
378+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
379+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
380380
doc2.subscribe(function(err) {
381381
if (err) return done(err);
382382
doc2.on('create', function(context) {
@@ -390,8 +390,8 @@ module.exports = function() {
390390
});
391391

392392
it('subscribed client gets op from other client', function(done) {
393-
var doc = this.backend.connect().get('dogs', 'fido');
394-
var doc2 = this.backend.connect().get('dogs', 'fido');
393+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
394+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
395395
doc.create({age: 3}, function(err) {
396396
if (err) return done(err);
397397
doc2.subscribe(function(err) {
@@ -407,8 +407,8 @@ module.exports = function() {
407407
});
408408

409409
it('disconnecting stops op updates', function(done) {
410-
var doc = this.backend.connect().get('dogs', 'fido');
411-
var doc2 = this.backend.connect().get('dogs', 'fido');
410+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
411+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
412412
doc.create({age: 3}, function(err) {
413413
if (err) return done(err);
414414
doc2.subscribe(function(err) {
@@ -424,8 +424,8 @@ module.exports = function() {
424424

425425
it('backend.suppressPublish stops op updates', function(done) {
426426
var backend = this.backend;
427-
var doc = this.backend.connect().get('dogs', 'fido');
428-
var doc2 = this.backend.connect().get('dogs', 'fido');
427+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
428+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
429429
doc.create({age: 3}, function(err) {
430430
if (err) return done(err);
431431
doc2.subscribe(function(err) {
@@ -440,8 +440,8 @@ module.exports = function() {
440440
});
441441

442442
it('unsubscribe stops op updates', function(done) {
443-
var doc = this.backend.connect().get('dogs', 'fido');
444-
var doc2 = this.backend.connect().get('dogs', 'fido');
443+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
444+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
445445
doc.create({age: 3}, function(err) {
446446
if (err) return done(err);
447447
doc2.subscribe(function(err) {
@@ -460,8 +460,8 @@ module.exports = function() {
460460
it('doc destroy stops op updates', function(done) {
461461
var connection1 = this.backend.connect();
462462
var connection2 = this.backend.connect();
463-
var doc = connection1.get('dogs', 'fido');
464-
var doc2 = connection2.get('dogs', 'fido');
463+
var doc = connection1.get('dogs', 'fido').on('error', done);
464+
var doc2 = connection2.get('dogs', 'fido').on('error', done);
465465
doc.create({age: 3}, function(err) {
466466
if (err) return done(err);
467467
doc2.subscribe(function(err) {
@@ -480,7 +480,7 @@ module.exports = function() {
480480

481481
it('doc destroy removes doc from connection when doc is not subscribed', function(done) {
482482
var connection = this.backend.connect();
483-
var doc = connection.get('dogs', 'fido');
483+
var doc = connection.get('dogs', 'fido').on('error', done);
484484
expect(connection.getExisting('dogs', 'fido')).equal(doc);
485485
doc.destroy(function(err) {
486486
if (err) return done(err);
@@ -492,9 +492,9 @@ module.exports = function() {
492492
it('bulk unsubscribe stops op updates', function(done) {
493493
var connection = this.backend.connect();
494494
var connection2 = this.backend.connect();
495-
var doc = connection.get('dogs', 'fido');
496-
var fido = connection2.get('dogs', 'fido');
497-
var spot = connection2.get('dogs', 'spot');
495+
var doc = connection.get('dogs', 'fido').on('error', done);
496+
var fido = connection2.get('dogs', 'fido').on('error', done);
497+
var spot = connection2.get('dogs', 'spot').on('error', done);
498498
doc.create({age: 3}, function(err) {
499499
if (err) return done(err);
500500
async.parallel([
@@ -528,8 +528,8 @@ module.exports = function() {
528528

529529
it('a subscribed doc is re-subscribed after reconnect and gets any missing ops', function(done) {
530530
var backend = this.backend;
531-
var doc = this.backend.connect().get('dogs', 'fido');
532-
var doc2 = this.backend.connect().get('dogs', 'fido');
531+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
532+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
533533
doc.create({age: 3}, function(err) {
534534
if (err) return done(err);
535535
doc2.subscribe(function(err) {
@@ -550,8 +550,8 @@ module.exports = function() {
550550
});
551551

552552
it('calling subscribe, unsubscribe, subscribe sync leaves a doc subscribed', function(done) {
553-
var doc = this.backend.connect().get('dogs', 'fido');
554-
var doc2 = this.backend.connect().get('dogs', 'fido');
553+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
554+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
555555
doc.create({age: 3}, function(err) {
556556
if (err) return done(err);
557557
doc2.subscribe();
@@ -568,8 +568,8 @@ module.exports = function() {
568568

569569
it('doc fetches ops to catch up if it receives a future op', function(done) {
570570
var backend = this.backend;
571-
var doc = this.backend.connect().get('dogs', 'fido');
572-
var doc2 = this.backend.connect().get('dogs', 'fido');
571+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
572+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
573573
doc.create({age: 3}, function(err) {
574574
if (err) return done(err);
575575
doc2.subscribe(function(err) {
@@ -598,8 +598,8 @@ module.exports = function() {
598598

599599
it('doc fetches ops to catch up if it receives multiple future ops', function(done) {
600600
var backend = this.backend;
601-
var doc = this.backend.connect().get('dogs', 'fido');
602-
var doc2 = this.backend.connect().get('dogs', 'fido');
601+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
602+
var doc2 = this.backend.connect().get('dogs', 'fido').on('error', done);
603603
// Delaying op replies will cause multiple future ops to be received
604604
// before the fetch to catch up completes
605605
backend.use('op', function(request, next) {
@@ -614,7 +614,11 @@ module.exports = function() {
614614
if (--wait) return;
615615
expect(doc2.version).eql(5);
616616
expect(doc2.data).eql({age: 122});
617-
done();
617+
// Wait for whenNothingPending, because the doc might have kicked
618+
// off multiple fetches, and some could be pending still. We want to
619+
// resolve all inflight requests of the database before closing and
620+
// proceeding to the next test
621+
doc2.whenNothingPending(done);
618622
});
619623
backend.suppressPublish = true;
620624
doc.submitOp({p: ['age'], na: 1}, function(err) {
@@ -633,19 +637,20 @@ module.exports = function() {
633637
});
634638

635639
describe('doc.subscribed', function() {
636-
it('is set to false initially', function() {
637-
var doc = this.backend.connect().get('dogs', 'fido');
640+
it('is set to false initially', function(done) {
641+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
638642
expect(doc.subscribed).equal(false);
643+
done();
639644
});
640645

641-
it('remains false before subscribe call completes', function() {
642-
var doc = this.backend.connect().get('dogs', 'fido');
643-
doc.subscribe();
646+
it('remains false before subscribe call completes', function(done) {
647+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
648+
doc.subscribe(done);
644649
expect(doc.subscribed).equal(false);
645650
});
646651

647652
it('is set to true after subscribe completes', function(done) {
648-
var doc = this.backend.connect().get('dogs', 'fido');
653+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
649654
doc.subscribe(function(err) {
650655
if (err) return done(err);
651656
expect(doc.subscribed).equal(true);
@@ -654,7 +659,7 @@ module.exports = function() {
654659
});
655660

656661
it('is not set to true after subscribe completes if already unsubscribed', function(done) {
657-
var doc = this.backend.connect().get('dogs', 'fido');
662+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
658663
doc.subscribe(function(err) {
659664
if (err) return done(err);
660665
expect(doc.subscribed).equal(false);
@@ -664,7 +669,7 @@ module.exports = function() {
664669
});
665670

666671
it('is set to false sychronously in unsubscribe', function(done) {
667-
var doc = this.backend.connect().get('dogs', 'fido');
672+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
668673
doc.subscribe(function(err) {
669674
if (err) return done(err);
670675
expect(doc.subscribed).equal(true);
@@ -675,7 +680,7 @@ module.exports = function() {
675680
});
676681

677682
it('is set to false sychronously on disconnect', function(done) {
678-
var doc = this.backend.connect().get('dogs', 'fido');
683+
var doc = this.backend.connect().get('dogs', 'fido').on('error', done);
679684
doc.subscribe(function(err) {
680685
if (err) return done(err);
681686
expect(doc.subscribed).equal(true);

0 commit comments

Comments
 (0)