Skip to content

Commit 8332411

Browse files
author
Alec Gibson
committed
Fix flaky maxSubmitRetries test
This is a slightly speculative fix for a test that fails intermittently on `sharedb-mongo`. I believe these intermittent failures are due to a race condition in a concurrency test. The test works by attempting to fire two commits off at the same time, and hoping that one of them is committed just before the other, so that a `SubmitRequest.retry` is triggered whilst the `maxSubmitRetries` is set to `0`, resulting in an error that is expected. However, I believe it's possible for these commits to (in some cases) happen sequentially rather than concurrently, and fail to error. This change attempts to force them into this retry condition by: - Catching both ops in the `commit` middleware, _just_ before they're about to be committed (and hit a `retry` if applicable) - Waiting until both ops have reached this state - Triggering the first op's `commit` - Then in the callback of that op, triggering the second op's `commit` - The second op should now find that the first op has beaten it to committing, and trigger a `retry`
1 parent 762e05d commit 8332411

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

test/client/submit.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,25 +509,33 @@ describe('client submit', function() {
509509
});
510510

511511
it('submits fail above the backend.maxSubmitRetries threshold', function(done) {
512+
var backend = this.backend;
512513
this.backend.maxSubmitRetries = 0;
513514
var doc = this.backend.connect().get('dogs', 'fido');
514515
var doc2 = this.backend.connect().get('dogs', 'fido');
515516
doc.create({age: 3}, function(err) {
516517
if (err) return done(err);
517518
doc2.fetch(function(err) {
518519
if (err) return done(err);
519-
var count = 0;
520-
var cb = function(err) {
521-
count++;
522-
if (count === 1) {
523-
if (err) return done(err);
524-
} else {
525-
expect(err).ok();
526-
done();
520+
var docCallback;
521+
var doc2Callback;
522+
backend.use('commit', function (request, callback) {
523+
if (request.op.op[0].na === 2) docCallback = callback;
524+
if (request.op.op[0].na === 7) doc2Callback = callback;
525+
526+
if (docCallback && doc2Callback) {
527+
docCallback();
527528
}
528-
};
529-
doc.submitOp({p: ['age'], na: 2}, cb);
530-
doc2.submitOp({p: ['age'], na: 7}, cb);
529+
});
530+
531+
doc.submitOp({p: ['age'], na: 2}, function (error) {
532+
if (error) return done(error);
533+
doc2Callback();
534+
});
535+
doc2.submitOp({p: ['age'], na: 7}, function (error) {
536+
expect(error).ok();
537+
done();
538+
});
531539
});
532540
});
533541
});

0 commit comments

Comments
 (0)