From c9a1e0cc691e2c4e1fd80840bc0fb02a1475781c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 16 Aug 2017 20:32:06 +0200 Subject: [PATCH] Do not call callbacks twice in async boot scripts See #252 for details --- lib/executor.js | 4 ++-- test/executor.test.js | 17 +++++++++++++++++ .../simple-app/boot/promise-callback.js | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/simple-app/boot/promise-callback.js diff --git a/lib/executor.js b/lib/executor.js index 0351aa6..052f714 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -306,11 +306,11 @@ function runScripts(app, list, callback) { async.eachSeries(functions, function(f, done) { debug('Running script %s', f.path); var cb = function(err) { - debug('Async function finished %s', f.path); + debug('Async function %s %s', err ? 'failed' : 'finished', f.path); done(err); // Make sure done() isn't called twice, e.g. if a script returns a // thenable object and also calls the passed callback. - cb = null; + cb = function() {}; }; try { var result = f.func(app, cb); diff --git a/test/executor.test.js b/test/executor.test.js index 1123d3a..40646a2 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -373,6 +373,23 @@ describe('executor', function() { }); }); + describe('with boot script returning a promise and calling callback', + function() { + before(function() { + process.promiseAndCallback = true; + }); + + after(function() { + delete process.promiseAndCallback; + }); + + it('should only call the callback once', function(done) { + // Note: Mocha will fail this test if done() is called twice + boot.execute(app, simpleAppInstructions(), done); + }); + } + ); + describe('for mixins', function() { var options; beforeEach(function() { diff --git a/test/fixtures/simple-app/boot/promise-callback.js b/test/fixtures/simple-app/boot/promise-callback.js new file mode 100644 index 0000000..4618a06 --- /dev/null +++ b/test/fixtures/simple-app/boot/promise-callback.js @@ -0,0 +1,13 @@ +// Copyright IBM Corp. 2017. All Rights Reserved. +// Node module: loopback-boot +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +var Promise = require('bluebird'); + +module.exports = function(app, callback) { + callback(); + if (process.promiseAndCallback) { + return Promise.reject(); + } +};