11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/814:
91cb0ef41Sopenharmony_ci// Make sure that Buffers passed to fs.write() are not garbage-collected
101cb0ef41Sopenharmony_ci// even when the callback is being reused.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ciconst filename = path.join(tmpdir.path, 'test.txt');
161cb0ef41Sopenharmony_ciconst fd = fs.openSync(filename, 'w');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst size = 16 * 1024;
191cb0ef41Sopenharmony_ciconst writes = 1000;
201cb0ef41Sopenharmony_cilet done = 0;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst ondone = common.mustSucceed(() => {
231cb0ef41Sopenharmony_ci  if (++done < writes) {
241cb0ef41Sopenharmony_ci    if (done % 25 === 0) global.gc();
251cb0ef41Sopenharmony_ci    setImmediate(write);
261cb0ef41Sopenharmony_ci  } else {
271cb0ef41Sopenharmony_ci    assert.strictEqual(
281cb0ef41Sopenharmony_ci      fs.readFileSync(filename, 'utf8'),
291cb0ef41Sopenharmony_ci      'x'.repeat(writes * size));
301cb0ef41Sopenharmony_ci    fs.closeSync(fd);
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci}, writes);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciwrite();
351cb0ef41Sopenharmony_cifunction write() {
361cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(size, 'x');
371cb0ef41Sopenharmony_ci  fs.write(fd, buf, 0, buf.length, -1, ondone);
381cb0ef41Sopenharmony_ci}
39