11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Tests opening 100 concurrent simultaneous uploading streams over a single
41cb0ef41Sopenharmony_ci// connection and makes sure that the data for each is appropriately echoed.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst http2 = require('http2');
111cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = http2.createServer();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst count = 100;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
181cb0ef41Sopenharmony_ci  stream.respond();
191cb0ef41Sopenharmony_ci  stream.pipe(stream);
201cb0ef41Sopenharmony_ci}, count));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
231cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
241cb0ef41Sopenharmony_ci  client.setMaxListeners(100);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const countdown = new Countdown(count, () => {
271cb0ef41Sopenharmony_ci    server.close();
281cb0ef41Sopenharmony_ci    client.close();
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  function doRequest() {
321cb0ef41Sopenharmony_ci    const req = client.request({ ':method': 'POST' });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    let data = '';
351cb0ef41Sopenharmony_ci    req.setEncoding('utf8');
361cb0ef41Sopenharmony_ci    req.on('data', (chunk) => data += chunk);
371cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
381cb0ef41Sopenharmony_ci      assert.strictEqual(data, 'abcdefghij');
391cb0ef41Sopenharmony_ci    }));
401cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => countdown.dec()));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    let n = 0;
431cb0ef41Sopenharmony_ci    function writeChunk() {
441cb0ef41Sopenharmony_ci      if (n < 10) {
451cb0ef41Sopenharmony_ci        req.write(String.fromCharCode(97 + n));
461cb0ef41Sopenharmony_ci        setTimeout(writeChunk, 10);
471cb0ef41Sopenharmony_ci      } else {
481cb0ef41Sopenharmony_ci        req.end();
491cb0ef41Sopenharmony_ci      }
501cb0ef41Sopenharmony_ci      n++;
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    writeChunk();
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  for (let n = 0; n < count; n++)
571cb0ef41Sopenharmony_ci    doRequest();
581cb0ef41Sopenharmony_ci}));
59