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/29223.
91cb0ef41Sopenharmony_ci// There was a "leak" in the accounting of session memory leading
101cb0ef41Sopenharmony_ci// to streams eventually failing with NGHTTP2_ENHANCE_YOUR_CALM.
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_ci// Simple server that sends 200k and closes the stream.
181cb0ef41Sopenharmony_ciconst data200k = 'a'.repeat(200 * 1024);
191cb0ef41Sopenharmony_ciserver.on('stream', (stream) => {
201cb0ef41Sopenharmony_ci  stream.write(data200k);
211cb0ef41Sopenharmony_ci  stream.end();
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
251cb0ef41Sopenharmony_ci  const client = http2.connect(`https://localhost:${server.address().port}`, {
261cb0ef41Sopenharmony_ci    ca: fixtures.readKey('agent2-cert.pem'),
271cb0ef41Sopenharmony_ci    servername: 'agent2',
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    // Set maxSessionMemory to 1MB so the leak causes errors faster.
301cb0ef41Sopenharmony_ci    maxSessionMemory: 1
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // Repeatedly create a new stream and read the incoming data. Even though we
341cb0ef41Sopenharmony_ci  // only have one stream active at a time, prior to the fix for #29223,
351cb0ef41Sopenharmony_ci  // session memory would steadily increase and we'd eventually hit the 1MB
361cb0ef41Sopenharmony_ci  // maxSessionMemory limit and get NGHTTP2_ENHANCE_YOUR_CALM errors trying to
371cb0ef41Sopenharmony_ci  // create new streams.
381cb0ef41Sopenharmony_ci  let streamsLeft = 50;
391cb0ef41Sopenharmony_ci  function newStream() {
401cb0ef41Sopenharmony_ci    const stream = client.request({ ':path': '/' });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    stream.on('data', () => { });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    stream.on('close', () => {
451cb0ef41Sopenharmony_ci      if (streamsLeft-- > 0) {
461cb0ef41Sopenharmony_ci        newStream();
471cb0ef41Sopenharmony_ci      } else {
481cb0ef41Sopenharmony_ci        client.destroy();
491cb0ef41Sopenharmony_ci        server.close();
501cb0ef41Sopenharmony_ci      }
511cb0ef41Sopenharmony_ci    });
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  newStream();
551cb0ef41Sopenharmony_ci}));
56