11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst Agent = http.Agent;
61cb0ef41Sopenharmony_ciconst { getEventListeners, once } = require('events');
71cb0ef41Sopenharmony_ciconst agent = new Agent();
81cb0ef41Sopenharmony_ciconst server = http.createServer();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(async () => {
111cb0ef41Sopenharmony_ci  const port = server.address().port;
121cb0ef41Sopenharmony_ci  const host = 'localhost';
131cb0ef41Sopenharmony_ci  const options = {
141cb0ef41Sopenharmony_ci    port: port,
151cb0ef41Sopenharmony_ci    host: host,
161cb0ef41Sopenharmony_ci    _agentKey: agent.getName({ port, host })
171cb0ef41Sopenharmony_ci  };
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  async function postCreateConnection() {
201cb0ef41Sopenharmony_ci    const ac = new AbortController();
211cb0ef41Sopenharmony_ci    const { signal } = ac;
221cb0ef41Sopenharmony_ci    const connection = agent.createConnection({ ...options, signal });
231cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
241cb0ef41Sopenharmony_ci    ac.abort();
251cb0ef41Sopenharmony_ci    const [err] = await once(connection, 'error');
261cb0ef41Sopenharmony_ci    assert.strictEqual(err?.name, 'AbortError');
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  async function preCreateConnection() {
301cb0ef41Sopenharmony_ci    const ac = new AbortController();
311cb0ef41Sopenharmony_ci    const { signal } = ac;
321cb0ef41Sopenharmony_ci    ac.abort();
331cb0ef41Sopenharmony_ci    const connection = agent.createConnection({ ...options, signal });
341cb0ef41Sopenharmony_ci    const [err] = await once(connection, 'error');
351cb0ef41Sopenharmony_ci    assert.strictEqual(err?.name, 'AbortError');
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  async function agentAsParam() {
391cb0ef41Sopenharmony_ci    const ac = new AbortController();
401cb0ef41Sopenharmony_ci    const { signal } = ac;
411cb0ef41Sopenharmony_ci    const request = http.get({
421cb0ef41Sopenharmony_ci      port: server.address().port,
431cb0ef41Sopenharmony_ci      path: '/hello',
441cb0ef41Sopenharmony_ci      agent: agent,
451cb0ef41Sopenharmony_ci      signal,
461cb0ef41Sopenharmony_ci    });
471cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
481cb0ef41Sopenharmony_ci    ac.abort();
491cb0ef41Sopenharmony_ci    const [err] = await once(request, 'error');
501cb0ef41Sopenharmony_ci    assert.strictEqual(err?.name, 'AbortError');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  async function agentAsParamPreAbort() {
541cb0ef41Sopenharmony_ci    const ac = new AbortController();
551cb0ef41Sopenharmony_ci    const { signal } = ac;
561cb0ef41Sopenharmony_ci    ac.abort();
571cb0ef41Sopenharmony_ci    const request = http.get({
581cb0ef41Sopenharmony_ci      port: server.address().port,
591cb0ef41Sopenharmony_ci      path: '/hello',
601cb0ef41Sopenharmony_ci      agent: agent,
611cb0ef41Sopenharmony_ci      signal,
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
641cb0ef41Sopenharmony_ci    const [err] = await once(request, 'error');
651cb0ef41Sopenharmony_ci    assert.strictEqual(err?.name, 'AbortError');
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  await postCreateConnection();
691cb0ef41Sopenharmony_ci  await preCreateConnection();
701cb0ef41Sopenharmony_ci  await agentAsParam();
711cb0ef41Sopenharmony_ci  await agentAsParamPreAbort();
721cb0ef41Sopenharmony_ci  server.close();
731cb0ef41Sopenharmony_ci}));
74