Skip to content

Commit e2ae99f

Browse files
joyeecheungMylesBorins
authored andcommitted
test: mock the lookup function in parallel tests
These tests should not make any DNS calls. The lookup would fail when the DNS requests are hijacked and time out instead of erroring out. PR-URL: #17296 Refs: nodejs/help#687 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b7578bf commit e2ae99f

3 files changed

+49
-23
lines changed

test/parallel/test-http-client-req-error-dont-double-fire.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
'use strict';
2+
3+
// This tests that the error emitted on the socket does
4+
// not get fired again when the 'error' event handler throws
5+
// an error.
6+
27
const assert = require('assert');
38
const http = require('http');
49
const common = require('../common');
10+
const { addresses } = require('../common/internet');
11+
const { errorLookupMock } = require('../common/dns');
12+
13+
const host = addresses.INVALID_HOST;
514

6-
// Invalid hostname as per https://tools.ietf.org/html/rfc2606#section-2
7-
const host = 'this.hostname.is.invalid';
8-
const req = http.get({ host });
15+
const req = http.get({
16+
host,
17+
lookup: common.mustCall(errorLookupMock())
18+
});
919
const err = new Error('mock unexpected code error');
1020
req.on('error', common.mustCall(() => {
1121
throw err;
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
'use strict';
2+
3+
// This tests that the error thrown from net.createConnection
4+
// comes with host and port properties.
5+
// See https://github.com/nodejs/node-v0.x-archive/issues/7005
6+
27
const common = require('../common');
38
const net = require('net');
49
const assert = require('assert');
510

11+
const { addresses } = require('../common/internet');
12+
const {
13+
errorLookupMock,
14+
mockedErrorCode
15+
} = require('../common/dns');
16+
617
// Using port 0 as hostname used is already invalid.
7-
const c = net.createConnection(0, 'this.hostname.is.invalid');
18+
const c = net.createConnection({
19+
port: 0,
20+
host: addresses.INVALID_HOST,
21+
lookup: common.mustCall(errorLookupMock())
22+
});
823

924
c.on('connect', common.mustNotCall());
1025

1126
c.on('error', common.mustCall(function(e) {
12-
// If Name Service Switch is available on the operating system then it
13-
// might be configured differently (/etc/nsswitch.conf).
14-
// If the system is configured with no dns the error code will be EAI_AGAIN,
15-
// but if there are more services after the dns entry, for example some
16-
// linux distributions ship a myhostname service by default which would
17-
// still produce the ENOTFOUND error.
18-
assert.ok(e.code === 'ENOTFOUND' || e.code === 'EAI_AGAIN');
27+
assert.strictEqual(e.code, mockedErrorCode);
1928
assert.strictEqual(e.port, 0);
20-
assert.strictEqual(e.hostname, 'this.hostname.is.invalid');
29+
assert.strictEqual(e.hostname, addresses.INVALID_HOST);
2130
}));

test/parallel/test-net-connect-immediate-finish.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,35 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23+
24+
// This tests that if the socket is still in the 'connecting' state
25+
// when the user calls socket.end() ('finish'), the socket would emit
26+
// 'connect' and defer the handling until the 'connect' event is handled.
27+
2328
const common = require('../common');
2429
const assert = require('assert');
2530
const net = require('net');
2631

32+
const { addresses } = require('../common/internet');
33+
const {
34+
errorLookupMock,
35+
mockedErrorCode,
36+
mockedSysCall
37+
} = require('../common/dns');
38+
2739
const client = net.connect({
28-
host: 'this.hostname.is.invalid',
29-
port: common.PORT
40+
host: addresses.INVALID_HOST,
41+
port: common.PORT,
42+
lookup: common.mustCall(errorLookupMock())
3043
});
3144

3245
client.once('error', common.mustCall((err) => {
3346
assert(err);
3447
assert.strictEqual(err.code, err.errno);
35-
// If Name Service Switch is available on the operating system then it
36-
// might be configured differently (/etc/nsswitch.conf).
37-
// If the system is configured with no dns the error code will be EAI_AGAIN,
38-
// but if there are more services after the dns entry, for example some
39-
// linux distributions ship a myhostname service by default which would
40-
// still produce the ENOTFOUND error.
41-
assert.ok(err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN');
48+
assert.strictEqual(err.code, mockedErrorCode);
4249
assert.strictEqual(err.host, err.hostname);
43-
assert.strictEqual(err.host, 'this.hostname.is.invalid');
44-
assert.strictEqual(err.syscall, 'getaddrinfo');
50+
assert.strictEqual(err.host, addresses.INVALID_HOST);
51+
assert.strictEqual(err.syscall, mockedSysCall);
4552
}));
4653

4754
client.end();

0 commit comments

Comments
 (0)