Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky maxSubmitRetries test #227

Merged
merged 4 commits into from
Jul 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions test/client/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,25 +509,44 @@ describe('client submit', function() {
});

it('submits fail above the backend.maxSubmitRetries threshold', function(done) {
var backend = this.backend;
this.backend.maxSubmitRetries = 0;
var doc = this.backend.connect().get('dogs', 'fido');
var doc2 = this.backend.connect().get('dogs', 'fido');
doc.create({age: 3}, function(err) {
if (err) return done(err);
doc2.fetch(function(err) {
if (err) return done(err);
var count = 0;
var cb = function(err) {
count++;
if (count === 1) {
if (err) return done(err);
} else {
expect(err).ok();
done();
var docCallback;
var doc2Callback;
// The submit retry happens just after an op is committed. This hook into the middleware
// catches both ops just before they're about to be committed. This ensures that both ops
// are certainly working on the same snapshot (ie one op hasn't been committed before the
// other fetches the snapshot to apply to). By storing the callbacks, we can then
// manually trigger the callbacks, first calling doc, and when we know that's been committed,
// we then commit doc2.
backend.use('commit', function (request, callback) {
if (request.op.op[0].na === 2) docCallback = callback;
if (request.op.op[0].na === 7) doc2Callback = callback;

// Wait until both ops have been applied to the same snapshot and are about to be committed
if (docCallback && doc2Callback) {
// Trigger the first op's commit and then the second one later, which will cause the
// second op to retry
docCallback();
}
};
doc.submitOp({p: ['age'], na: 2}, cb);
doc2.submitOp({p: ['age'], na: 7}, cb);
});
doc.submitOp({p: ['age'], na: 2}, function (error) {
if (error) return done(error);
// When we know the first op has been committed, we try to commit the second op, which will
// fail because it's working on an out-of-date snapshot. It will retry, but exceed the
// maxSubmitRetries limit of 0
doc2Callback();
});
doc2.submitOp({p: ['age'], na: 7}, function (error) {
expect(error).ok();
done();
});
});
});
});
Expand Down