Skip to content

Commit

Permalink
refactor: use logger.close, .end is deprecated (eggjs#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and fengmk2 committed Dec 29, 2016
1 parent 16b1c26 commit 583c6fa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class EggApplication extends EggCore {
*/
close() {
for (const logger of this.loggers.values()) {
logger.end();
logger.close();
}
this.messenger.close();
process.removeListener('unhandledRejection', this._unhandledRejectionHandler);
Expand Down
5 changes: 4 additions & 1 deletion test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('test/app/extend/context.test.js', () => {

it('env=local: level => debug', function* () {
mm.env('local');
mm(process.env, 'EGG_LOG', 'none');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
const logdir = app.config.logger.dir;
Expand Down Expand Up @@ -46,6 +46,7 @@ describe('test/app/extend/context.test.js', () => {

it('env=unittest: level => info', function* () {
mm.env('unittest');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
const logdir = app.config.logger.dir;
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('test/app/extend/context.test.js', () => {

it('env=prod: level => info', function* () {
mm.env('unittest');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
const logdir = app.config.logger.dir;
Expand Down Expand Up @@ -375,6 +377,7 @@ describe('test/app/extend/context.test.js', () => {
});

it('should run background task error', function* () {
mm.consoleLevel('NONE');
yield request(app.callback())
.get('/error')
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MockClient extends EventEmitter {
}

* getTimeout() {
yield sleep(5000);
yield sleep(6000);
return 'timeout';
}

Expand Down
1 change: 0 additions & 1 deletion test/lib/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('test/lib/agent.test.js', () => {
let app;
before(() => {
app = utils.cluster('apps/agent-throw');
app.debug();
return app.ready();
});
after(() => app.close());
Expand Down
63 changes: 36 additions & 27 deletions test/lib/cluster/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const mm = require('egg-mock');
const request = require('supertest');
const coffee = require('coffee');
const sleep = require('ko-sleep');
const utils = require('../../utils');


describe('test/lib/cluster/master.test.js', () => {

afterEach(mm.restore);
Expand All @@ -14,37 +16,44 @@ describe('test/lib/cluster/master.test.js', () => {
before(() => {
mm.env('default');
app = utils.cluster('apps/app-die');
app.debug();
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should restart after app worker exit', done => {
request(app.callback())
it('should restart after app worker exit', function* () {
try {
yield request(app.callback())
.get('/exit')
// wait for app worker restart
.end(() => setTimeout(() => {
// error pipe to console
app.expect('stdout', /App Worker#1:\d+ disconnect/);
app.expect('stderr', /nodejs\.AppWorkerDiedError: \[master]/);
app.expect('stderr', /App Worker#1:\d+ died/);
app.expect('stdout', /App Worker#2:\d+ started/);
.end();
} catch (_) {
// do nothing
}

// wait for app worker restart
yield sleep(5000);

done();
// this will be slow on ci env
}, 5000));
// error pipe to console
app.expect('stdout', /App Worker#1:\d+ disconnect/);
app.expect('stderr', /nodejs\.AppWorkerDiedError: \[master]/);
app.expect('stderr', /App Worker#1:\d+ died/);
app.expect('stdout', /App Worker#2:\d+ started/);
});

it('should restart when app worker throw uncaughtException', done => {
request(app.callback())
it('should restart when app worker throw uncaughtException', function* () {
try {
yield request(app.callback())
.get('/uncaughtException')
// wait for app worker restart
.end(() => setTimeout(() => {
app.expect('stderr', /\[graceful:worker:\d+:uncaughtException] throw error 1 times/);
app.expect('stdout', /App Worker#\d:\d+ started/);
done();
}, 5000));
.end();
} catch (_) {
// do nothing
}

// wait for app worker restart
yield sleep(5000);

app.expect('stderr', /\[graceful:worker:\d+:uncaughtException] throw error 1 times/);
app.expect('stdout', /App Worker#\d:\d+ started/);
});
});

Expand All @@ -54,7 +63,7 @@ describe('test/lib/cluster/master.test.js', () => {
after(() => master.close());

it('should master exit with 1', done => {
mm(process.env, 'EGG_LOG', 'none');
mm.consoleLevel('NONE');
master = utils.cluster('apps/worker-die', { coverage: true });
master.expect('code', 1).ready(done);
});
Expand All @@ -72,7 +81,8 @@ describe('test/lib/cluster/master.test.js', () => {

it('should production env stdout message include "Egg started"', done => {
mm.env('prod');
mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app/config'));
mm.consoleLevel('NONE');
mm.home(utils.getFilepath('apps/mock-production-app/config'));
app = utils.cluster('apps/mock-production-app', { coverage: true });
app.expect('stdout', /Egg started/).ready(done);
});
Expand All @@ -81,7 +91,7 @@ describe('test/lib/cluster/master.test.js', () => {
describe('--cluster', () => {
let app;
before(() => {
mm(process.env, 'EGG_LOG', 'none');
mm.consoleLevel('NONE');
app = utils.cluster('apps/cluster_mod_app', { coverage: true });
return app.ready();
});
Expand Down Expand Up @@ -116,7 +126,7 @@ describe('test/lib/cluster/master.test.js', () => {
let app;
before(() => {
mm.env('prod');
mm(process.env, 'HOME', utils.getFilepath('apps/custom-env-app'));
mm.home(utils.getFilepath('apps/custom-env-app'));
app = utils.cluster('apps/custom-env-app');
return app.ready();
});
Expand All @@ -138,7 +148,7 @@ describe('test/lib/cluster/master.test.js', () => {
before(() => {
// dependencies relation:
// aliyun-egg-app -> aliyun-egg-biz -> aliyun-egg -> egg
mm(process.env, 'HOME', utils.getFilepath('apps/aliyun-egg-app'));
mm.home(utils.getFilepath('apps/aliyun-egg-app'));
app = utils.cluster('apps/aliyun-egg-app', {
customEgg: utils.getFilepath('apps/aliyun-egg-biz'),
});
Expand Down Expand Up @@ -179,7 +189,6 @@ describe('test/lib/cluster/master.test.js', () => {

it('should start without customEgg', done => {
app = coffee.fork(utils.getFilepath('apps/master-worker-started/dispatch.js'))
.debug()
.coverage(false);

setTimeout(() => {
Expand Down

0 comments on commit 583c6fa

Please sign in to comment.