Skip to content

Commit

Permalink
test: make sure app close (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and popomore committed Jun 12, 2017
1 parent 1d72e37 commit 9d705e4
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"lint": "eslint app config lib test *.js",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"cov": "egg-bin cov --timeout 100000",
"ci": "npm run lint && npm run cov",
"doc-server": "doctools server",
"doc-build": "doctools build",
Expand Down
1 change: 0 additions & 1 deletion test/app/extend/application.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');

const request = require('supertest');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
Expand Down
27 changes: 12 additions & 15 deletions test/app/middleware/site_file.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const request = require('supertest');
const assert = require('assert');
const utils = require('../../utils');

Expand All @@ -10,41 +9,40 @@ describe('test/app/middleware/site_file.test.js', () => {
app = utils.app('apps/middlewares');
return app.ready();
});

after(() => app.close());

it('should GET /favicon.ico 200', () => {
return request(app.callback())
return app.httpRequest()
.get('/favicon.ico')
.expect('Content-Type', 'image/x-icon')
// .expect(res => console.log(res.headers))
.expect(200);
});

it('should GET /favicon.ico?t=123 200', () => {
return request(app.callback())
return app.httpRequest()
.get('/favicon.ico?t=123')
.expect('Content-Type', 'image/x-icon')
// .expect(res => console.log(res.headers))
.expect(200);
});

it('should 200 when accessing /robots.txt', () => {
return request(app.callback())
return app.httpRequest()
.get('/robots.txt')
.expect('User-agent: Baiduspider\nDisallow: /\n\nUser-agent: baiduspider\nDisallow: /')
.expect(200);
});

it('should 200 when accessing crossdomain.xml', () => {
return request(app.callback())
return app.httpRequest()
.get('/crossdomain.xml')
.expect('xxx')
.expect(200);
});

it('should support HEAD', () => {
return request(app.callback())
return app.httpRequest()
.head('/robots.txt')
.expect('content-length', '72')
// body must be empty for HEAD
Expand All @@ -53,26 +51,26 @@ describe('test/app/middleware/site_file.test.js', () => {
});

it('should ignore POST', () => {
return request(app.callback())
return app.httpRequest()
.post('/robots.txt')
.expect(404);
});

it('normal router should work', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect('home')
.expect(200);
});

it('not defined router should 404', () => {
return request(app.callback())
return app.httpRequest()
.get('/xxx')
.expect(404);
});

it('should 404 when accessing fake.txt using wrong config', () => {
return request(app.callback())
return app.httpRequest()
.get('/fake.txt')
.expect(404);
});
Expand All @@ -83,14 +81,13 @@ describe('test/app/middleware/site_file.test.js', () => {
app = utils.app('apps/favicon');
return app.ready();
});

after(() => app.close());

it('should redirect https://eggjs.org/favicon.ico', () => {
return request(app.callback())
return app.httpRequest()
.get('/favicon.ico')
.expect(302, (err, res) => {
assert(!err);
.expect(302)
.expect(res => {
assert(!res.headers['set-cookie']);
assert(res.headers.location === 'https://eggjs.org/favicon.ico');
});
Expand Down
13 changes: 5 additions & 8 deletions test/lib/cluster/app_worker.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
'use strict';

const request = require('supertest');
const utils = require('../../utils');

describe('test/lib/cluster/app_worker.test.js', () => {
let app;
before(done => {
app = utils.cluster('apps/app-server', {
coverage: true,
});
app.ready(done);
before(() => {
app = utils.cluster('apps/app-server');
app.coverage(true);
return app.ready();
});

after(() => app.close());

it('should start cluster success and app worker emit `server` event', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect('true');
});
Expand Down
10 changes: 4 additions & 6 deletions test/lib/cluster/cluster-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

const mm = require('egg-mock');
const assert = require('assert');
const utils = require('../../utils');
const request = require('supertest');
const innerClient = require('cluster-client/lib/symbol').innerClient;
const utils = require('../../utils');

describe('test/lib/cluster/cluster-client.test.js', () => {
let app;
before(function* () {
mm.consoleLevel('NONE');
app = utils.app('apps/cluster_mod_app', { coverage: true });
app = utils.app('apps/cluster_mod_app');
yield app.ready();
});
after(function* () {
Expand All @@ -21,7 +20,7 @@ describe('test/lib/cluster/cluster-client.test.js', () => {
});

it('should publish & subscribe ok', () => {
return request(app.callback())
return app.httpRequest()
.post('/publish')
.send({ value: '30.20.78.299' })
.expect('ok')
Expand All @@ -32,11 +31,10 @@ describe('test/lib/cluster/cluster-client.test.js', () => {
});
})
.then(() => {
return request(app.callback())
return app.httpRequest()
.get('/getHosts')
.expect('30.20.78.299:20880')
.expect(200);
});
});

});
56 changes: 32 additions & 24 deletions test/lib/cluster/master.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';

const mm = require('egg-mock');
const request = require('supertest');
const coffee = require('coffee');
const sleep = require('mz-modules/sleep');
const utils = require('../../utils');

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

afterEach(mm.restore);

describe('app worker die', () => {
Expand All @@ -22,7 +20,7 @@ describe('test/lib/cluster/master.test.js', () => {

it('should restart after app worker exit', function* () {
try {
yield request(app.callback())
yield app.httpRequest()
.get('/exit');
} catch (_) {
// do nothing
Expand All @@ -40,7 +38,7 @@ describe('test/lib/cluster/master.test.js', () => {

it('should restart when app worker throw uncaughtException', function* () {
try {
yield request(app.callback())
yield app.httpRequest()
.get('/uncaughtException');
} catch (_) {
// do nothing
Expand All @@ -61,7 +59,8 @@ describe('test/lib/cluster/master.test.js', () => {

it('should master exit with 1', done => {
mm.consoleLevel('NONE');
master = utils.cluster('apps/worker-die', { coverage: true });
master = utils.cluster('apps/worker-die');
master.coverage(false);
master.expect('code', 1).ready(done);
});
});
Expand All @@ -72,15 +71,17 @@ describe('test/lib/cluster/master.test.js', () => {
afterEach(() => app.close());

it('should dev env stdout message include "Egg started"', done => {
app = utils.cluster('apps/master-worker-started', { coverage: true });
app = utils.cluster('apps/master-worker-started');
app.coverage(false);
app.expect('stdout', /Egg started/).ready(done);
});

it('should production env stdout message include "Egg started"', done => {
mm.env('prod');
mm.consoleLevel('NONE');
mm.home(utils.getFilepath('apps/mock-production-app/config'));
app = utils.cluster('apps/mock-production-app', { coverage: true });
app = utils.cluster('apps/mock-production-app');
app.coverage(true);
app.expect('stdout', /Egg started/).ready(done);
});
});
Expand All @@ -89,20 +90,21 @@ describe('test/lib/cluster/master.test.js', () => {
let app;
before(() => {
mm.consoleLevel('NONE');
app = utils.cluster('apps/cluster_mod_app', { coverage: true });
app = utils.cluster('apps/cluster_mod_app');
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should online cluster mode startup success', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect('hi cluster')
.expect(200);
});

it('should assign a free port by master', () => {
return request(app.callback())
return app.httpRequest()
.get('/clusterPort')
.expect(/\d+/)
.expect(200);
Expand All @@ -112,13 +114,14 @@ describe('test/lib/cluster/master.test.js', () => {
describe('--dev', () => {
let app;
before(() => {
app = utils.cluster('apps/cluster_mod_app', { coverage: true });
app = utils.cluster('apps/cluster_mod_app');
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should dev cluster mode startup success', () => {
return request(app)
return app.httpRequest()
.get('/')
.expect('hi cluster')
.expect(200);
Expand All @@ -130,41 +133,45 @@ describe('test/lib/cluster/master.test.js', () => {
let app2;
before(function* () {
mm.consoleLevel('NONE');
app1 = utils.cluster('apps/cluster_mod_app', { coverage: true });
app2 = utils.cluster('apps/cluster_mod_app', { coverage: true });
app1 = utils.cluster('apps/cluster_mod_app');
app1.coverage(false);
app2 = utils.cluster('apps/cluster_mod_app');
app2.coverage(false);
yield [
app1.ready(),
app2.ready(),
];
});
after(() => {
app1.close();
app2.close();
after(function* () {
yield [
app1.close(),
app2.close(),
];
});

it('should online cluster mode startup success, app1', () => {
return request(app1.callback())
return app1.httpRequest()
.get('/')
.expect('hi cluster')
.expect(200);
});

it('should assign a free port by master, app1', () => {
return request(app1.callback())
return app1.httpRequest()
.get('/clusterPort')
.expect(/\d+/)
.expect(200);
});

it('should online cluster mode startup success, app2', () => {
return request(app2.callback())
return app2.httpRequest()
.get('/')
.expect('hi cluster')
.expect(200);
});

it('should assign a free port by master, app2', () => {
return request(app2.callback())
return app2.httpRequest()
.get('/clusterPort')
.expect(/\d+/)
.expect(200);
Expand All @@ -178,12 +185,13 @@ describe('test/lib/cluster/master.test.js', () => {
mm.env('prod');
mm.home(utils.getFilepath('apps/custom-env-app'));
app = utils.cluster('apps/custom-env-app');
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should start with prod env', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect({
env: 'prod',
Expand All @@ -202,12 +210,13 @@ describe('test/lib/cluster/master.test.js', () => {
app = utils.cluster('apps/aliyun-egg-app', {
customEgg: utils.getFilepath('apps/aliyun-egg-biz'),
});
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should start success', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect({
'aliyun-egg-core': true,
Expand Down Expand Up @@ -249,5 +258,4 @@ describe('test/lib/cluster/master.test.js', () => {
}, 10000);
});
});

});
Loading

0 comments on commit 9d705e4

Please sign in to comment.