-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: add new connection attempts events #51045
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
ShogunPanda:no-attempts-when-aborted
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
128 changes: 128 additions & 0 deletions
128
test/internet/test-net-autoselectfamily-events-failure.js
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,128 @@ | ||
'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'); | ||
|
||
// | ||
// When testing this is MacOS, remember that the last connection will have no timeout at Node.js | ||
// level but only at operating system one. | ||
// | ||
// The default for MacOS is 75 seconds. It can be changed by doing: | ||
// | ||
// sudo sysctl net.inet.tcp.keepinit=VALUE_IN_MS | ||
// | ||
|
||
// Test that all failure events are emitted when trying a single IP (which means autoselectfamily is bypassed) | ||
{ | ||
const pass = common.mustCallAtLeast(1); | ||
|
||
const connection = createConnection({ | ||
host: 'example.org', | ||
port: 10, | ||
lookup: createMockedLookup(INET4_IP), | ||
autoSelectFamily: true, | ||
autoSelectFamilyAttemptTimeout: 10, | ||
}); | ||
|
||
connection.on('connectionAttempt', (address, port, family) => { | ||
assert.strictEqual(address, INET4_IP); | ||
assert.strictEqual(port, 10); | ||
assert.strictEqual(family, 4); | ||
|
||
pass(); | ||
}); | ||
|
||
connection.on('connectionAttemptFailed', (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/), | ||
`Received unexpected error code ${error.code}`, | ||
); | ||
|
||
pass(); | ||
}); | ||
|
||
connection.on('ready', () => { | ||
pass(); | ||
connection.destroy(); | ||
}); | ||
|
||
connection.on('error', () => { | ||
pass(); | ||
connection.destroy(); | ||
}); | ||
|
||
setTimeout(() => { | ||
pass(); | ||
process.exit(0); | ||
}, 5000).unref(); | ||
|
||
} | ||
|
||
// Test that all events are emitted when trying multiple IPs | ||
{ | ||
const pass = common.mustCallAtLeast(1); | ||
|
||
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', (address, port, family) => { | ||
const expected = addresses.shift(); | ||
|
||
assert.strictEqual(address, expected.address); | ||
assert.strictEqual(port, expected.port); | ||
assert.strictEqual(family, expected.family); | ||
|
||
pass(); | ||
}); | ||
|
||
connection.on('connectionAttemptFailed', (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|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/), | ||
`Received unexpected error code ${error.code}`, | ||
); | ||
|
||
pass(); | ||
}); | ||
|
||
connection.on('ready', () => { | ||
pass(); | ||
connection.destroy(); | ||
}); | ||
|
||
connection.on('error', () => { | ||
pass(); | ||
connection.destroy(); | ||
}); | ||
|
||
setTimeout(() => { | ||
pass(); | ||
process.exit(0); | ||
}, 5000).unref(); | ||
|
||
} |
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,54 @@ | ||
'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'); | ||
|
||
// | ||
// When testing this is MacOS, remember that the last connection will have no timeout at Node.js | ||
// level but only at operating system one. | ||
// | ||
// The default for MacOS is 75 seconds. It can be changed by doing: | ||
// | ||
// sudo sysctl net.inet.tcp.keepinit=VALUE_IN_MS | ||
// | ||
// Depending on the network, it might be impossible to obtain a timeout in 10ms, | ||
// which is the minimum value allowed by network family autoselection. | ||
// At the time of writing (Dec 2023), the network times out on local machine and in the Node CI, | ||
// but it does not on GitHub actions runner. | ||
// Therefore, after five seconds we just consider this test as passed. | ||
|
||
// 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, | ||
}); | ||
|
||
const pass = common.mustCall(); | ||
|
||
connection.on('connectionAttemptTimeout', (address, port, family) => { | ||
assert.strictEqual(address, INET4_IP); | ||
assert.strictEqual(port, 443); | ||
assert.strictEqual(family, 4); | ||
connection.destroy(); | ||
pass(); | ||
}); | ||
|
||
connection.on('ready', () => { | ||
pass(); | ||
connection.destroy(); | ||
}); | ||
|
||
setTimeout(() => { | ||
pass(); | ||
process.exit(0); | ||
}, 5000).unref(); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShogunPanda Looks like it should be
* address {string}
. Notnumber
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. This has been fixed in #51490.