11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst http2 = require('http2');
71cb0ef41Sopenharmony_ciconst v8 = require('v8');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/28088:
101cb0ef41Sopenharmony_ci// Verify that Http2Settings and Http2Ping objects don't reference the session
111cb0ef41Sopenharmony_ci// after it is destroyed, either because they are detached from it or have been
121cb0ef41Sopenharmony_ci// destroyed themselves.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// We use an higher autoSelectFamilyAttemptTimeout in this test as the v8.getHeapSnapshot().resume()
151cb0ef41Sopenharmony_ci// will slow the connection flow and we don't want the second connection attempt to start.
161cb0ef41Sopenharmony_cilet autoSelectFamilyAttemptTimeout = common.platformTimeout(1000);
171cb0ef41Sopenharmony_ciif (common.isWindows) {
181cb0ef41Sopenharmony_ci  // Some of the windows machines in the CI need more time to establish connection
191cb0ef41Sopenharmony_ci  autoSelectFamilyAttemptTimeout = common.platformTimeout(10000);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cifor (const variant of ['ping', 'settings']) {
231cb0ef41Sopenharmony_ci  const server = http2.createServer();
241cb0ef41Sopenharmony_ci  server.on('session', common.mustCall((session) => {
251cb0ef41Sopenharmony_ci    if (variant === 'ping') {
261cb0ef41Sopenharmony_ci      session.ping(common.expectsError({
271cb0ef41Sopenharmony_ci        code: 'ERR_HTTP2_PING_CANCEL'
281cb0ef41Sopenharmony_ci      }));
291cb0ef41Sopenharmony_ci    } else {
301cb0ef41Sopenharmony_ci      session.settings(undefined, common.mustNotCall());
311cb0ef41Sopenharmony_ci    }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    session.on('close', common.mustCall(() => {
341cb0ef41Sopenharmony_ci      v8.getHeapSnapshot().resume();
351cb0ef41Sopenharmony_ci      server.close();
361cb0ef41Sopenharmony_ci    }));
371cb0ef41Sopenharmony_ci    session.destroy();
381cb0ef41Sopenharmony_ci  }));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
411cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${server.address().port}`, { autoSelectFamilyAttemptTimeout },
421cb0ef41Sopenharmony_ci                                 common.mustCall());
431cb0ef41Sopenharmony_ci    client.on('error', (err) => {
441cb0ef41Sopenharmony_ci      // We destroy the session so it's possible to get ECONNRESET here.
451cb0ef41Sopenharmony_ci      if (err.code !== 'ECONNRESET')
461cb0ef41Sopenharmony_ci        throw err;
471cb0ef41Sopenharmony_ci    });
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci}
50