forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-net-autoselectfamily-ipv4first.js
More file actions
52 lines (42 loc) · 1.37 KB
/
test-net-autoselectfamily-ipv4first.js
File metadata and controls
52 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const common = require('../common');
const { createMockedLookup } = require('../common/dns');
const assert = require('assert');
const { createConnection, createServer } = require('net');
// Test that happy eyeballs algorithm is properly implemented when a A record is returned first.
if (common.hasIPv6) {
const ipv4Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
}));
const ipv6Server = createServer((socket) => {
socket.on('data', common.mustNotCall(() => {
socket.write('response-ipv6');
socket.end();
}));
});
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
ipv6Server.listen(port, '::1', common.mustCall(() => {
const connection = createConnection({
host: 'example.org',
port,
lookup: createMockedLookup('127.0.0.1', '::1'),
autoSelectFamily: true,
});
let response = '';
connection.setEncoding('utf-8');
connection.on('data', (chunk) => {
response += chunk;
});
connection.on('end', common.mustCall(() => {
assert.strictEqual(response, 'response-ipv4');
ipv4Server.close();
ipv6Server.close();
}));
connection.write('request');
}));
}));
}