11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto) {
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci}
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Issue #23116
81cb0ef41Sopenharmony_ci// nghttp2 keeps closed stream structures around in memory (couple of hundred
91cb0ef41Sopenharmony_ci// bytes each) until a session is closed. It does this to maintain the priority
101cb0ef41Sopenharmony_ci// tree. However, it limits the number of requests that can be made in a
111cb0ef41Sopenharmony_ci// session before our memory tracking (correctly) kicks in.
121cb0ef41Sopenharmony_ci// The fix is to tell nghttp2 to forget about closed streams. We don't make use
131cb0ef41Sopenharmony_ci// of priority anyway.
141cb0ef41Sopenharmony_ci// Without the fix, this test fails at ~40k requests with an exception:
151cb0ef41Sopenharmony_ci// Error [ERR_HTTP2_STREAM_ERROR]: Stream closed with error code
161cb0ef41Sopenharmony_ci// NGHTTP2_ENHANCE_YOUR_CALM
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst http2 = require('http2');
191cb0ef41Sopenharmony_ciconst assert = require('assert');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst server = http2.createServer({ maxSessionMemory: 1 });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciserver.on('session', function(session) {
241cb0ef41Sopenharmony_ci  session.on('stream', function(stream) {
251cb0ef41Sopenharmony_ci    stream.on('end', common.mustCall(function() {
261cb0ef41Sopenharmony_ci      this.respond({
271cb0ef41Sopenharmony_ci        ':status': 200
281cb0ef41Sopenharmony_ci      }, {
291cb0ef41Sopenharmony_ci        endStream: true
301cb0ef41Sopenharmony_ci      });
311cb0ef41Sopenharmony_ci    }));
321cb0ef41Sopenharmony_ci    stream.resume();
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci});
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciserver.listen(0, function() {
371cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  function next(i) {
401cb0ef41Sopenharmony_ci    if (i === 10000) {
411cb0ef41Sopenharmony_ci      client.close();
421cb0ef41Sopenharmony_ci      return server.close();
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci    const stream = client.request({ ':method': 'POST' });
451cb0ef41Sopenharmony_ci    stream.on('response', common.mustCall(function(headers) {
461cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
471cb0ef41Sopenharmony_ci      this.on('close', common.mustCall(() => next(i + 1)));
481cb0ef41Sopenharmony_ci    }));
491cb0ef41Sopenharmony_ci    stream.end();
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  next(0);
531cb0ef41Sopenharmony_ci});
54