summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-agent-abort-controller.js
diff options
context:
space:
mode:
authorNitzan Uziely <linkgoron@gmail.com>2021-03-12 20:24:00 +0200
committerRich Trott <rtrott@gmail.com>2021-03-20 12:52:04 -0700
commit711e210d35b41a7a4b81497affd4ccace1f31d3a (patch)
tree631b88a7d81ce6cb8f416da154e2c66873f609c5 /test/parallel/test-http-agent-abort-controller.js
parenta41c3e17adca6a8f8e50a6f8f657dd4cf85fc9ce (diff)
downloadnode-new-711e210d35b41a7a4b81497affd4ccace1f31d3a.tar.gz
http: fix double AbortSignal registration
Fix an issue where AbortSignals are registered twice PR-URL: https://github.com/nodejs/node/pull/37730 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/parallel/test-http-agent-abort-controller.js')
-rw-r--r--test/parallel/test-http-agent-abort-controller.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/parallel/test-http-agent-abort-controller.js b/test/parallel/test-http-agent-abort-controller.js
new file mode 100644
index 0000000000..c5ece3ab35
--- /dev/null
+++ b/test/parallel/test-http-agent-abort-controller.js
@@ -0,0 +1,73 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const http = require('http');
+const Agent = http.Agent;
+const { getEventListeners, once } = 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 })
+ };
+
+ async function postCreateConnection() {
+ const ac = new AbortController();
+ const { signal } = ac;
+ const connection = agent.createConnection({ ...options, signal });
+ assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
+ ac.abort();
+ const [err] = await once(connection, 'error');
+ assert.strictEqual(err?.name, 'AbortError');
+ }
+
+ async function preCreateConnection() {
+ const ac = new AbortController();
+ const { signal } = ac;
+ ac.abort();
+ const connection = agent.createConnection({ ...options, signal });
+ const [err] = await once(connection, 'error');
+ assert.strictEqual(err?.name, 'AbortError');
+ }
+
+ async function agentAsParam() {
+ const ac = new AbortController();
+ const { signal } = ac;
+ const request = http.get({
+ port: server.address().port,
+ path: '/hello',
+ agent: agent,
+ signal,
+ });
+ assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
+ ac.abort();
+ const [err] = await once(request, 'error');
+ assert.strictEqual(err?.name, 'AbortError');
+ }
+
+ async function agentAsParamPreAbort() {
+ const ac = new AbortController();
+ const { signal } = ac;
+ ac.abort();
+ const request = http.get({
+ port: server.address().port,
+ path: '/hello',
+ agent: agent,
+ signal,
+ });
+ assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
+ const [err] = await once(request, 'error');
+ assert.strictEqual(err?.name, 'AbortError');
+ }
+
+ await postCreateConnection();
+ await preCreateConnection();
+ await agentAsParam();
+ await agentAsParamPreAbort();
+ server.close();
+}));