11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifor (const destroyer of ['destroy', 'abort']) {
81cb0ef41Sopenharmony_ci  let socketsCreated = 0;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  class Agent extends http.Agent {
111cb0ef41Sopenharmony_ci    createConnection(options, oncreate) {
121cb0ef41Sopenharmony_ci      const socket = super.createConnection(options, oncreate);
131cb0ef41Sopenharmony_ci      socketsCreated++;
141cb0ef41Sopenharmony_ci      return socket;
151cb0ef41Sopenharmony_ci    }
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => res.end());
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
211cb0ef41Sopenharmony_ci    const port = server.address().port;
221cb0ef41Sopenharmony_ci    const agent = new Agent({
231cb0ef41Sopenharmony_ci      keepAlive: true,
241cb0ef41Sopenharmony_ci      maxSockets: 1
251cb0ef41Sopenharmony_ci    });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    http.get({ agent, port }, (res) => res.resume());
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    const req = http.get({ agent, port }, common.mustNotCall());
301cb0ef41Sopenharmony_ci    req[destroyer]();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    if (destroyer === 'destroy') {
331cb0ef41Sopenharmony_ci      req.on('error', common.mustCall((err) => {
341cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ECONNRESET');
351cb0ef41Sopenharmony_ci      }));
361cb0ef41Sopenharmony_ci    } else {
371cb0ef41Sopenharmony_ci      req.on('error', common.mustNotCall());
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    http.get({ agent, port }, common.mustCall((res) => {
411cb0ef41Sopenharmony_ci      res.resume();
421cb0ef41Sopenharmony_ci      assert.strictEqual(socketsCreated, 1);
431cb0ef41Sopenharmony_ci      agent.destroy();
441cb0ef41Sopenharmony_ci      server.close();
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci}
48