11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst { kTimeout, TIMEOUT_MAX } = require('internal/timers');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst tls = require('tls');
121cb0ef41Sopenharmony_ciconst net = require('net');
131cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst options = {
161cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
171cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = tls.createServer(options, common.mustCall((c) => {
211cb0ef41Sopenharmony_ci  setImmediate(() => {
221cb0ef41Sopenharmony_ci    c.write('hello', () => {
231cb0ef41Sopenharmony_ci      setImmediate(() => {
241cb0ef41Sopenharmony_ci        c.destroy();
251cb0ef41Sopenharmony_ci        server.close();
261cb0ef41Sopenharmony_ci      });
271cb0ef41Sopenharmony_ci    });
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci}));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cilet socket;
321cb0ef41Sopenharmony_cilet lastIdleStart;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciserver.listen(0, () => {
351cb0ef41Sopenharmony_ci  socket = net.connect(server.address().port, function() {
361cb0ef41Sopenharmony_ci    const s = socket.setTimeout(TIMEOUT_MAX, function() {
371cb0ef41Sopenharmony_ci      throw new Error('timeout');
381cb0ef41Sopenharmony_ci    });
391cb0ef41Sopenharmony_ci    assert.ok(s instanceof net.Socket);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    assert.notStrictEqual(socket[kTimeout]._idleTimeout, -1);
421cb0ef41Sopenharmony_ci    lastIdleStart = socket[kTimeout]._idleStart;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    const tsocket = tls.connect({
451cb0ef41Sopenharmony_ci      socket: socket,
461cb0ef41Sopenharmony_ci      rejectUnauthorized: false
471cb0ef41Sopenharmony_ci    });
481cb0ef41Sopenharmony_ci    tsocket.resume();
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci});
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciprocess.on('exit', () => {
531cb0ef41Sopenharmony_ci  assert.strictEqual(socket[kTimeout]._idleTimeout, -1);
541cb0ef41Sopenharmony_ci  assert(lastIdleStart < socket[kTimeout]._idleStart);
551cb0ef41Sopenharmony_ci});
56