From b1b43008c931044a7cd6b6f8d5833b08c13b67c6 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 9 Feb 2014 19:22:11 +0100 Subject: [PATCH] Executor bug completely fixed --- lib/executor.js | 2 +- test/executor.test.js | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/executor.js b/lib/executor.js index 7128ef7..f06f1ab 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -22,7 +22,7 @@ function Executor () { // Always tell the queue task is complete. Execute callback if any was given. if (typeof lastArg === 'function') { callback = function () { - cb(); + process.nextTick(cb); lastArg.apply(null, arguments); }; diff --git a/test/executor.test.js b/test/executor.test.js index 575236b..17b907e 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -49,18 +49,28 @@ function testRightOrder (d, done) { // Do nothing with the error which is only there to test we stay on track }); - d.find({}, function (err) { - process.nextTick(function () { - d.insert({ bar: 1 }, function (err) { - for (var i = 0; i < currentUncaughtExceptionHandlers.length; i += 1) { - process.on('uncaughtException', currentUncaughtExceptionHandlers[i]); - } - - done(); + d.find({}, function (err, docs) { + docs.length.should.equal(0); + + d.insert({ a: 1 }, function () { + d.update({ a: 1 }, { a: 2 }, {}, function () { + d.find({}, function (err, docs) { + docs[0].a.should.equal(2); + + process.nextTick(function () { + d.update({ a: 2 }, { a: 3 }, {}, function () { + d.find({}, function (err, docs) { + docs[0].a.should.equal(3); + done(); + + }); + }); + }); + + throw 'Some error'; + }); }); }); - - throw 'Some error'; }); } @@ -68,7 +78,7 @@ function testRightOrder (d, done) { describe('Executor', function () { - describe.only('With persistent database', function () { + describe('With persistent database', function () { var d; beforeEach(function (done) { @@ -99,6 +109,10 @@ describe('Executor', function () { it('A throw in a callback doesnt prevent execution of next operations', function(done) { testThrowInCallback(d, done); }); + + it('Operations are executed in the right order', function(done) { + testRightOrder(d, done); + }); }); // ==== End of 'With persistent database' ==== @@ -113,7 +127,7 @@ describe('Executor', function () { d.loadDatabase(function (err) { assert.isNull(err); d.getAllData().length.should.equal(0); - return cb(); + return done(); }); }); @@ -121,6 +135,10 @@ describe('Executor', function () { testThrowInCallback(d, done); }); + it('Operations are executed in the right order', function(done) { + testRightOrder(d, done); + }); + }); // ==== End of 'With non persistent database' ==== });