summaryrefslogtreecommitdiff
path: root/test/parallel/test-tls-wrap-timeout.js
blob: 6ae2c39c59b8d92c0999824f68c54f6abf8c834a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Flags: --expose_internals

'use strict';
const common = require('../common');
const { kTimeout, TIMEOUT_MAX } = require('internal/timers');

if (!common.hasCrypto)
  common.skip('missing crypto');

const assert = require('assert');
const tls = require('tls');
const net = require('net');
const fixtures = require('../common/fixtures');

const options = {
  key: fixtures.readKey('agent1-key.pem'),
  cert: fixtures.readKey('agent1-cert.pem')
};

const server = tls.createServer(options, common.mustCall((c) => {
  setImmediate(() => {
    c.write('hello', () => {
      setImmediate(() => {
        c.destroy();
        server.close();
      });
    });
  });
}));

let socket;
let lastIdleStart;

server.listen(0, () => {
  socket = net.connect(server.address().port, function() {
    const s = socket.setTimeout(TIMEOUT_MAX, function() {
      throw new Error('timeout');
    });
    assert.ok(s instanceof net.Socket);

    assert.notStrictEqual(socket[kTimeout]._idleTimeout, -1);
    lastIdleStart = socket[kTimeout]._idleStart;

    const tsocket = tls.connect({
      socket: socket,
      rejectUnauthorized: false
    });
    tsocket.resume();
  });
});

process.on('exit', () => {
  assert.strictEqual(socket[kTimeout]._idleTimeout, -1);
  assert(lastIdleStart < socket[kTimeout]._idleStart);
});