Skip to content

Commit

Permalink
feat: close return promise (#128)
Browse files Browse the repository at this point in the history
logger should close stream when app close
  • Loading branch information
popomore authored and dead-horse committed Oct 26, 2016
1 parent 03d70e1 commit 5dafb58
Show file tree
Hide file tree
Showing 28 changed files with 357 additions and 340 deletions.
5 changes: 3 additions & 2 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const url = require('url');


module.exports = {

Expand Down Expand Up @@ -36,7 +37,7 @@ module.exports = {
* @return {String} 含有域名的完整 URL
*/
urlFor(name, params) {
return this.ctx.protocol + '://' + this.ctx.host + path.join('/', this.app.router.url(name, params));
return this.ctx.protocol + '://' + this.ctx.host + url.resolve('/', this.app.router.url(name, params));
},

};
2 changes: 1 addition & 1 deletion examples/schedule/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe.skip('egg schedule example', () => {
countLine(log, 'worker&&cron').should.equal(1);
countLine(log, 'worker&&interval').should.equal(1);

app.close();
yield app.close();
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Agent extends EggApplication {

close() {
process.removeListener('uncaughtException', this._uncaughtExceptionHandler);
super.close();
return super.close();
}

}
Expand Down
7 changes: 5 additions & 2 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,18 @@ class EggApplication extends EggCore {
this.ready(() => clearTimeout(startTimeoutTimer));
}


/**
* 关闭 app 上的所有事件监听
* @public
* @return {Promise} promise
*/
close() {
super.close();
for (const logger of this.loggers.values()) {
logger.end();
}
this.messenger.close();
process.removeListener('unhandledRejection', this._unhandledRejectionHandler);
return super.close();
}
}

Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
"co": "^4.6.0",
"debug": "^2.2.0",
"delegates": "^1.0.0",
"egg-cluster": "^0.1.0",
"egg-cluster": "^1.0.0",
"egg-cookies": "^1.0.0",
"egg-core": "^0.2.1",
"egg-core": "^0.5.0",
"egg-cors": "^0.0.2",
"egg-development": "^1.0.1",
"egg-development": "^1.0.2",
"egg-i18n": "^1.0.2",
"egg-logger": "^1.2.0",
"egg-logrotator": "^2.1.0",
"egg-logger": "^1.3.0",
"egg-logrotator": "^2.2.0",
"egg-multipart": "^1.0.0",
"egg-onerror": "^0.0.3",
"egg-rest": "^1.0.1",
"egg-schedule": "^2.0.0",
"egg-onerror": "^1.0.0",
"egg-rest": "^1.1.0",
"egg-schedule": "^2.2.0",
"egg-security": "^1.2.1",
"egg-session": "^0.0.2",
"egg-static": "^0.1.0",
Expand All @@ -48,42 +48,42 @@
"scmp": "^1.0.0",
"sdk-base": "^2.0.1",
"sendmessage": "^1.0.5",
"urllib": "^2.14.0"
"urllib": "^2.17.0"
},
"devDependencies": {
"autod": "^2.7.1",
"autod-egg": "^1.0.0",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.0",
"co-sleep": "^0.0.1",
"coffee": "^3.2.5",
"coffee": "^3.3.0",
"cross-env": "^2.0.1",
"egg-alinode": "^1.0.3",
"egg-bin": "^1.3.0",
"egg-ci": "^1.0.3",
"egg-mock": "^0.0.4",
"egg-mock": "^0.0.8",
"egg-plugin-puml": "1",
"egg-view-nunjucks": "^0.5.0",
"eslint": "^3.0.0",
"eslint-config-egg": "^3.1.0",
"estraverse": "^4.1.1",
"formstream": "^1.0.0",
"glob": "^7.0.6",
"glob": "^7.1.1",
"koa": "^1.2.4",
"koa-router": "^5.4.0",
"merge-descriptors": "^1.0.1",
"moment": "^2.15.0",
"moment": "^2.15.2",
"npminstall": "^2.1.1",
"nunjucks": "^2.5.2",
"once": "^1.3.3",
"pedding": "^1.0.0",
"rds": "^0.1.0",
"rimraf": "^2.5.4",
"should": "^11.1.0",
"should": "^11.1.1",
"stream-wormhole": "^1.0.0",
"supertest": "^2.0.0",
"toa": "^2.1.0",
"toa-router": "^1.5.1"
"supertest": "^2.0.1",
"toa": "^2.3.1",
"toa-router": "^1.5.2"
},
"main": "index.js",
"files": [
Expand Down
9 changes: 5 additions & 4 deletions test/app/extend/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,28 @@ describe('test/app/extend/agent.test.js', () => {
});

describe('agent.instrument()', () => {
let app;
afterEach(() => app.close());

it.skip('should not log in unittest env', function* () {
mm.env('unittest');
const app = utils.app('apps/agent-instrument');
app = utils.app('apps/agent-instrument');
yield app.ready();
yield sleep(1000);
// TODO: why egg-agent.log not exists?
const log = fs.readFileSync(
utils.getFilepath('apps/agent-instrument/logs/agent-instrument/egg-agent.log'), 'utf8');
log.should.not.match(/\[http\] \/hello/);
app.close();
});

it('should log in local env', function* () {
mm.env('local');
const app = utils.app('apps/agent-instrument', { cache: false });
app = utils.app('apps/agent-instrument', { cache: false });
yield app.ready();
yield sleep(1000);
const log = fs.readFileSync(
utils.getFilepath('apps/agent-instrument/logs/agent-instrument/egg-agent.log'), 'utf8');
log.should.match(/\[http\] \/hello/);
app.close();
});
});
});
28 changes: 15 additions & 13 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ describe('test/app/extend/application.test.js', () => {
});

describe('app.inspect()', () => {
it('should inspect app properties', done => {
const app = utils.app('apps/demo');
app.ready(() => {
app.inspect().should.have.properties([
'name', 'baseDir',
'env', 'subdomainOffset',
'controller', 'middlewares', 'serviceClasses',
'config', 'urllib', 'loggers',
]);
app.inspect().name.should.equal('demo');
app.close();
done();
});
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());

it('should inspect app properties', () => {
app.inspect().should.have.properties([
'name', 'baseDir',
'env', 'subdomainOffset',
'controller', 'middlewares', 'serviceClasses',
'config', 'urllib', 'loggers',
]);
app.inspect().name.should.equal('demo');
});
});

Expand Down
51 changes: 42 additions & 9 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,45 +260,78 @@ describe('test/app/extend/context.test.js', () => {
});

describe('ctx.roleFailureHandler()', () => {
it('should detect ajax', function* () {
const context = yield utils.createContext({ isAjax: true });
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
afterEach(mm.restore);

it('should detect ajax', () => {
const context = app.mockContext({ isAjax: true });
context.roleFailureHandler('admin');
context.body.should.eql({ message: 'Forbidden, required role: admin', stat: 'deny' });
});

it('should response message when is not ajax', function* () {
const context = yield utils.createContext();
const context = app.mockContext();
context.roleFailureHandler('admin');
context.body.should.equal('Forbidden, required role: admin');
});
});

describe('ctx.curl()', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
afterEach(mm.restore);

it('should curl ok', function* () {
const context = yield utils.createContext();
this.timeout(10000);
const context = app.mockContext();
const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js');
res.status.should.equal(200);
});
});

describe('ctx.realStatus', () => {
it('should get from status ok', function* () {
const context = yield utils.createContext();
let app;
beforeEach(() => {
app = utils.app('apps/demo');
return app.ready();
});
afterEach(() => app.close());
afterEach(mm.restore);

it('should get from status ok', () => {
const context = app.mockContext();
context.status = 200;
context.realStatus.should.equal(200);
});

it('should get from realStatus ok', () => {
const context = utils.createContext();
const context = app.mockContext();
context.status = 302;
context.realStatus = 500;
context.realStatus.should.equal(500);
});
});

describe('ctx.state', () => {
it('should delegate ctx.locals', function* () {
const context = yield utils.createContext();
let app;
beforeEach(() => {
app = utils.app('apps/demo');
return app.ready();
});
afterEach(() => app.close());
afterEach(mm.restore);

it('should delegate ctx.locals', () => {
const context = app.mockContext();
context.locals = { a: 'a', b: 'b' };
context.state = { a: 'aa', c: 'cc' };
context.state.should.eql({ a: 'aa', b: 'b', c: 'cc' });
Expand Down
1 change: 1 addition & 0 deletions test/app/extend/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('test/app/extend/helper.test.js', () => {
app = utils.app('apps/helper');
return app.ready();
});
after(() => app.close());

describe('pathFor()', () => {
it('should get home path url', () => {
Expand Down
Loading

0 comments on commit 5dafb58

Please sign in to comment.