Fix race condition in replication tests

This commit is contained in:
Miroslav Bajtoš 2016-02-05 09:21:25 +01:00
parent a0806eab89
commit e98ed99fe7
1 changed files with 54 additions and 44 deletions

View File

@ -67,40 +67,26 @@ describe('Replication / Change APIs', function() {
}); });
it('should call rectifyAllChanges if no id is passed for rectifyOnDelete', function(done) { it('should call rectifyAllChanges if no id is passed for rectifyOnDelete', function(done) {
SourceModel.rectifyChange = function() { var calls = mockSourceModelRectify();
return done(new Error('Should not call rectifyChange'));
};
SourceModel.rectifyAllChanges = function() {
return done();
};
SourceModel.destroyAll({name: 'John'}, function(err, data) { SourceModel.destroyAll({name: 'John'}, function(err, data) {
if (err) if (err) return done(err);
return done(err); expect(calls).to.eql(['rectifyAllChanges']);
done();
}); });
}); });
it('should call rectifyAllChanges if no id is passed for rectifyOnSave', function(done) { it('should call rectifyAllChanges if no id is passed for rectifyOnSave', function(done) {
SourceModel.rectifyChange = function() { var calls = mockSourceModelRectify();
return done(new Error('Should not call rectifyChange'));
};
SourceModel.rectifyAllChanges = function() {
return done();
};
var newData = {'name': 'Janie'}; var newData = {'name': 'Janie'};
SourceModel.update({name: 'Jane'}, newData, function(err, data) { SourceModel.update({name: 'Jane'}, newData, function(err, data) {
if (err) if (err) return done(err);
return done(err); expect(calls).to.eql(['rectifyAllChanges']);
done();
}); });
}); });
it('rectifyOnDelete for Delete should call rectifyChange instead of rectifyAllChanges', function(done) { it('rectifyOnDelete for Delete should call rectifyChange instead of rectifyAllChanges', function(done) {
TargetModel.rectifyChange = function() { var calls = mockTargetModelRectify();
return done();
};
TargetModel.rectifyAllChanges = function() {
return done(new Error('Should not call rectifyAllChanges'));
};
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
SourceModel.destroyAll({name: 'John'}, callback); SourceModel.destroyAll({name: 'John'}, callback);
@ -110,19 +96,14 @@ describe('Replication / Change APIs', function() {
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation // replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
} }
], function(err, results) { ], function(err, results) {
if (err) if (err) return done(err);
return done(err); expect(calls).to.eql(['rectifyChange']);
done();
}); });
}); });
it('rectifyOnSave for Update should call rectifyChange instead of rectifyAllChanges', function(done) { it('rectifyOnSave for Update should call rectifyChange instead of rectifyAllChanges', function(done) {
TargetModel.rectifyChange = function() { var calls = mockTargetModelRectify();
return done();
};
TargetModel.rectifyAllChanges = function() {
return done(new Error('Should not call rectifyAllChanges'));
};
var newData = {'name': 'Janie'}; var newData = {'name': 'Janie'};
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
@ -131,20 +112,16 @@ describe('Replication / Change APIs', function() {
function(data, callback) { function(data, callback) {
SourceModel.replicate(TargetModel, callback); SourceModel.replicate(TargetModel, callback);
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation // replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
}], function(err, result) { }
if (err) ], function(err, result) {
return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyChange']);
done();
}); });
}); });
it('rectifyOnSave for Create should call rectifyChange instead of rectifyAllChanges', function(done) { it('rectifyOnSave for Create should call rectifyChange instead of rectifyAllChanges', function(done) {
TargetModel.rectifyChange = function() { var calls = mockTargetModelRectify();
return done();
};
TargetModel.rectifyAllChanges = function() {
return done(new Error('Should not call rectifyAllChanges'));
};
var newData = [{name: 'Janie', surname: 'Doe'}]; var newData = [{name: 'Janie', surname: 'Doe'}];
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
@ -155,10 +132,43 @@ describe('Replication / Change APIs', function() {
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation // replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
} }
], function(err, result) { ], function(err, result) {
if (err) if (err) return done(err);
return done(err); expect(calls).to.eql(['rectifyChange']);
done();
}); });
}); });
function mockSourceModelRectify() {
var calls = [];
SourceModel.rectifyChange = function(id, cb) {
calls.push('rectifyChange');
process.nextTick(cb);
};
SourceModel.rectifyAllChanges = function(cb) {
calls.push('rectifyAllChanges');
process.nextTick(cb);
};
return calls;
}
function mockTargetModelRectify() {
var calls = [];
TargetModel.rectifyChange = function(id, cb) {
calls.push('rectifyChange');
process.nextTick(cb);
};
TargetModel.rectifyAllChanges = function(cb) {
calls.push('rectifyAllChanges');
process.nextTick(cb);
};
return calls;
}
}); });
describe('Model.changes(since, filter, callback)', function() { describe('Model.changes(since, filter, callback)', function() {