Skip to content

Commit

Permalink
test: use async function instead of generator function (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Nov 20, 2017
1 parent 5513456 commit 12edd64
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 306 deletions.
7 changes: 3 additions & 4 deletions test/app/extend/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ describe('test/app/extend/agent.test.js', () => {
});
after(() => app.close());

it('should add singleton success', function* () {
let config = yield app.agent.dataService.get('second').getConfig();
it('should add singleton success', async () => {
let config = await app.agent.dataService.get('second').getConfig();
assert(config.foo === 'bar');
assert(config.foo2 === 'bar2');

const ds = app.agent.dataService.createInstance({ foo: 'barrr' });
config = yield ds.getConfig();
config = await ds.getConfig();
assert(config.foo === 'barrr');
});
});

});
18 changes: 9 additions & 9 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ describe('test/app/extend/application.test.js', () => {
let app;
after(() => app.close());

it('should log info when plugin is not ready', function* () {
it('should log info when plugin is not ready', async () => {
app = utils.cluster('apps/notready');
// it won't be ready, so wait for the timeout
yield sleep(11000);
await sleep(11000);

app.expect('stderr', /\[egg:core:ready_timeout] 10 seconds later a was still unable to finish./);
});
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('test/app/extend/application.test.js', () => {
});
after(() => app.close());

it('should get anonymous context object', function* () {
it('should get anonymous context object', async () => {
const ctx = app.createAnonymousContext({
socket: {
remoteAddress: '10.0.0.1',
Expand All @@ -138,13 +138,13 @@ describe('test/app/extend/application.test.js', () => {
});
after(() => app.close());

it('should add singleton success', function* () {
let config = yield app.dataService.get('first').getConfig();
it('should add singleton success', async () => {
let config = await app.dataService.get('first').getConfig();
assert(config.foo === 'bar');
assert(config.foo1 === 'bar1');

const ds = app.dataService.createInstance({ foo: 'barrr' });
config = yield ds.getConfig();
config = await ds.getConfig();
assert(config.foo === 'barrr');
});
});
Expand All @@ -157,12 +157,12 @@ describe('test/app/extend/application.test.js', () => {
});
after(() => app.close());

it('should run background task success', function* () {
yield app.httpRequest()
it('should run background task success', async () => {
await app.httpRequest()
.get('/app_background')
.expect(200)
.expect('hello app');
yield sleep(5000);
await sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/mock background run at app result file size: \d+/.test(log));
Expand Down
50 changes: 25 additions & 25 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ describe('test/app/extend/context.test.js', () => {
let app;
afterEach(() => app.close());

it('env=local: level => info', function* () {
it('env=local: level => info', async () => {
mm.env('local');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
await app.ready();
const logdir = app.config.logger.dir;

yield app.httpRequest()
await app.httpRequest()
.get('/logger?message=foo')
.expect('logger');

yield sleep(5000);
await sleep(5000);

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
assert(errorContent.includes('nodejs.Error: error foo'));
Expand All @@ -42,22 +42,22 @@ describe('test/app/extend/context.test.js', () => {
assert(coreLoggerContent.includes('core warn foo'));
});

it('env=unittest: level => info', function* () {
it('env=unittest: level => info', async () => {
mm.env('unittest');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
await app.ready();
const logdir = app.config.logger.dir;

app.mockContext({
userId: '123123',
});

yield app.httpRequest()
await app.httpRequest()
.get('/logger?message=foo')
.expect('logger');

yield sleep(5000);
await sleep(5000);

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
assert(errorContent.includes('nodejs.Error: error foo'));
Expand All @@ -75,18 +75,18 @@ describe('test/app/extend/context.test.js', () => {
assert(coreLoggerContent.includes('core warn foo'));
});

it('env=prod: level => info', function* () {
it('env=prod: level => info', async () => {
mm.env('unittest');
mm.consoleLevel('NONE');
app = utils.app('apps/demo', { cache: false });
yield app.ready();
await app.ready();
const logdir = app.config.logger.dir;

yield app.httpRequest()
await app.httpRequest()
.get('/logger?message=foo')
.expect('logger');

yield sleep(5000);
await sleep(5000);

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
assert(errorContent.includes('nodejs.Error: error foo'));
Expand Down Expand Up @@ -118,12 +118,12 @@ describe('test/app/extend/context.test.js', () => {
.expect('null');
});

it('should log with padding message', function* () {
yield app.httpRequest()
it('should log with padding message', async () => {
await app.httpRequest()
.get('/logger')
.expect(200);

yield sleep(100);
await sleep(100);
const logPath = utils.getFilepath('apps/get-logger/logs/get-logger/a.log');
assert(
/\[-\/127.0.0.1\/-\/\d+ms GET \/logger] aaa/.test(fs.readFileSync(logPath, 'utf8'))
Expand Down Expand Up @@ -232,12 +232,12 @@ describe('test/app/extend/context.test.js', () => {
});
after(() => app.close());

it('should run background task success', function* () {
yield app.httpRequest()
it('should run background task success', async () => {
await app.httpRequest()
.get('/')
.expect(200)
.expect('hello');
yield sleep(5000);
await sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/background run result file size: \d+/.test(log));
Expand All @@ -250,13 +250,13 @@ describe('test/app/extend/context.test.js', () => {
);
});

it('should run background task error', function* () {
it('should run background task error', async () => {
mm.consoleLevel('NONE');
yield app.httpRequest()
await app.httpRequest()
.get('/error')
.expect(200)
.expect('hello error');
yield sleep(5000);
await sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
assert(/ENOENT: no such file or directory/.test(log));
Expand All @@ -275,10 +275,10 @@ describe('test/app/extend/context.test.js', () => {
after(() => app.close());

describe('ctx.curl()', () => {
it('should curl ok', function* () {
const localServer = yield utils.startLocalServer();
it('should curl ok', async () => {
const localServer = await utils.startLocalServer();
const context = app.mockContext();
const res = yield context.curl(`${localServer}/foo/bar`);
const res = await context.curl(`${localServer}/foo/bar`);
assert(res.status === 200);
});

Expand All @@ -290,7 +290,7 @@ describe('test/app/extend/context.test.js', () => {
});

describe('ctx.httpclient', () => {
it('should only one httpclient on one ctx', function* () {
it('should only one httpclient on one ctx', async () => {
const ctx = app.mockContext();
const httpclient = ctx.httpclient;
assert(ctx.httpclient === httpclient);
Expand Down
1 change: 0 additions & 1 deletion test/app/extend/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('test/app/extend/helper.test.js', () => {
.expect(/^http:\/\/127\.0\.0\.1:\d+\/home\?foo=1$/)
.expect(200);
});

});

describe('escape()', () => {
Expand Down
32 changes: 15 additions & 17 deletions test/app/extend/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ describe('test/app/extend/request.test.js', () => {
afterEach(mm.restore);

describe('req.host', () => {
it('should return host with port', function* () {
it('should return host with port', () => {
mm(req.header, 'host', 'foo.com:3000');
assert(req.hostname === 'foo.com');
});

it('should return "" when no host present', function* () {
it('should return "" when no host present', () => {
assert(typeof req.host === 'string');
assert(req.host === '');
});

it('should return host from X-Forwarded-Host header', function* () {
it('should return host from X-Forwarded-Host header', () => {
mm(req.header, 'x-forwarded-host', 'foo.com');
assert(typeof req.host === 'string');
assert(req.host === 'foo.com');
});

it('should return host from Host header when proxy=false', function* () {
it('should return host from Host header when proxy=false', () => {
mm(app.config, 'proxy', false);
mm(req.header, 'x-forwarded-host', 'foo.com');
mm(req.header, 'host', 'bar.com');
Expand All @@ -48,45 +48,45 @@ describe('test/app/extend/request.test.js', () => {
});

describe('req.hostname', () => {
it('should return hostname with port', function* () {
it('should return hostname with port', () => {
mm(req.header, 'host', 'foo.com:3000');
assert(req.hostname === 'foo.com');
});

it('should return "" when no host present', function* () {
it('should return "" when no host present', () => {
assert(typeof req.hostname === 'string');
assert(req.hostname === '');
});
});

describe('req.ip', () => {
it('should return ipv4', function* () {
it('should return ipv4', () => {
mm(req.socket, 'remoteAddress', '::ffff:127.0.0.1');
assert(req.ip === '127.0.0.1');
assert(req.ip === '127.0.0.1');
});
});

describe('req.ips', () => {
it('should used x-forwarded-for', function* () {
it('should used x-forwarded-for', () => {
mm(req.header, 'x-forwarded-for', '127.0.0.1,127.0.0.2');
assert.deepEqual(req.ips, [ '127.0.0.1', '127.0.0.2' ]);
});

it('should used x-real-ip', function* () {
it('should used x-real-ip', () => {
mm(app.config, 'ipHeaders', 'X-Forwarded-For, X-Real-IP');
mm(req.header, 'x-forwarded-for', '');
mm(req.header, 'x-real-ip', '127.0.0.1,127.0.0.2');
assert.deepEqual(req.ips, [ '127.0.0.1', '127.0.0.2' ]);
});

it('should return []', function* () {
it('should return []', () => {
mm(req.header, 'x-forwarded-for', '');
mm(req.header, 'x-real-ip', '');
assert.deepEqual(req.ips, []);
});

it('should return [] when proxy=false', function* () {
it('should return [] when proxy=false', () => {
mm(app.config, 'proxy', false);
mm(req.header, 'x-forwarded-for', '127.0.0.1,127.0.0.2');
assert.deepEqual(req.ips, []);
Expand Down Expand Up @@ -276,12 +276,12 @@ describe('test/app/extend/request.test.js', () => {
});

describe('request.acceptJSON', () => {
it('should true when url path ends with .json', function* () {
it('should true when url path ends with .json', async () => {
mm(req, 'path', 'hello.json');
assert(req.acceptJSON === true);
});

it('should true when response is json', function* () {
it('should true when response is json', async () => {
const context = app.mockContext({
headers: {
accept: 'text/html',
Expand All @@ -292,7 +292,7 @@ describe('test/app/extend/request.test.js', () => {
assert(context.request.acceptJSON === true);
});

it('should true when accept json', function* () {
it('should true when accept json', async () => {
const context = app.mockContext({
headers: {
accept: 'application/json',
Expand All @@ -302,7 +302,7 @@ describe('test/app/extend/request.test.js', () => {
assert(context.request.acceptJSON === true);
});

it('should false when do not accept json', function* () {
it('should false when do not accept json', async () => {
const context = app.mockContext({
headers: {
accept: 'text/html',
Expand All @@ -313,7 +313,6 @@ describe('test/app/extend/request.test.js', () => {
assert(request.acceptJSON === false);
});
});

});

describe('work with egg app', () => {
Expand Down Expand Up @@ -355,5 +354,4 @@ describe('test/app/extend/request.test.js', () => {
});
});
});

});
Loading

0 comments on commit 12edd64

Please sign in to comment.