-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: added connection attempt events
- Loading branch information
1 parent
1b60054
commit c5c53e2
Showing
9 changed files
with
463 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { addresses: { INET6_IP, INET4_IP } } = require('../common/internet'); | ||
const { createMockedLookup } = require('../common/dns'); | ||
|
||
const assert = require('assert'); | ||
const { createConnection } = require('net'); | ||
|
||
// Test that all events are emitted when trying a single IP (which means autoselectfamily is bypassed) | ||
{ | ||
const connection = createConnection({ | ||
host: 'example.org', | ||
port: 10, | ||
lookup: createMockedLookup(INET4_IP), | ||
autoSelectFamily: true, | ||
autoSelectFamilyAttemptTimeout: 10, | ||
}); | ||
|
||
connection.on('connectionAttempt', common.mustCall((address, port, family) => { | ||
assert.strictEqual(address, INET4_IP); | ||
assert.strictEqual(port, 10); | ||
assert.strictEqual(family, 4); | ||
})); | ||
|
||
connection.on('connectionAttemptFailed', common.mustCall((address, port, family, error) => { | ||
assert.strictEqual(address, INET4_IP); | ||
assert.strictEqual(port, 10); | ||
assert.strictEqual(family, 4); | ||
|
||
assert.ok(error.code.match(/ECONNREFUSED|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/)); | ||
})); | ||
|
||
connection.on('ready', common.mustNotCall()); | ||
connection.on('error', common.mustCall()); | ||
} | ||
|
||
// Test that all events are emitted when trying multiple IPs | ||
{ | ||
const connection = createConnection({ | ||
host: 'example.org', | ||
port: 10, | ||
lookup: createMockedLookup(INET6_IP, INET4_IP), | ||
autoSelectFamily: true, | ||
autoSelectFamilyAttemptTimeout: 10, | ||
}); | ||
|
||
const addresses = [ | ||
{ address: INET6_IP, port: 10, family: 6 }, | ||
{ address: INET6_IP, port: 10, family: 6 }, | ||
{ address: INET4_IP, port: 10, family: 4 }, | ||
{ address: INET4_IP, port: 10, family: 4 }, | ||
]; | ||
|
||
connection.on('connectionAttempt', common.mustCall((address, port, family) => { | ||
const expected = addresses.shift(); | ||
|
||
assert.strictEqual(address, expected.address); | ||
assert.strictEqual(port, expected.port); | ||
assert.strictEqual(family, expected.family); | ||
}, 2)); | ||
|
||
connection.on('connectionAttemptFailed', common.mustCall((address, port, family, error) => { | ||
const expected = addresses.shift(); | ||
|
||
assert.strictEqual(address, expected.address); | ||
assert.strictEqual(port, expected.port); | ||
assert.strictEqual(family, expected.family); | ||
|
||
assert.ok(error.code.match(/ECONNREFUSED|EHOSTUNREACH|ETIMEDOUT/), `Received unexpected error code ${error.code}`); | ||
}, 2)); | ||
|
||
connection.on('ready', common.mustNotCall()); | ||
connection.on('error', common.mustCall()); | ||
} | ||
|
||
// Test that if a connection attempt times out and the socket is destroyed before the | ||
// next attempt starts then the process does not crash | ||
{ | ||
const connection = createConnection({ | ||
host: 'example.org', | ||
port: 443, | ||
lookup: createMockedLookup(INET4_IP, INET6_IP), | ||
autoSelectFamily: true, | ||
autoSelectFamilyAttemptTimeout: 10, | ||
}); | ||
|
||
connection.on('connectionAttemptTimeout', common.mustCall((address, port, family) => { | ||
assert.strictEqual(address, INET4_IP); | ||
assert.strictEqual(port, 443); | ||
assert.strictEqual(family, 4); | ||
connection.destroy(); | ||
})); | ||
|
||
connection.on('ready', common.mustNotCall()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.