11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// This test assesses whether long-running writes can complete
71cb0ef41Sopenharmony_ci// or timeout because the socket is not aware that the backing
81cb0ef41Sopenharmony_ci// stream is still writing.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst writeSize = 3000000;
111cb0ef41Sopenharmony_cilet socket;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustCall((req, res) => {
141cb0ef41Sopenharmony_ci  server.close();
151cb0ef41Sopenharmony_ci  const content = Buffer.alloc(writeSize, 0x44);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  res.writeHead(200, {
181cb0ef41Sopenharmony_ci    'Content-Type': 'application/octet-stream',
191cb0ef41Sopenharmony_ci    'Content-Length': content.length.toString(),
201cb0ef41Sopenharmony_ci    'Vary': 'Accept-Encoding'
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  socket = res.socket;
241cb0ef41Sopenharmony_ci  const onTimeout = socket._onTimeout;
251cb0ef41Sopenharmony_ci  socket._onTimeout = common.mustCallAtLeast(() => onTimeout.call(socket), 1);
261cb0ef41Sopenharmony_ci  res.write(content);
271cb0ef41Sopenharmony_ci  res.end();
281cb0ef41Sopenharmony_ci}));
291cb0ef41Sopenharmony_ciserver.on('timeout', () => {
301cb0ef41Sopenharmony_ci  // TODO(apapirovski): This test is faulty on certain Windows systems
311cb0ef41Sopenharmony_ci  // as no queue is ever created
321cb0ef41Sopenharmony_ci  assert(!socket._handle || socket._handle.writeQueueSize === 0,
331cb0ef41Sopenharmony_ci         'Should not timeout');
341cb0ef41Sopenharmony_ci});
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
371cb0ef41Sopenharmony_ci  http.get({
381cb0ef41Sopenharmony_ci    path: '/',
391cb0ef41Sopenharmony_ci    port: server.address().port
401cb0ef41Sopenharmony_ci  }, (res) => {
411cb0ef41Sopenharmony_ci    res.once('data', () => {
421cb0ef41Sopenharmony_ci      socket._onTimeout();
431cb0ef41Sopenharmony_ci      res.on('data', () => {});
441cb0ef41Sopenharmony_ci    });
451cb0ef41Sopenharmony_ci    res.on('end', () => server.close());
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci}));
48