11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst http = require('http');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci{
81cb0ef41Sopenharmony_ci  // Ensure reuse of successful sockets.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  const agent = new http.Agent({ keepAlive: true });
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => {
131cb0ef41Sopenharmony_ci    res.end();
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
171cb0ef41Sopenharmony_ci    let socket;
181cb0ef41Sopenharmony_ci    http.get({ port: server.address().port, agent })
191cb0ef41Sopenharmony_ci      .on('response', common.mustCall((res) => {
201cb0ef41Sopenharmony_ci        socket = res.socket;
211cb0ef41Sopenharmony_ci        assert(socket);
221cb0ef41Sopenharmony_ci        res.resume();
231cb0ef41Sopenharmony_ci        socket.on('free', common.mustCall(() => {
241cb0ef41Sopenharmony_ci          http.get({ port: server.address().port, agent })
251cb0ef41Sopenharmony_ci            .on('response', common.mustCall((res) => {
261cb0ef41Sopenharmony_ci              assert.strictEqual(socket, res.socket);
271cb0ef41Sopenharmony_ci              assert(socket);
281cb0ef41Sopenharmony_ci              agent.destroy();
291cb0ef41Sopenharmony_ci              server.close();
301cb0ef41Sopenharmony_ci            }));
311cb0ef41Sopenharmony_ci        }));
321cb0ef41Sopenharmony_ci      }));
331cb0ef41Sopenharmony_ci  }));
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  // Ensure that timed-out sockets are not reused.
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  const agent = new http.Agent({ keepAlive: true, timeout: 50 });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => {
421cb0ef41Sopenharmony_ci    res.end();
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
461cb0ef41Sopenharmony_ci    http.get({ port: server.address().port, agent })
471cb0ef41Sopenharmony_ci      .on('response', common.mustCall((res) => {
481cb0ef41Sopenharmony_ci        const socket = res.socket;
491cb0ef41Sopenharmony_ci        assert(socket);
501cb0ef41Sopenharmony_ci        res.resume();
511cb0ef41Sopenharmony_ci        socket.on('free', common.mustCall(() => {
521cb0ef41Sopenharmony_ci          socket.on('timeout', common.mustCall(() => {
531cb0ef41Sopenharmony_ci            http.get({ port: server.address().port, agent })
541cb0ef41Sopenharmony_ci              .on('response', common.mustCall((res) => {
551cb0ef41Sopenharmony_ci                assert.notStrictEqual(socket, res.socket);
561cb0ef41Sopenharmony_ci                assert.strictEqual(socket.destroyed, true);
571cb0ef41Sopenharmony_ci                agent.destroy();
581cb0ef41Sopenharmony_ci                server.close();
591cb0ef41Sopenharmony_ci              }));
601cb0ef41Sopenharmony_ci          }));
611cb0ef41Sopenharmony_ci        }));
621cb0ef41Sopenharmony_ci      }));
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  // Ensure that destroyed sockets are not reused.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  const agent = new http.Agent({ keepAlive: true });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => {
721cb0ef41Sopenharmony_ci    res.end();
731cb0ef41Sopenharmony_ci  });
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
761cb0ef41Sopenharmony_ci    let socket;
771cb0ef41Sopenharmony_ci    http.get({ port: server.address().port, agent })
781cb0ef41Sopenharmony_ci      .on('response', common.mustCall((res) => {
791cb0ef41Sopenharmony_ci        socket = res.socket;
801cb0ef41Sopenharmony_ci        assert(socket);
811cb0ef41Sopenharmony_ci        res.resume();
821cb0ef41Sopenharmony_ci        socket.on('free', common.mustCall(() => {
831cb0ef41Sopenharmony_ci          socket.destroy();
841cb0ef41Sopenharmony_ci          http.get({ port: server.address().port, agent })
851cb0ef41Sopenharmony_ci            .on('response', common.mustCall((res) => {
861cb0ef41Sopenharmony_ci              assert.notStrictEqual(socket, res.socket);
871cb0ef41Sopenharmony_ci              assert(socket);
881cb0ef41Sopenharmony_ci              agent.destroy();
891cb0ef41Sopenharmony_ci              server.close();
901cb0ef41Sopenharmony_ci            }));
911cb0ef41Sopenharmony_ci        }));
921cb0ef41Sopenharmony_ci      }));
931cb0ef41Sopenharmony_ci  }));
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci{
971cb0ef41Sopenharmony_ci  // Ensure custom keepSocketAlive timeout is respected
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  const CUSTOM_TIMEOUT = 60;
1001cb0ef41Sopenharmony_ci  const AGENT_TIMEOUT = 50;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  class CustomAgent extends http.Agent {
1031cb0ef41Sopenharmony_ci    keepSocketAlive(socket) {
1041cb0ef41Sopenharmony_ci      if (!super.keepSocketAlive(socket)) {
1051cb0ef41Sopenharmony_ci        return false;
1061cb0ef41Sopenharmony_ci      }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci      socket.setTimeout(CUSTOM_TIMEOUT);
1091cb0ef41Sopenharmony_ci      return true;
1101cb0ef41Sopenharmony_ci    }
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  const agent = new CustomAgent({ keepAlive: true, timeout: AGENT_TIMEOUT });
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => {
1161cb0ef41Sopenharmony_ci    res.end();
1171cb0ef41Sopenharmony_ci  });
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1201cb0ef41Sopenharmony_ci    http.get({ port: server.address().port, agent })
1211cb0ef41Sopenharmony_ci      .on('response', common.mustCall((res) => {
1221cb0ef41Sopenharmony_ci        const socket = res.socket;
1231cb0ef41Sopenharmony_ci        assert(socket);
1241cb0ef41Sopenharmony_ci        res.resume();
1251cb0ef41Sopenharmony_ci        socket.on('free', common.mustCall(() => {
1261cb0ef41Sopenharmony_ci          socket.on('timeout', common.mustCall(() => {
1271cb0ef41Sopenharmony_ci            assert.strictEqual(socket.timeout, CUSTOM_TIMEOUT);
1281cb0ef41Sopenharmony_ci            agent.destroy();
1291cb0ef41Sopenharmony_ci            server.close();
1301cb0ef41Sopenharmony_ci          }));
1311cb0ef41Sopenharmony_ci        }));
1321cb0ef41Sopenharmony_ci      }));
1331cb0ef41Sopenharmony_ci  }));
1341cb0ef41Sopenharmony_ci}
135