summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-backpressure.js
blob: 9b69dddbfd2e26bb3d0ed4575fe804127bb41c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';

// Verifies that a full HTTP2 pipeline handles backpressure.

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const makeDuplexPair = require('../common/duplexpair');

common.crashOnUnhandledRejection();

{
  let req;
  const server = http2.createServer();
  server.on('stream', common.mustCallAsync(async (stream, headers) => {
    stream.respond({
      'content-type': 'text/html',
      ':status': 200
    });
    req._readableState.highWaterMark = 20;
    stream._writableState.highWaterMark = 20;
    assert.strictEqual(stream.write('A'.repeat(5)), true);
    assert.strictEqual(stream.write('A'.repeat(40)), false);
    assert.strictEqual(await event(req, 'data'), 'A'.repeat(5));
    assert.strictEqual(await event(req, 'data'), 'A'.repeat(40));
    await event(stream, 'drain');
    assert.strictEqual(stream.write('A'.repeat(5)), true);
    assert.strictEqual(stream.write('A'.repeat(40)), false);
  }));

  const { clientSide, serverSide } = makeDuplexPair();
  server.emit('connection', serverSide);

  const client = http2.connect('http://localhost:80', {
    createConnection: common.mustCall(() => clientSide)
  });

  req = client.request({ ':path': '/' });
  req.setEncoding('utf8');
  req.end();
}

function event(ee, eventName) {
  return new Promise((resolve) => {
    ee.once(eventName, common.mustCall(resolve));
  });
}