1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const http = require('http'); 5const Agent = http.Agent; 6const { getEventListeners, once } = require('events'); 7const agent = new Agent(); 8const server = http.createServer(); 9 10server.listen(0, common.mustCall(async () => { 11 const port = server.address().port; 12 const host = 'localhost'; 13 const options = { 14 port: port, 15 host: host, 16 _agentKey: agent.getName({ port, host }) 17 }; 18 19 async function postCreateConnection() { 20 const ac = new AbortController(); 21 const { signal } = ac; 22 const connection = agent.createConnection({ ...options, signal }); 23 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 24 ac.abort(); 25 const [err] = await once(connection, 'error'); 26 assert.strictEqual(err?.name, 'AbortError'); 27 } 28 29 async function preCreateConnection() { 30 const ac = new AbortController(); 31 const { signal } = ac; 32 ac.abort(); 33 const connection = agent.createConnection({ ...options, signal }); 34 const [err] = await once(connection, 'error'); 35 assert.strictEqual(err?.name, 'AbortError'); 36 } 37 38 async function agentAsParam() { 39 const ac = new AbortController(); 40 const { signal } = ac; 41 const request = http.get({ 42 port: server.address().port, 43 path: '/hello', 44 agent: agent, 45 signal, 46 }); 47 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 48 ac.abort(); 49 const [err] = await once(request, 'error'); 50 assert.strictEqual(err?.name, 'AbortError'); 51 } 52 53 async function agentAsParamPreAbort() { 54 const ac = new AbortController(); 55 const { signal } = ac; 56 ac.abort(); 57 const request = http.get({ 58 port: server.address().port, 59 path: '/hello', 60 agent: agent, 61 signal, 62 }); 63 assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 64 const [err] = await once(request, 'error'); 65 assert.strictEqual(err?.name, 'AbortError'); 66 } 67 68 await postCreateConnection(); 69 await preCreateConnection(); 70 await agentAsParam(); 71 await agentAsParamPreAbort(); 72 server.close(); 73})); 74