11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Verifies that a full HTTP2 pipeline handles backpressure.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  let req;
141cb0ef41Sopenharmony_ci  const server = http2.createServer();
151cb0ef41Sopenharmony_ci  server.on('stream', mustCallAsync(async (stream, headers) => {
161cb0ef41Sopenharmony_ci    stream.respond({
171cb0ef41Sopenharmony_ci      'content-type': 'text/html',
181cb0ef41Sopenharmony_ci      ':status': 200
191cb0ef41Sopenharmony_ci    });
201cb0ef41Sopenharmony_ci    req._readableState.highWaterMark = 20;
211cb0ef41Sopenharmony_ci    stream._writableState.highWaterMark = 20;
221cb0ef41Sopenharmony_ci    assert.strictEqual(stream.write('A'.repeat(5)), true);
231cb0ef41Sopenharmony_ci    assert.strictEqual(stream.write('A'.repeat(40)), false);
241cb0ef41Sopenharmony_ci    assert.strictEqual(await event(req, 'data'), 'A'.repeat(5));
251cb0ef41Sopenharmony_ci    assert.strictEqual(await event(req, 'data'), 'A'.repeat(40));
261cb0ef41Sopenharmony_ci    await event(stream, 'drain');
271cb0ef41Sopenharmony_ci    assert.strictEqual(stream.write('A'.repeat(5)), true);
281cb0ef41Sopenharmony_ci    assert.strictEqual(stream.write('A'.repeat(40)), false);
291cb0ef41Sopenharmony_ci  }));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = makeDuplexPair();
321cb0ef41Sopenharmony_ci  server.emit('connection', serverSide);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const client = http2.connect('http://localhost:80', {
351cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide)
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  req = client.request({ ':path': '/' });
391cb0ef41Sopenharmony_ci  req.setEncoding('utf8');
401cb0ef41Sopenharmony_ci  req.end();
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifunction event(ee, eventName) {
441cb0ef41Sopenharmony_ci  return new Promise((resolve) => {
451cb0ef41Sopenharmony_ci    ee.once(eventName, common.mustCall(resolve));
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cifunction mustCallAsync(fn, exact) {
501cb0ef41Sopenharmony_ci  return common.mustCall((...args) => {
511cb0ef41Sopenharmony_ci    return Promise.resolve(fn(...args)).then(common.mustCall((val) => val));
521cb0ef41Sopenharmony_ci  }, exact);
531cb0ef41Sopenharmony_ci}
54