|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const common = require('../common');
|
4 |
| -const { addresses: { INET4_IP } } = require('../common/internet'); |
| 4 | +const { addresses: { INET6_IP, INET4_IP } } = require('../common/internet'); |
5 | 5 | const { createMockedLookup } = require('../common/dns');
|
6 | 6 |
|
7 | 7 | const assert = require('assert');
|
8 | 8 | const { createConnection } = require('net');
|
9 | 9 |
|
| 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 | + |
10 | 77 | // Test that if a connection attempt times out and the socket is destroyed before the
|
11 | 78 | // next attempt starts then the process does not crash
|
12 | 79 | {
|
13 | 80 | const connection = createConnection({
|
14 | 81 | host: 'example.org',
|
15 | 82 | port: 443,
|
16 |
| - lookup: createMockedLookup(INET4_IP, '127.0.0.1'), |
| 83 | + lookup: createMockedLookup(INET4_IP, INET6_IP), |
17 | 84 | autoSelectFamily: true,
|
18 | 85 | autoSelectFamilyAttemptTimeout: 10,
|
19 | 86 | });
|
|
0 commit comments