From e98ed99fe76621c82b5b4b3d690c51c726554caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 5 Feb 2016 09:21:25 +0100 Subject: [PATCH] Fix race condition in replication tests --- test/replication.test.js | 98 ++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/test/replication.test.js b/test/replication.test.js index 2606aaf5..ecdffbf8 100644 --- a/test/replication.test.js +++ b/test/replication.test.js @@ -67,40 +67,26 @@ describe('Replication / Change APIs', function() { }); it('should call rectifyAllChanges if no id is passed for rectifyOnDelete', function(done) { - SourceModel.rectifyChange = function() { - return done(new Error('Should not call rectifyChange')); - }; - SourceModel.rectifyAllChanges = function() { - return done(); - }; + var calls = mockSourceModelRectify(); SourceModel.destroyAll({name: 'John'}, function(err, data) { - if (err) - return done(err); + if (err) return done(err); + expect(calls).to.eql(['rectifyAllChanges']); + done(); }); }); it('should call rectifyAllChanges if no id is passed for rectifyOnSave', function(done) { - SourceModel.rectifyChange = function() { - return done(new Error('Should not call rectifyChange')); - }; - SourceModel.rectifyAllChanges = function() { - return done(); - }; + var calls = mockSourceModelRectify(); var newData = {'name': 'Janie'}; SourceModel.update({name: 'Jane'}, newData, function(err, data) { - if (err) - return done(err); + if (err) return done(err); + expect(calls).to.eql(['rectifyAllChanges']); + done(); }); }); it('rectifyOnDelete for Delete should call rectifyChange instead of rectifyAllChanges', function(done) { - TargetModel.rectifyChange = function() { - return done(); - }; - TargetModel.rectifyAllChanges = function() { - return done(new Error('Should not call rectifyAllChanges')); - }; - + var calls = mockTargetModelRectify(); async.waterfall([ function(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 } ], function(err, results) { - if (err) - return done(err); + if (err) return done(err); + expect(calls).to.eql(['rectifyChange']); + done(); }); }); it('rectifyOnSave for Update should call rectifyChange instead of rectifyAllChanges', function(done) { - TargetModel.rectifyChange = function() { - return done(); - }; - TargetModel.rectifyAllChanges = function() { - return done(new Error('Should not call rectifyAllChanges')); - }; - + var calls = mockTargetModelRectify(); var newData = {'name': 'Janie'}; async.waterfall([ function(callback) { @@ -131,20 +112,16 @@ describe('Replication / Change APIs', function() { function(data, callback) { SourceModel.replicate(TargetModel, callback); // replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation - }], function(err, result) { - if (err) - return done(err); + } + ], function(err, result) { + if (err) return done(err); + expect(calls).to.eql(['rectifyChange']); + done(); }); }); it('rectifyOnSave for Create should call rectifyChange instead of rectifyAllChanges', function(done) { - TargetModel.rectifyChange = function() { - return done(); - }; - TargetModel.rectifyAllChanges = function() { - return done(new Error('Should not call rectifyAllChanges')); - }; - + var calls = mockTargetModelRectify(); var newData = [{name: 'Janie', surname: 'Doe'}]; async.waterfall([ function(callback) { @@ -155,10 +132,43 @@ describe('Replication / Change APIs', function() { // replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation } ], function(err, result) { - if (err) - return done(err); + if (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() {