Skip to content

Commit

Permalink
test: add async test case (eggjs#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Feb 9, 2017
1 parent c6914e0 commit e3532e4
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": "eslint-config-egg",
"parserOptions": {
"ecmaVersion": 2017
}
}
28 changes: 28 additions & 0 deletions test/async/_async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const assert = require('assert');
const request = require('supertest');
const mm = require('egg-mock');
const utils = require('../utils');

describe('test/async.test.js', () => {
afterEach(mm.restore);
let app;
before(async () => {
app = utils.app('apps/async-app');
await app.ready();
assert(app.beforeStartExectuted);
assert(app.scheduleExecuted);
});
after(async () => {
await app.close();
assert(app.beforeCloseExecuted);
});

it('middleware, controller and service support async functions', async () => {
await request(app.callback())
.get('/api')
.expect(200)
.expect([ 'service', 'controller', 'router', 'middleware' ]);
});
});
7 changes: 7 additions & 0 deletions test/async/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const nodeVersion = Number(process.version.match(/^v(\d+\.\d)+\./)[1]);
// only node >= 7.6 support async function without flags
if (nodeVersion >= 7.6) {
require('./_async');
}
14 changes: 14 additions & 0 deletions test/fixtures/apps/async-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = app => {
app.beforeStart(async () => {
await Promise.resolve();
await app.runSchedule('async');
app.beforeStartExectuted = true;
});

app.beforeClose(async () => {
await Promise.resolve();
app.beforeCloseExecuted = true;
});
};
11 changes: 11 additions & 0 deletions test/fixtures/apps/async-app/app/controller/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
return class ApiController extends app.Controller {
async index() {
const result = await this.service.api.getName();
this.ctx.body.push(result);
this.ctx.body.push('controller');
}
};
};
9 changes: 9 additions & 0 deletions test/fixtures/apps/async-app/app/middleware/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = () => {
return async (ctx, next) => {
ctx.body = [];
await next();
ctx.body.push('middleware');
};
};
9 changes: 9 additions & 0 deletions test/fixtures/apps/async-app/app/middleware/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = () => {
return async (ctx, next) => {
ctx.body = [];
await next();
ctx.body.push('router');
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/async-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.get('/api', app.middlewares.router(), 'api.index');
};
11 changes: 11 additions & 0 deletions test/fixtures/apps/async-app/app/schedule/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

exports.schedule = {
type: 'worker',
interval: 1000000,
};

exports.task = async (ctx) => {
await Promise.resolve();
ctx.app.scheduleExecuted = true;
};
14 changes: 14 additions & 0 deletions test/fixtures/apps/async-app/app/service/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = app => {
return class ApiService extends app.Service {
async getName() {
await sleep(100);
return 'service';
}
};
};

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
4 changes: 4 additions & 0 deletions test/fixtures/apps/async-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

exports.keys = 'key';
exports.middleware = [ 'async' ];
3 changes: 3 additions & 0 deletions test/fixtures/apps/async-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "async-app"
}

0 comments on commit e3532e4

Please sign in to comment.