1'use strict';
2
3const { mustCall, mustSucceed, hasCrypto, skip } = require('../common');
4if (!hasCrypto)
5  skip('missing crypto');
6const assert = require('assert');
7const { createServer, connect } = require('http2');
8const Countdown = require('../common/countdown');
9
10// This test ensures that `bufferSize` of Http2Session and Http2Stream work
11// as expected.
12{
13  const kSockets = 2;
14  const kTimes = 10;
15  const kBufferSize = 30;
16  const server = createServer();
17
18  let client;
19  const countdown = new Countdown(kSockets, () => {
20    client.close();
21    server.close();
22  });
23
24  server.on('stream', mustCall((stream) => {
25    stream.on('data', mustCall());
26    stream.on('end', mustCall());
27    stream.on('close', mustCall(() => {
28      countdown.dec();
29    }));
30  }, kSockets));
31
32  server.listen(0, mustCall(() => {
33    const authority = `http://localhost:${server.address().port}`;
34    client = connect(authority);
35
36    client.once('connect', mustCall());
37
38    for (let j = 0; j < kSockets; j += 1) {
39      const stream = client.request({ ':method': 'POST' });
40      stream.on('data', () => {});
41
42      for (let i = 0; i < kTimes; i += 1) {
43        stream.write(Buffer.allocUnsafe(kBufferSize), mustSucceed());
44        const expectedSocketBufferSize = kBufferSize * (i + 1);
45        assert.strictEqual(stream.bufferSize, expectedSocketBufferSize);
46      }
47      stream.end();
48      stream.close();
49    }
50  }));
51}
52