From 431e93dbe37f5d39e8cf11d88ad1c1e8368f479c Mon Sep 17 00:00:00 2001 From: Jeffrey Xiao Date: Wed, 19 Jul 2017 11:23:35 -0700 Subject: [PATCH 1/3] Expose shutDownSequence for better end-to-end testing --- src/server.js | 5 ++++- src/worker.js | 11 +++++++++-- test/server-test.js | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/server.js b/src/server.js index e2ebe1b..8adeb2b 100644 --- a/src/server.js +++ b/src/server.js @@ -31,7 +31,10 @@ export default function hypernova(userConfig, onServer) { throw new TypeError('Hypernova requires a `getComponent` property and it must be a function'); } - logger.init(config.logger); + // don't initialize logger if it has already been initialized + if (logger.init) { + logger.init(config.logger); + } const app = express(); diff --git a/src/worker.js b/src/worker.js index c00271a..3320a60 100644 --- a/src/worker.js +++ b/src/worker.js @@ -16,9 +16,9 @@ const attachEndpoint = (app, config, callback) => { app.post(config.endpoint, renderBatch(config, callback)); }; + const initServer = (app, config, callback) => { let server; - function exit(code) { return () => process.exit(code); } @@ -54,6 +54,10 @@ const initServer = (app, config, callback) => { .catch(exit(code)); } + function readyToClose(getClose) { + getClose(shutDownSequence); + } + function errorHandler(err, req, res, next) { // eslint-disable-line no-unused-vars // If there is an error with body-parser and the status is set then we can safely swallow // the error and report it. @@ -91,7 +95,10 @@ const initServer = (app, config, callback) => { // run through the initialize methods of any plugins that define them runAppLifecycle('initialize', config.plugins, config) .then(() => { - server = app.listen(config.port, config.host, callback); + server = app.listen(config.port, config.host, () => { + callback(); + readyToClose(config.getClose); + }); return null; }) .catch(shutDownSequence); diff --git a/test/server-test.js b/test/server-test.js index 2fcc8ae..c840c9b 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -7,7 +7,37 @@ describe('Hypernova server', () => { assert.throws(hypernova, TypeError); }); - it('starts up the hypernova server without blowing up', () => { - hypernova({ devMode: true, getComponent: () => {} }); + it('starts up the hypernova server without blowing up', (done) => { + hypernova({ + devMode: true, + getComponent: () => {}, + plugins: [ + { + shutDown: () => { + done(); + }, + }, + ], + getClose: (close) => { + close(null, null, 0); + }, + }); + }); + + it('starts up the hypernova server again without blowing up', (done) => { + hypernova({ + devMode: true, + getComponent: () => {}, + plugins: [ + { + shutDown: () => { + done(); + }, + }, + ], + getClose: (close) => { + close(null, null, 0); + }, + }); }); }); From b8abdb75b465ec07f37f6584af309e1feeaff84b Mon Sep 17 00:00:00 2001 From: Jeffrey Xiao Date: Wed, 19 Jul 2017 11:36:25 -0700 Subject: [PATCH 2/3] Fix server-test --- test/server-test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/server-test.js b/test/server-test.js index c840c9b..8f05f1b 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -1,8 +1,20 @@ import { assert } from 'chai'; +import sinon from 'sinon'; import hypernova from '../server'; describe('Hypernova server', () => { + let sandbox; + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + sandbox.stub(process, 'exit'); + }); + + afterEach(() => { + sandbox.restore(); + }); + it('blows up if hypernova does not get getComponent', () => { assert.throws(hypernova, TypeError); }); From a30f27c877e14e34cd89e0a8a1a0c750e3bdb3bd Mon Sep 17 00:00:00 2001 From: Jeffrey Xiao Date: Wed, 19 Jul 2017 12:44:58 -0700 Subject: [PATCH 3/3] Add default getClose and update README --- README.md | 4 +++- src/server.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e1c0e56..134d05e 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,9 @@ Options, and their defaults // the port the app will start on port: 8080, // default endpoint path - endpoint: '/batch' + endpoint: '/batch', + // default close callback + getClose: () => {}, } ``` diff --git a/src/server.js b/src/server.js index 8adeb2b..ca73098 100644 --- a/src/server.js +++ b/src/server.js @@ -22,6 +22,7 @@ const defaultConfig = { plugins: [], port: 8080, host: '0.0.0.0', + getClose: () => {}, }; export default function hypernova(userConfig, onServer) {