Skip to content

Commit 632fad3

Browse files
authored
Merge pull request #227 from alecgibson/fix-flaky-test
Fix flaky maxSubmitRetries test
2 parents 6ba106b + 08eb790 commit 632fad3

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

test/client/submit.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,25 +509,44 @@ 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+
// The submit retry happens just after an op is committed. This hook into the middleware
523+
// catches both ops just before they're about to be committed. This ensures that both ops
524+
// are certainly working on the same snapshot (ie one op hasn't been committed before the
525+
// other fetches the snapshot to apply to). By storing the callbacks, we can then
526+
// manually trigger the callbacks, first calling doc, and when we know that's been committed,
527+
// we then commit doc2.
528+
backend.use('commit', function (request, callback) {
529+
if (request.op.op[0].na === 2) docCallback = callback;
530+
if (request.op.op[0].na === 7) doc2Callback = callback;
531+
532+
// Wait until both ops have been applied to the same snapshot and are about to be committed
533+
if (docCallback && doc2Callback) {
534+
// Trigger the first op's commit and then the second one later, which will cause the
535+
// second op to retry
536+
docCallback();
527537
}
528-
};
529-
doc.submitOp({p: ['age'], na: 2}, cb);
530-
doc2.submitOp({p: ['age'], na: 7}, cb);
538+
});
539+
doc.submitOp({p: ['age'], na: 2}, function (error) {
540+
if (error) return done(error);
541+
// When we know the first op has been committed, we try to commit the second op, which will
542+
// fail because it's working on an out-of-date snapshot. It will retry, but exceed the
543+
// maxSubmitRetries limit of 0
544+
doc2Callback();
545+
});
546+
doc2.submitOp({p: ['age'], na: 7}, function (error) {
547+
expect(error).ok();
548+
done();
549+
});
531550
});
532551
});
533552
});

0 commit comments

Comments
 (0)