-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: fix double AbortSignal registration
Fix an issue where AbortSignals are registered twice
- Loading branch information
Showing
5 changed files
with
137 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const Agent = http.Agent; | ||
const { getEventListeners } = require('events'); | ||
const agent = new Agent(); | ||
const server = http.createServer(); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const port = server.address().port; | ||
const host = 'localhost'; | ||
const options = { | ||
port: port, | ||
host: host, | ||
_agentKey: agent.getName({ port, host }) | ||
}; | ||
|
||
function postCreateConnection() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const connection = agent.createConnection({ ...options, signal }); | ||
connection.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
}); | ||
} | ||
|
||
function preCreateConnection() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const connection = agent.createConnection({ ...options, signal }); | ||
connection.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
}); | ||
} | ||
|
||
function agentAsParam() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const request = http.get({ | ||
port: server.address().port, | ||
path: '/hello', | ||
agent: agent, | ||
signal, | ||
}); | ||
request.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
}); | ||
} | ||
|
||
function agentAsParamPreAbort() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const request = http.get({ | ||
port: server.address().port, | ||
path: '/hello', | ||
agent: agent, | ||
signal, | ||
}); | ||
request.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
}); | ||
} | ||
|
||
await postCreateConnection(); | ||
await preCreateConnection(); | ||
await agentAsParam(); | ||
await agentAsParamPreAbort(); | ||
server.close(); | ||
})); |
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