Merge pull request #174 from strongloop/fix/booted-event-asynchrony

executor: move "booted" and cb() to the next tick
This commit is contained in:
Miroslav Bajtoš 2016-02-23 12:07:42 +01:00
commit 90b9211fff
2 changed files with 10 additions and 5 deletions

View File

@ -43,7 +43,12 @@ module.exports = function execute(app, instructions, callback) {
function(done) {
enableAnonymousSwagger(app, instructions);
done();
}], function(err) {
},
// Ensure both the "booted" event and the callback are always called
// in the next tick of the even loop.
// See http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony
process.nextTick,
], function(err) {
app.booting = false;
if (err) return callback(err);

View File

@ -66,15 +66,15 @@ describe('executor', function() {
});
});
it('should emit the `booted` event', function(done) {
it('should emit the `booted` event in the next tick', function(done) {
boot.execute(app, dummyInstructions, function(err) {
expect(err).to.be.undefined();
});
app.on('booted', function() {
// This test fails with a timeout when the `booted` event has not been
// emitted correctly
done();
});
boot.execute(app, dummyInstructions, function(err) {
expect(err).to.be.undefined();
});
});
it('should work when called synchronously', function() {