|
| 1 | +var common = require('../common'); |
| 2 | +var assert = require('assert'); |
| 3 | + |
| 4 | +// TODO Improved this test. test_ca.pem is too small. A proper test would |
| 5 | +// great a large utf8 (with multibyte chars) file and stream it in, |
| 6 | +// performing sanity checks throughout. |
| 7 | + |
| 8 | +var path = require('path'); |
| 9 | +var fs = require('fs'); |
| 10 | +var fn = path.join(common.fixturesDir, 'elipses.txt'); |
| 11 | +var rangeFile = path.join(common.fixturesDir, 'x.txt'); |
| 12 | + |
| 13 | +var callbacks = { open: 0, end: 0, close: 0 }; |
| 14 | + |
| 15 | +var paused = false; |
| 16 | + |
| 17 | +var file = fs.ReadStream(fn); |
| 18 | + |
| 19 | +file.on('open', function(fd) { |
| 20 | + file.length = 0; |
| 21 | + callbacks.open++; |
| 22 | + assert.equal('number', typeof fd); |
| 23 | + assert.ok(file.readable); |
| 24 | + |
| 25 | + // GH-535 |
| 26 | + file.pause(); |
| 27 | + file.resume(); |
| 28 | + file.pause(); |
| 29 | + file.resume(); |
| 30 | +}); |
| 31 | + |
| 32 | +file.on('data', function(data) { |
| 33 | + assert.ok(data instanceof Buffer); |
| 34 | + assert.ok(!paused); |
| 35 | + file.length += data.length; |
| 36 | + |
| 37 | + paused = true; |
| 38 | + file.pause(); |
| 39 | + |
| 40 | + setTimeout(function() { |
| 41 | + paused = false; |
| 42 | + file.resume(); |
| 43 | + }, 10); |
| 44 | +}); |
| 45 | + |
| 46 | + |
| 47 | +file.on('end', function(chunk) { |
| 48 | + callbacks.end++; |
| 49 | +}); |
| 50 | + |
| 51 | + |
| 52 | +file.on('close', function() { |
| 53 | + callbacks.close++; |
| 54 | + |
| 55 | + //assert.equal(fs.readFileSync(fn), fileContent); |
| 56 | +}); |
| 57 | + |
| 58 | +var file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'})); |
| 59 | +file3.length = 0; |
| 60 | +file3.on('data', function(data) { |
| 61 | + assert.equal('string', typeof(data)); |
| 62 | + file3.length += data.length; |
| 63 | + |
| 64 | + for (var i = 0; i < data.length; i++) { |
| 65 | + // http://www.fileformat.info/info/unicode/char/2026/index.htm |
| 66 | + assert.equal('\u2026', data[i]); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +file3.on('close', function() { |
| 71 | + callbacks.close++; |
| 72 | +}); |
| 73 | + |
| 74 | +process.on('exit', function() { |
| 75 | + assert.equal(1, callbacks.open); |
| 76 | + assert.equal(1, callbacks.end); |
| 77 | + assert.equal(2, callbacks.close); |
| 78 | + assert.equal(30000, file.length); |
| 79 | + assert.equal(10000, file3.length); |
| 80 | + console.error('ok'); |
| 81 | +}); |
| 82 | + |
| 83 | +var file4 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, start: 1, end: 2})); |
| 84 | +assert.equal(file4.start, 1); |
| 85 | +assert.equal(file4.end, 2); |
| 86 | +var contentRead = ''; |
| 87 | +file4.on('data', function(data) { |
| 88 | + contentRead += data.toString('utf-8'); |
| 89 | +}); |
| 90 | +file4.on('end', function(data) { |
| 91 | + assert.equal(contentRead, 'yz'); |
| 92 | +}); |
| 93 | + |
| 94 | +var file5 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, start: 1})); |
| 95 | +assert.equal(file5.start, 1); |
| 96 | +file5.data = ''; |
| 97 | +file5.on('data', function(data) { |
| 98 | + file5.data += data.toString('utf-8'); |
| 99 | +}); |
| 100 | +file5.on('end', function() { |
| 101 | + assert.equal(file5.data, 'yz\n'); |
| 102 | +}); |
| 103 | + |
| 104 | +// https://github.com/joyent/node/issues/2320 |
| 105 | +var file6 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1.23, start: 1})); |
| 106 | +assert.equal(file6.start, 1); |
| 107 | +file6.data = ''; |
| 108 | +file6.on('data', function(data) { |
| 109 | + file6.data += data.toString('utf-8'); |
| 110 | +}); |
| 111 | +file6.on('end', function() { |
| 112 | + assert.equal(file6.data, 'yz\n'); |
| 113 | +}); |
| 114 | + |
| 115 | +assert.throws(function() { |
| 116 | + fs.createReadStream(rangeFile, Object.create({start: 10, end: 2})); |
| 117 | +}, /start must be <= end/); |
| 118 | + |
| 119 | +var stream = fs.createReadStream(rangeFile, Object.create({ start: 0, end: 0 })); |
| 120 | +assert.equal(stream.start, 0); |
| 121 | +assert.equal(stream.end, 0); |
| 122 | +stream.data = ''; |
| 123 | + |
| 124 | +stream.on('data', function(chunk) { |
| 125 | + stream.data += chunk; |
| 126 | +}); |
| 127 | + |
| 128 | +stream.on('end', function() { |
| 129 | + assert.equal('x', stream.data); |
| 130 | +}); |
| 131 | + |
| 132 | +// pause and then resume immediately. |
| 133 | +var pauseRes = fs.createReadStream(rangeFile); |
| 134 | +pauseRes.pause(); |
| 135 | +pauseRes.resume(); |
| 136 | + |
| 137 | +var file7 = fs.createReadStream(rangeFile, Object.create({autoClose: false })); |
| 138 | +assert.equal(file7.autoClose, false); |
| 139 | +file7.on('data', function() {}); |
| 140 | +file7.on('end', function() { |
| 141 | + process.nextTick(function() { |
| 142 | + assert(!file7.closed); |
| 143 | + assert(!file7.destroyed); |
| 144 | + file7Next(); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +function file7Next(){ |
| 149 | + // This will tell us if the fd is usable again or not. |
| 150 | + file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 })); |
| 151 | + file7.data = ''; |
| 152 | + file7.on('data', function(data) { |
| 153 | + file7.data += data; |
| 154 | + }); |
| 155 | + file7.on('end', function(err) { |
| 156 | + assert.equal(file7.data, 'xyz\n'); |
| 157 | + }); |
| 158 | +} |
| 159 | + |
| 160 | +// Just to make sure autoClose won't close the stream because of error. |
| 161 | +var file8 = fs.createReadStream(null, Object.create({fd: 13337, autoClose: false })); |
| 162 | +file8.on('data', function() {}); |
| 163 | +file8.on('error', common.mustCall(function() {})); |
| 164 | + |
| 165 | +// Make sure stream is destroyed when file does not exist. |
| 166 | +var file9 = fs.createReadStream('/path/to/file/that/does/not/exist'); |
| 167 | +file9.on('data', function() {}); |
| 168 | +file9.on('error', common.mustCall(function() {})); |
| 169 | + |
| 170 | +process.on('exit', function() { |
| 171 | + assert(file7.closed); |
| 172 | + assert(file7.destroyed); |
| 173 | + |
| 174 | + assert(!file8.closed); |
| 175 | + assert(!file8.destroyed); |
| 176 | + assert(file8.fd); |
| 177 | + |
| 178 | + assert(!file9.closed); |
| 179 | + assert(file9.destroyed); |
| 180 | +}); |
0 commit comments