Skip to content

Commit

Permalink
fix(httpclient): can't use runInBackground in agent (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and atian25 committed Sep 14, 2018
1 parent 4faf68f commit a8a3dfb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/core/dnscache_httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ class DNSCacheHttpClient extends HttpClient {
if (now - record.timestamp >= this.dnsCacheLookupInterval) {
// make sure next request don't refresh dns query
record.timestamp = now;
this.app.runInBackground(async () => {
await this[UPDATE_DNS](hostname, args);
});
this[UPDATE_DNS](hostname, args).catch(err => this.app.emit('error', err));
}

return { url: formatDnsLookupUrl(hostname, url, record.ip), args };
Expand Down
28 changes: 28 additions & 0 deletions test/lib/core/dnscache_httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,34 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => {
assert(timestamp !== record.timestamp);
});

it('should cache and update with agent', async () => {
const agent = app._agent;
mm(dns, 'lookup', async () => [ '127.0.0.1' ]);

let obj = urlparse(url + '/get_headers');
let result = await agent.curl(obj, { dataType: 'json' });
assert(result.status === 200);
assert(result.data.host === host);
let record = agent.httpclient.dnsCache.get('localhost');
const timestamp = record.timestamp;
assert(record);

obj = urlparse(url + '/get_headers');
result = await agent.curl(obj, { dataType: 'json' });
assert(result.status === 200);
assert(result.data.host === host);
record = agent.httpclient.dnsCache.get('localhost');
assert(timestamp === record.timestamp);

await sleep(5000);
obj = urlparse(url + '/get_headers');
result = await agent.curl(obj, { dataType: 'json' });
assert(result.status === 200);
assert(result.data.host === host);
record = agent.httpclient.dnsCache.get('localhost');
assert(timestamp !== record.timestamp);
});

it('should not cache ip', async () => {
const obj = urlparse(url.replace('localhost', '127.0.0.1') + '/get_headers');
const result = await app.curl(obj, { dataType: 'json' });
Expand Down

0 comments on commit a8a3dfb

Please sign in to comment.