11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ciconst http2 = require('http2');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/29353.
91cb0ef41Sopenharmony_ci// Test that it’s okay for an HTTP2 + TLS server to destroy a stream instance
101cb0ef41Sopenharmony_ci// while reading it.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst server = http2.createSecureServer({
131cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent2-key.pem'),
141cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent2-cert.pem')
151cb0ef41Sopenharmony_ci});
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst filenames = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
201cb0ef41Sopenharmony_ci  function write() {
211cb0ef41Sopenharmony_ci    stream.write('a'.repeat(10240));
221cb0ef41Sopenharmony_ci    stream.once('drain', write);
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  write();
251cb0ef41Sopenharmony_ci}, filenames.length));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
281cb0ef41Sopenharmony_ci  const client = http2.connect(`https://localhost:${server.address().port}`, {
291cb0ef41Sopenharmony_ci    ca: fixtures.readKey('agent2-cert.pem'),
301cb0ef41Sopenharmony_ci    servername: 'agent2'
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  let destroyed = 0;
341cb0ef41Sopenharmony_ci  for (const entry of filenames) {
351cb0ef41Sopenharmony_ci    const stream = client.request({
361cb0ef41Sopenharmony_ci      ':path': `/${entry}`
371cb0ef41Sopenharmony_ci    });
381cb0ef41Sopenharmony_ci    stream.once('data', common.mustCall(() => {
391cb0ef41Sopenharmony_ci      stream.destroy();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci      if (++destroyed === filenames.length) {
421cb0ef41Sopenharmony_ci        client.destroy();
431cb0ef41Sopenharmony_ci        server.close();
441cb0ef41Sopenharmony_ci      }
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci}));
48