| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 12cb0fb commit 54a4c6a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + // Flags: --expose-gc | ||
| 2 | + 'use strict'; | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const tmpdir = require('../common/tmpdir'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const path = require('path'); | ||
| 7 | + | ||
| 8 | + // Regression test for https://github.com/nodejs/node-v0.x-archive/issues/814: | ||
| 9 | + // Make sure that Buffers passed to fs.write() are not garbage-collected | ||
| 10 | + // even when the callback is being reused. | ||
| 11 | + | ||
| 12 | + const fs = require('fs'); | ||
| 13 | + | ||
| 14 | + tmpdir.refresh(); | ||
| 15 | + const filename = path.join(tmpdir.path, 'test.txt'); | ||
| 16 | + const fd = fs.openSync(filename, 'w'); | ||
| 17 | + | ||
| 18 | + const size = 16 * 1024; | ||
| 19 | + const writes = 1000; | ||
| 20 | + let done = 0; | ||
| 21 | + | ||
| 22 | + const ondone = common.mustCall((err) => { | ||
| 23 | + assert.ifError(err); | ||
| 24 | + if (++done < writes) { | ||
| 25 | + if (done % 25 === 0) global.gc(); | ||
| 26 | + setImmediate(write); | ||
| 27 | + } else { | ||
| 28 | + assert.strictEqual( | ||
| 29 | + fs.readFileSync(filename, 'utf8'), | ||
| 30 | + 'x'.repeat(writes * size)); | ||
| 31 | + fs.closeSync(fd); | ||
| 32 | + } | ||
| 33 | + }, writes); | ||
| 34 | + | ||
| 35 | + write(); | ||
| 36 | + function write() { | ||
| 37 | + const buf = Buffer.alloc(size, 'x'); | ||
| 38 | + fs.write(fd, buf, 0, buf.size, -1, ondone); | ||
| 39 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments