11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Note: This is a variant of test-net-write-fully-async-hex-string.js.
51cb0ef41Sopenharmony_ci// This always worked, but it seemed appropriate to add a test that checks the
61cb0ef41Sopenharmony_ci// behavior for Buffers, too.
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst net = require('net');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst data = Buffer.alloc(1000000);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst server = net.createServer(common.mustCall(function(conn) {
131cb0ef41Sopenharmony_ci  conn.resume();
141cb0ef41Sopenharmony_ci})).listen(0, common.mustCall(function() {
151cb0ef41Sopenharmony_ci  const conn = net.createConnection(this.address().port, common.mustCall(() => {
161cb0ef41Sopenharmony_ci    let count = 0;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci    function writeLoop() {
191cb0ef41Sopenharmony_ci      if (count++ === 200) {
201cb0ef41Sopenharmony_ci        conn.destroy();
211cb0ef41Sopenharmony_ci        server.close();
221cb0ef41Sopenharmony_ci        return;
231cb0ef41Sopenharmony_ci      }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci      while (conn.write(Buffer.from(data)));
261cb0ef41Sopenharmony_ci      global.gc({ type: 'minor' });
271cb0ef41Sopenharmony_ci      // The buffer allocated above should still be alive.
281cb0ef41Sopenharmony_ci    }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    conn.on('drain', writeLoop);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    writeLoop();
331cb0ef41Sopenharmony_ci  }));
341cb0ef41Sopenharmony_ci}));
35