1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const fs = require('fs'); 25const assert = require('assert'); 26const join = require('path').join; 27 28const tmpdir = require('../common/tmpdir'); 29 30const filename = join(tmpdir.path, 'out.txt'); 31 32tmpdir.refresh(); 33 34const fd = fs.openSync(filename, 'w'); 35 36const line = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n'; 37 38const N = 10240; 39let complete = 0; 40 41for (let i = 0; i < N; i++) { 42 // Create a new buffer for each write. Before the write is actually 43 // executed by the thread pool, the buffer will be collected. 44 const buffer = Buffer.from(line); 45 fs.write(fd, buffer, 0, buffer.length, null, function(er, written) { 46 complete++; 47 if (complete === N) { 48 fs.closeSync(fd); 49 const s = fs.createReadStream(filename); 50 s.on('data', testBuffer); 51 } 52 }); 53} 54 55let bytesChecked = 0; 56 57function testBuffer(b) { 58 for (let i = 0; i < b.length; i++) { 59 bytesChecked++; 60 if (b[i] !== 'a'.charCodeAt(0) && b[i] !== '\n'.charCodeAt(0)) { 61 throw new Error(`invalid char ${i},${b[i]}`); 62 } 63 } 64} 65 66process.on('exit', function() { 67 // Probably some of the writes are going to overlap, so we can't assume 68 // that we get (N * line.length). Let's just make sure we've checked a 69 // few... 70 assert.ok(bytesChecked > 1000); 71}); 72