diff --git a/lib/cmd/start.js b/lib/cmd/start.js index 7027a67..040c71e 100644 --- a/lib/cmd/start.js +++ b/lib/cmd/start.js @@ -163,7 +163,7 @@ class StartCommand extends Command { // check start status yield this.checkStatus(argv); } else { - options.stdio = options.stdio || 'inherit'; + options.stdio = [ 'inherit', 'inherit', 'inherit', 'ipc' ]; debug('Run spawn `node %s`', eggArgs.join(' ')); const child = this.child = spawn('node', eggArgs, options); child.once('exit', code => { diff --git a/package.json b/package.json index f79995c..77d9374 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "devDependencies": { "autod": "^3.0.1", + "co": "^4.6.0", "coffee": "^4.1.0", "egg": "^1.11.0", "egg-bin": "^4.3.5", diff --git a/test/fixtures/ipc-bin/start.js b/test/fixtures/ipc-bin/start.js new file mode 100644 index 0000000..5d1414e --- /dev/null +++ b/test/fixtures/ipc-bin/start.js @@ -0,0 +1,36 @@ +'use strict'; + +const co = require('co'); + +const BaseStartCommand = require('../../../lib/cmd/start'); + +class StartCommand extends BaseStartCommand { + * run(context) { + yield super.run(context); + const child = this.child; + child.on('message', msg => { + if (msg && msg.action === 'egg-ready') { + console.log('READY!!!'); + } + }); + } +} + +const start = new StartCommand(); + +co(function* () { + yield start.run({ + argv: { + framework: 'custom-framework', + _: [ process.env.BASE_DIR ], + workers: 2, + title: 'egg-server-example', + }, + cwd: process.env.BASE_DIR, + execArgv: [], + env: { + PATH: process.env.PATH, + }, + }); +}); + diff --git a/test/start.test.js b/test/start.test.js index af1984a..e318607 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -36,7 +36,7 @@ describe('test/start.test.js', () => { yield utils.cleanup(fixturePath); }); - after(function* () { + afterEach(function* () { app.proc.kill('SIGTERM'); yield utils.cleanup(fixturePath); }); @@ -57,6 +57,27 @@ describe('test/start.test.js', () => { const result = yield httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); + + it('should get ready', function* () { + app = coffee.fork(path.join(__dirname, './fixtures/ipc-bin/start.js'), [], { + env: { + BASE_DIR: fixturePath, + PATH: process.env.PATH, + }, + }); + // app.debug(); + app.expect('code', 0); + + yield sleep(waitTime); + + assert(app.stderr === ''); + assert(app.stdout.includes('READY!!!')); + assert(app.stdout.includes('--title=egg-server-example')); + assert(app.stdout.includes('"title":"egg-server-example"')); + assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); + assert(app.stdout.includes('app_worker#2:')); + assert(!app.stdout.includes('app_worker#3:')); + }); }); describe('child exit with 1', () => {