Skip to content

Commit aa8f6f8

Browse files
committed
net: improved test
1 parent 7546326 commit aa8f6f8

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

test/internet/test-net-autoselectfamily-events.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,86 @@
11
'use strict';
22

33
const common = require('../common');
4-
const { addresses: { INET4_IP } } = require('../common/internet');
4+
const { addresses: { INET6_IP, INET4_IP } } = require('../common/internet');
55
const { createMockedLookup } = require('../common/dns');
66

77
const assert = require('assert');
88
const { createConnection } = require('net');
99

10+
// Test that all events are emitted when trying a single IP (which means autoselectfamily is bypassed)
11+
{
12+
const connection = createConnection({
13+
host: 'example.org',
14+
port: 10,
15+
lookup: createMockedLookup(INET4_IP),
16+
autoSelectFamily: true,
17+
autoSelectFamilyAttemptTimeout: 10,
18+
});
19+
20+
connection.on('connectionAttempt', common.mustCall((address, port, family) => {
21+
assert.strictEqual(address, INET4_IP);
22+
assert.strictEqual(port, 10);
23+
assert.strictEqual(family, 4);
24+
}));
25+
26+
connection.on('connectionAttemptFailed', common.mustCall((address, port, family, error) => {
27+
assert.strictEqual(address, INET4_IP);
28+
assert.strictEqual(port, 10);
29+
assert.strictEqual(family, 4);
30+
31+
assert.ok(error.code.match(/ECONNREFUSED|EHOSTUNREACH|ETIMEDOUT/));
32+
}));
33+
34+
connection.on('ready', common.mustNotCall());
35+
connection.on('error', common.mustCall());
36+
}
37+
38+
// Test that all events are emitted when trying multiple IPs
39+
{
40+
const connection = createConnection({
41+
host: 'example.org',
42+
port: 10,
43+
lookup: createMockedLookup(INET6_IP, INET4_IP),
44+
autoSelectFamily: true,
45+
autoSelectFamilyAttemptTimeout: 10,
46+
});
47+
48+
const addresses = [
49+
{ address: INET6_IP, port: 10, family: 6 },
50+
{ address: INET6_IP, port: 10, family: 6 },
51+
{ address: INET4_IP, port: 10, family: 4 },
52+
{ address: INET4_IP, port: 10, family: 4 },
53+
];
54+
55+
connection.on('connectionAttempt', common.mustCall((address, port, family) => {
56+
const expected = addresses.shift();
57+
58+
assert.strictEqual(address, expected.address);
59+
assert.strictEqual(port, expected.port);
60+
assert.strictEqual(family, expected.family);
61+
}, 2));
62+
63+
connection.on('connectionAttemptFailed', common.mustCall((address, port, family, error) => {
64+
const expected = addresses.shift();
65+
66+
assert.strictEqual(address, expected.address);
67+
assert.strictEqual(port, expected.port);
68+
assert.strictEqual(family, expected.family);
69+
70+
assert.ok(error.code.match(/ECONNREFUSED|EHOSTUNREACH|ETIMEDOUT/));
71+
}, 2));
72+
73+
connection.on('ready', common.mustNotCall());
74+
connection.on('error', common.mustCall());
75+
}
76+
1077
// Test that if a connection attempt times out and the socket is destroyed before the
1178
// next attempt starts then the process does not crash
1279
{
1380
const connection = createConnection({
1481
host: 'example.org',
1582
port: 443,
16-
lookup: createMockedLookup(INET4_IP, '127.0.0.1'),
83+
lookup: createMockedLookup(INET4_IP, INET6_IP),
1784
autoSelectFamily: true,
1885
autoSelectFamilyAttemptTimeout: 10,
1986
});

test/parallel/test-net-autoselectfamily-events.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)