11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Test that the `'timeout'` event is emitted exactly once if the `timeout`
41cb0ef41Sopenharmony_ci// option and `request.setTimeout()` are used together.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst { expectsError, mustCall } = require('../common');
71cb0ef41Sopenharmony_ciconst { strictEqual } = require('assert');
81cb0ef41Sopenharmony_ciconst { createServer, get } = require('http');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst server = createServer(() => {
111cb0ef41Sopenharmony_ci  // Never respond.
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciserver.listen(0, mustCall(() => {
151cb0ef41Sopenharmony_ci  const req = get({
161cb0ef41Sopenharmony_ci    port: server.address().port,
171cb0ef41Sopenharmony_ci    timeout: 2000,
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  req.setTimeout(1000);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  req.on('socket', mustCall((socket) => {
231cb0ef41Sopenharmony_ci    strictEqual(socket.timeout, 2000);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    socket.on('connect', mustCall(() => {
261cb0ef41Sopenharmony_ci      strictEqual(socket.timeout, 1000);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci      // Reschedule the timer to not wait 1 sec and make the test finish faster.
291cb0ef41Sopenharmony_ci      socket.setTimeout(10);
301cb0ef41Sopenharmony_ci      strictEqual(socket.timeout, 10);
311cb0ef41Sopenharmony_ci    }));
321cb0ef41Sopenharmony_ci  }));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  req.on('error', expectsError({
351cb0ef41Sopenharmony_ci    name: 'Error',
361cb0ef41Sopenharmony_ci    code: 'ECONNRESET',
371cb0ef41Sopenharmony_ci    message: 'socket hang up'
381cb0ef41Sopenharmony_ci  }));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  req.on('close', mustCall(() => {
411cb0ef41Sopenharmony_ci    strictEqual(req.destroyed, true);
421cb0ef41Sopenharmony_ci    server.close();
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  req.on('timeout', mustCall(() => {
461cb0ef41Sopenharmony_ci    strictEqual(req.socket.listenerCount('timeout'), 1);
471cb0ef41Sopenharmony_ci    req.destroy();
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci}));
50