Merge pull request #286 from strongloop/fix/booting-flag

fix: set `app.booting` flag immediately
This commit is contained in:
Miroslav Bajtoš 2019-03-22 16:34:04 +01:00 committed by GitHub
commit e73b90ed97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 2 deletions

9
lib/bootstrapper.js vendored
View File

@ -202,11 +202,20 @@ Bootstrapper.prototype.run = function(context, done) {
context = context || {}; context = context || {};
var phases = context.phases || this.phases; var phases = context.phases || this.phases;
if (phases.includes('starting') || phases.includes('start')) {
context.app.booting = true;
}
var bootPlugins = this.getExtensions('/boot'); var bootPlugins = this.getExtensions('/boot');
async.eachSeries(phases, function(phase, done) { async.eachSeries(phases, function(phase, done) {
debug('Phase %s', phase); debug('Phase %s', phase);
async.eachSeries(bootPlugins, pluginIteratorFactory(context, phase), done); async.eachSeries(bootPlugins, pluginIteratorFactory(context, phase), done);
}, function(err) { }, function(err) {
if (phases.includes('started')) {
context.app.booting = false;
}
return done(err, context); return done(err, context);
}); });
return done.promise; return done.promise;

View File

@ -116,7 +116,6 @@ function applyAppConfig(app, appConfig) {
Application.prototype.starting = function(context) { Application.prototype.starting = function(context) {
var app = context.app; var app = context.app;
app.booting = true;
assertLoopBackVersion(app); assertLoopBackVersion(app);
var appConfig = context.instructions.application; var appConfig = context.instructions.application;
@ -129,7 +128,6 @@ Application.prototype.starting = function(context) {
Application.prototype.started = function(context, done) { Application.prototype.started = function(context, done) {
var app = context.app; var app = context.app;
app.booting = false;
process.nextTick(function() { process.nextTick(function() {
app.emit('booted'); app.emit('booted');
done(); done();

40
test/acceptance.test.js Normal file
View File

@ -0,0 +1,40 @@
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var path = require('path');
var loopback = require('loopback');
var chai = require('chai');
var dirtyChai = require('dirty-chai');
var expect = chai.expect;
chai.use(dirtyChai);
const bootLoopBackApp = require('..');
describe('bootLoopBackApp', function() {
var app;
beforeEach(function() {
app = loopback();
});
it('sets app.booting immediately', function() {
const appDir = path.join(__dirname, './fixtures/empty-app');
// Start the bootstrapper
const promise = bootLoopBackApp(app, appDir);
// Still in the original turn of the event loop,
// verify that the app is signalling "boot in progress"
expect(app.booting).to.equal(true);
// Wait for bootstrapper to finish
return promise.then(() => {
// Verify that app is signalling "boot has finished"
expect(app.booting).to.equal(false);
});
});
});

2
test/fixtures/empty-app/config.json vendored Normal file
View File

@ -0,0 +1,2 @@
{
}