Skip to content

Commit

Permalink
test: make dnscache test case more stable (#4297)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed May 13, 2020
1 parent 64efd07 commit 427a30a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"findlinks": "^2.1.0",
"formstream": "^1.1.0",
"glob": "^7.1.3",
"koa": "^2.11.0",
"koa-static": "^3.0.0",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
Expand Down
4 changes: 2 additions & 2 deletions test/lib/core/dnscache_httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => {
record = app.httpclient.dnsCache.get('localhost');
assert(timestamp === record.timestamp);

await sleep(5000);
await sleep(5500);
obj = urlparse(url + '/get_headers');
result = await app.curl(obj, { dataType: 'json' });
assert(result.status === 200);
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => {
record = agent.httpclient.dnsCache.get('localhost');
assert(timestamp === record.timestamp);

await sleep(5000);
await sleep(5500);
obj = urlparse(url + '/get_headers');
result = await agent.curl(obj, { dataType: 'json' });
assert(result.status === 200);
Expand Down
66 changes: 35 additions & 31 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const fs = require('fs');
const path = require('path');
const http = require('http');
const mm = require('egg-mock');
const sleep = require('mz-modules/sleep');
const Koa = require('koa');
const http = require('http');
const fixtures = path.join(__dirname, 'fixtures');
const eggPath = path.join(__dirname, '..');
const egg = require('..');
Expand Down Expand Up @@ -51,39 +53,41 @@ exports.startLocalServer = () => {
return resolve('http://127.0.0.1:' + localServer.address().port);
}
let retry = false;
localServer = http.createServer((req, res) => {
req.resume();
req.on('end', () => {
res.statusCode = 200;
if (req.url === '/get_headers') {
res.setHeader('Content-Type', 'json');
res.end(JSON.stringify(req.headers));
} else if (req.url === '/timeout') {
setTimeout(() => {
res.end(`${req.method} ${req.url}`);
}, 10000);
return;
} else if (req.url === '/error') {
res.statusCode = 500;
res.end('this is an error');
return;
} else if (req.url === '/retry') {
if (!retry) {
retry = true;
res.statusCode = 500;
res.end();
} else {
res.setHeader('x-retry', '1');
res.statusCode = 200;
res.end('retry suc');
retry = false;
}
return;

const app = new Koa();
app.use(async ctx => {
if (ctx.path === '/get_headers') {
ctx.body = ctx.request.headers;
return;
}

if (ctx.path === '/timeout') {
await sleep(10000);
ctx.body = `${ctx.method} ${ctx.path}`;
return;
}

if (ctx.path === '/error') {
ctx.status = 500;
ctx.body = 'this is an error';
return;
}

if (ctx.path === '/retry') {
if (!retry) {
retry = true;
ctx.status = 500;
} else {
res.end(`${req.method} ${req.url}`);
ctx.set('x-retry', '1');
ctx.body = 'retry suc';
retry = false;
}
});
return;
}

ctx.body = `${ctx.method} ${ctx.path}`;
});
localServer = http.createServer(app.callback());

localServer.listen(0, err => {
if (err) return reject(err);
Expand Down

0 comments on commit 427a30a

Please sign in to comment.