1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5
6const http2 = require('http2');
7const v8 = require('v8');
8
9// Regression test for https://github.com/nodejs/node/issues/28088:
10// Verify that Http2Settings and Http2Ping objects don't reference the session
11// after it is destroyed, either because they are detached from it or have been
12// destroyed themselves.
13
14// We use an higher autoSelectFamilyAttemptTimeout in this test as the v8.getHeapSnapshot().resume()
15// will slow the connection flow and we don't want the second connection attempt to start.
16let autoSelectFamilyAttemptTimeout = common.platformTimeout(1000);
17if (common.isWindows) {
18  // Some of the windows machines in the CI need more time to establish connection
19  autoSelectFamilyAttemptTimeout = common.platformTimeout(10000);
20}
21
22for (const variant of ['ping', 'settings']) {
23  const server = http2.createServer();
24  server.on('session', common.mustCall((session) => {
25    if (variant === 'ping') {
26      session.ping(common.expectsError({
27        code: 'ERR_HTTP2_PING_CANCEL'
28      }));
29    } else {
30      session.settings(undefined, common.mustNotCall());
31    }
32
33    session.on('close', common.mustCall(() => {
34      v8.getHeapSnapshot().resume();
35      server.close();
36    }));
37    session.destroy();
38  }));
39
40  server.listen(0, common.mustCall(() => {
41    const client = http2.connect(`http://localhost:${server.address().port}`, { autoSelectFamilyAttemptTimeout },
42                                 common.mustCall());
43    client.on('error', (err) => {
44      // We destroy the session so it's possible to get ECONNRESET here.
45      if (err.code !== 'ECONNRESET')
46        throw err;
47    });
48  }));
49}
50