Skip to content

Commit fb7247d

Browse files
committed
test: add async test case
1 parent 70eb04f commit fb7247d

File tree

12 files changed

+119
-1
lines changed

12 files changed

+119
-1
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "eslint-config-egg"
2+
"extends": "eslint-config-egg",
3+
"parserOptions": {
4+
"ecmaVersion": 2017
5+
}
36
}

test/async/_async.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const request = require('supertest');
5+
const mm = require('egg-mock');
6+
const utils = require('../utils');
7+
8+
describe('test/async.test.js', () => {
9+
afterEach(mm.restore);
10+
let app;
11+
before(async () => {
12+
app = utils.app('apps/async-app');
13+
await app.ready();
14+
assert(app.beforeStartExectuted);
15+
assert(app.scheduleExecuted);
16+
});
17+
after(async () => {
18+
await app.close();
19+
assert(app.beforeCloseExecuted);
20+
});
21+
22+
it('middleware, controller and service support async functions', async () => {
23+
await request(app.callback())
24+
.get('/api')
25+
.expect(200)
26+
.expect([ 'service', 'controller', 'router', 'middleware' ]);
27+
});
28+
});

test/async/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const nodeVersion = Number(process.version.match(/^v(\d+\.\d)+\./)[1]);
4+
// only node >= 7.6 support async function without flags
5+
if (nodeVersion >= 7.6) {
6+
require('./_async');
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.beforeStart(async () => {
5+
await Promise.resolve();
6+
await app.runSchedule('async');
7+
app.beforeStartExectuted = true;
8+
});
9+
10+
app.beforeClose(async () => {
11+
await Promise.resolve();
12+
app.beforeCloseExecuted = true;
13+
});
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class ApiController extends app.Controller {
5+
async index() {
6+
const result = await this.service.api.getName();
7+
this.ctx.body.push(result);
8+
this.ctx.body.push('controller');
9+
}
10+
};
11+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = () => {
4+
return async (ctx, next) => {
5+
ctx.body = [];
6+
await next();
7+
ctx.body.push('middleware');
8+
};
9+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = () => {
4+
return async (ctx, next) => {
5+
ctx.body = [];
6+
await next();
7+
ctx.body.push('router');
8+
};
9+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.get('/api', app.middlewares.router(), 'api.index');
5+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
exports.schedule = {
4+
type: 'worker',
5+
interval: 1000000,
6+
};
7+
8+
exports.task = async (ctx) => {
9+
await Promise.resolve();
10+
ctx.app.scheduleExecuted = true;
11+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class ApiService extends app.Service {
5+
async getName() {
6+
await sleep(100);
7+
return 'service';
8+
}
9+
};
10+
};
11+
12+
function sleep(ms) {
13+
return new Promise(resolve => setTimeout(resolve, ms));
14+
}

0 commit comments

Comments
 (0)