summaryrefslogtreecommitdiff
path: root/test/async-hooks/test-graph.connection.js
blob: 37ecc79bb06956fb72c6ed88ed7c5844b4a89501 (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
56
57
'use strict';

const initHooks = require('./init-hooks');
const common = require('../common');
const verifyGraph = require('./verify-graph');

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

const tls = require('tls');
const Connection = process.binding('crypto').Connection;
const hooks = initHooks();
hooks.enable();

function createServerConnection(
  onhandshakestart,
  certificate = null,
  isServer = true,
  servername = 'some server',
  rejectUnauthorized
) {
  if (certificate == null) certificate = tls.createSecureContext();
  const ssl = new Connection(
    certificate.context, isServer, servername, rejectUnauthorized
  );
  if (isServer) {
    ssl.onhandshakestart = onhandshakestart;
    ssl.lastHandshakeTime = 0;
  }
  return ssl;
}

// creating first server connection and start it
const sc1 = createServerConnection(common.mustCall(onfirstHandShake));
sc1.start();

function onfirstHandShake() {
  // Create second connection inside handshake of first to show
  // that the triggerAsyncId of the second will be set to id of the first
  const sc2 = createServerConnection(common.mustCall(onsecondHandShake));
  sc2.start();
}
function onsecondHandShake() { }

process.on('exit', onexit);

function onexit() {
  hooks.disable();
  verifyGraph(
    hooks,
    [ { type: 'CONNECTION', id: 'connection:1', triggerAsyncId: null },
      { type: 'CONNECTION', id: 'connection:2',
        triggerAsyncId: 'connection:1' } ]
  );
}