Skip to content

Commit 6b74064

Browse files
benglMylesBorins
authored andcommitted
test: fix flaky test-http-dns-error
Under some conditions, the error received from getaddrinfo might actually be EAGAIN, meaning the request should be retried. Allowing for 5 retries before erroring out. Also replace one-off function with common.mustNotCall(). PR-URL: #16534 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent eb25252 commit 6b74064

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

test/parallel/test-http-dns-error.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,41 @@ const http = require('http');
3030
const https = require('https');
3131

3232
const host = '*'.repeat(256);
33+
const MAX_TRIES = 5;
3334

34-
function do_not_call() {
35-
throw new Error('This function should not have been called.');
36-
}
37-
38-
function test(mod) {
39-
35+
function tryGet(mod, tries) {
4036
// Bad host name should not throw an uncatchable exception.
4137
// Ensure that there is time to attach an error listener.
42-
const req1 = mod.get({ host: host, port: 42 }, do_not_call);
43-
req1.on('error', common.mustCall(function(err) {
38+
const req = mod.get({ host: host, port: 42 }, common.mustNotCall());
39+
req.on('error', common.mustCall(function(err) {
40+
if (err.code === 'EAGAIN' && tries < MAX_TRIES) {
41+
tryGet(mod, ++tries);
42+
return;
43+
}
4444
assert.strictEqual(err.code, 'ENOTFOUND');
4545
}));
4646
// http.get() called req1.end() for us
47+
}
4748

48-
const req2 = mod.request({
49+
function tryRequest(mod, tries) {
50+
const req = mod.request({
4951
method: 'GET',
5052
host: host,
5153
port: 42
52-
}, do_not_call);
53-
req2.on('error', common.mustCall(function(err) {
54+
}, common.mustNotCall());
55+
req.on('error', common.mustCall(function(err) {
56+
if (err.code === 'EAGAIN' && tries < MAX_TRIES) {
57+
tryRequest(mod, ++tries);
58+
return;
59+
}
5460
assert.strictEqual(err.code, 'ENOTFOUND');
5561
}));
56-
req2.end();
62+
req.end();
63+
}
64+
65+
function test(mod) {
66+
tryGet(mod, 0);
67+
tryRequest(mod, 0);
5768
}
5869

5970
if (common.hasCrypto) {

0 commit comments

Comments
 (0)