11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { mustCall, mustSucceed, hasCrypto, skip } = require('../common');
41cb0ef41Sopenharmony_ciif (!hasCrypto)
51cb0ef41Sopenharmony_ci  skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { createServer, connect } = require('http2');
81cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// This test ensures that `bufferSize` of Http2Session and Http2Stream work
111cb0ef41Sopenharmony_ci// as expected.
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  const kSockets = 2;
141cb0ef41Sopenharmony_ci  const kTimes = 10;
151cb0ef41Sopenharmony_ci  const kBufferSize = 30;
161cb0ef41Sopenharmony_ci  const server = createServer();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  let client;
191cb0ef41Sopenharmony_ci  const countdown = new Countdown(kSockets, () => {
201cb0ef41Sopenharmony_ci    client.close();
211cb0ef41Sopenharmony_ci    server.close();
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  server.on('stream', mustCall((stream) => {
251cb0ef41Sopenharmony_ci    stream.on('data', mustCall());
261cb0ef41Sopenharmony_ci    stream.on('end', mustCall());
271cb0ef41Sopenharmony_ci    stream.on('close', mustCall(() => {
281cb0ef41Sopenharmony_ci      countdown.dec();
291cb0ef41Sopenharmony_ci    }));
301cb0ef41Sopenharmony_ci  }, kSockets));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  server.listen(0, mustCall(() => {
331cb0ef41Sopenharmony_ci    const authority = `http://localhost:${server.address().port}`;
341cb0ef41Sopenharmony_ci    client = connect(authority);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    client.once('connect', mustCall());
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    for (let j = 0; j < kSockets; j += 1) {
391cb0ef41Sopenharmony_ci      const stream = client.request({ ':method': 'POST' });
401cb0ef41Sopenharmony_ci      stream.on('data', () => {});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci      for (let i = 0; i < kTimes; i += 1) {
431cb0ef41Sopenharmony_ci        stream.write(Buffer.allocUnsafe(kBufferSize), mustSucceed());
441cb0ef41Sopenharmony_ci        const expectedSocketBufferSize = kBufferSize * (i + 1);
451cb0ef41Sopenharmony_ci        assert.strictEqual(stream.bufferSize, expectedSocketBufferSize);
461cb0ef41Sopenharmony_ci      }
471cb0ef41Sopenharmony_ci      stream.end();
481cb0ef41Sopenharmony_ci      stream.close();
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  }));
511cb0ef41Sopenharmony_ci}
52