11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ciconst http = require('http');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// These test cases to check socketOnDrain where needPause becomes false.
81cb0ef41Sopenharmony_ci// When send large response enough to exceed highWaterMark, it expect the socket
91cb0ef41Sopenharmony_ci// to be paused and res.write would be failed.
101cb0ef41Sopenharmony_ci// And it should be resumed when outgoingData falls below highWaterMark.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cilet requestReceived = 0;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) {
151cb0ef41Sopenharmony_ci  const id = ++requestReceived;
161cb0ef41Sopenharmony_ci  const enoughToDrain = req.connection.writableHighWaterMark;
171cb0ef41Sopenharmony_ci  const body = 'x'.repeat(enoughToDrain * 100);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  if (id === 1) {
201cb0ef41Sopenharmony_ci    // Case of needParse = false
211cb0ef41Sopenharmony_ci    req.connection.once('pause', common.mustCall(() => {
221cb0ef41Sopenharmony_ci      assert(req.connection._paused, '_paused must be true because it exceeds' +
231cb0ef41Sopenharmony_ci                                     'highWaterMark by second request');
241cb0ef41Sopenharmony_ci    }));
251cb0ef41Sopenharmony_ci  } else {
261cb0ef41Sopenharmony_ci    // Case of needParse = true
271cb0ef41Sopenharmony_ci    const resume = req.connection.parser.resume.bind(req.connection.parser);
281cb0ef41Sopenharmony_ci    req.connection.parser.resume = common.mustCall((...args) => {
291cb0ef41Sopenharmony_ci      const paused = req.connection._paused;
301cb0ef41Sopenharmony_ci      assert(!paused, '_paused must be false because it become false by ' +
311cb0ef41Sopenharmony_ci                      'socketOnDrain when outgoingData falls below ' +
321cb0ef41Sopenharmony_ci                      'highWaterMark');
331cb0ef41Sopenharmony_ci      return resume(...args);
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci  assert(!res.write(body), 'res.write must return false because it will ' +
371cb0ef41Sopenharmony_ci                           'exceed highWaterMark on this call');
381cb0ef41Sopenharmony_ci  res.end();
391cb0ef41Sopenharmony_ci}).on('listening', () => {
401cb0ef41Sopenharmony_ci  const c = net.createConnection(server.address().port, () => {
411cb0ef41Sopenharmony_ci    c.write('GET / HTTP/1.1\r\n\r\n');
421cb0ef41Sopenharmony_ci    c.write('GET / HTTP/1.1\r\n\r\n',
431cb0ef41Sopenharmony_ci            () => setImmediate(() => c.resume()));
441cb0ef41Sopenharmony_ci    c.end();
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  c.on('end', () => {
481cb0ef41Sopenharmony_ci    server.close();
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci});
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciserver.listen(0);
53