-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add async test case (eggjs#339)
- Loading branch information
1 parent
c6914e0
commit e3532e4
Showing
12 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
exports.keys = 'key'; | ||
exports.middleware = [ 'async' ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "async-app" | ||
} |