test.js
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var fdSlicer = require('../');
var fs = require('fs');
var crypto = require('crypto');
var path = require('path');
var streamEqual = require('stream-equal');
var assert = require('assert');
var Pend = require('pend');
var StreamSink = require('streamsink');
var describe = global.describe;
var it = global.it;
var before = global.before;
var beforeEach = global.beforeEach;
var after = global.after;
var testBlobFile = path.join(__dirname, "test-blob.bin");
var testBlobFileSize = 20 * 1024 * 1024;
var testOutBlobFile = path.join(__dirname, "test-blob-out.bin");
describe("FdSlicer", function() {
before(function(done) {
var out = fs.createWriteStream(testBlobFile);
for (var i = 0; i < testBlobFileSize / 1024; i += 1) {
out.write(crypto.pseudoRandomBytes(1024));
}
out.end();
out.on('close', done);
});
beforeEach(function() {
try {
fs.unlinkSync(testOutBlobFile);
} catch (err) {
}
});
after(function() {
try {
fs.unlinkSync(testBlobFile);
fs.unlinkSync(testOutBlobFile);
} catch (err) {
}
});
it("reads a 20MB file (autoClose on)", function(done) {
fs.open(testBlobFile, 'r', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var actualStream = slicer.createReadStream();
var expectedStream = fs.createReadStream(testBlobFile);
var pend = new Pend();
pend.go(function(cb) {
slicer.on('close', cb);
});
pend.go(function(cb) {
streamEqual(expectedStream, actualStream, function(err, equal) {
if (err) return done(err);
assert.ok(equal);
cb();
});
});
pend.wait(done);
});
});
it("reads 4 chunks simultaneously", function(done) {
fs.open(testBlobFile, 'r', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd);
var actualPart1 = slicer.createReadStream({start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
var actualPart2 = slicer.createReadStream({start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
var actualPart3 = slicer.createReadStream({start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
var actualPart4 = slicer.createReadStream({start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
var expectedPart1 = slicer.createReadStream({start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
var expectedPart2 = slicer.createReadStream({start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
var expectedPart3 = slicer.createReadStream({start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
var expectedPart4 = slicer.createReadStream({start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
var pend = new Pend();
pend.go(function(cb) {
streamEqual(expectedPart1, actualPart1, function(err, equal) {
assert.ok(equal);
cb(err);
});
});
pend.go(function(cb) {
streamEqual(expectedPart2, actualPart2, function(err, equal) {
assert.ok(equal);
cb(err);
});
});
pend.go(function(cb) {
streamEqual(expectedPart3, actualPart3, function(err, equal) {
assert.ok(equal);
cb(err);
});
});
pend.go(function(cb) {
streamEqual(expectedPart4, actualPart4, function(err, equal) {
assert.ok(equal);
cb(err);
});
});
pend.wait(function(err) {
if (err) return done(err);
fs.close(fd, done);
});
});
});
it("writes a 20MB file (autoClose on)", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var actualStream = slicer.createWriteStream();
var inStream = fs.createReadStream(testBlobFile);
slicer.on('close', function() {
var expected = fs.createReadStream(testBlobFile);
var actual = fs.createReadStream(testOutBlobFile);
streamEqual(expected, actual, function(err, equal) {
if (err) return done(err);
assert.ok(equal);
done();
});
});
inStream.pipe(actualStream);
});
});
it("writes 4 chunks simultaneously", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd);
var actualPart1 = slicer.createWriteStream({start: testBlobFileSize * 0/4});
var actualPart2 = slicer.createWriteStream({start: testBlobFileSize * 1/4});
var actualPart3 = slicer.createWriteStream({start: testBlobFileSize * 2/4});
var actualPart4 = slicer.createWriteStream({start: testBlobFileSize * 3/4});
var in1 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 0/4, end: testBlobFileSize * 1/4});
var in2 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 1/4, end: testBlobFileSize * 2/4});
var in3 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 2/4, end: testBlobFileSize * 3/4});
var in4 = fs.createReadStream(testBlobFile, {start: testBlobFileSize * 3/4, end: testBlobFileSize * 4/4});
var pend = new Pend();
pend.go(function(cb) {
actualPart1.on('finish', cb);
});
pend.go(function(cb) {
actualPart2.on('finish', cb);
});
pend.go(function(cb) {
actualPart3.on('finish', cb);
});
pend.go(function(cb) {
actualPart4.on('finish', cb);
});
in1.pipe(actualPart1);
in2.pipe(actualPart2);
in3.pipe(actualPart3);
in4.pipe(actualPart4);
pend.wait(function() {
fs.close(fd, function(err) {
if (err) return done(err);
var expected = fs.createReadStream(testBlobFile);
var actual = fs.createReadStream(testOutBlobFile);
streamEqual(expected, actual, function(err, equal) {
if (err) return done(err);
assert.ok(equal);
done();
});
});
});
});
});
it("throws on invalid ref", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
assert.throws(function() {
slicer.unref();
}, /invalid unref/);
fs.close(fd, done);
});
});
it("write stream emits error when max size exceeded", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var ws = slicer.createWriteStream({start: 0, end: 1000});
ws.on('error', function(err) {
assert.strictEqual(err.code, 'ETOOBIG');
slicer.on('close', done);
});
ws.end(new Buffer(1001));
});
});
it("write stream does not emit error when max size not exceeded", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var ws = slicer.createWriteStream({end: 1000});
slicer.on('close', done);
ws.end(new Buffer(1000));
});
});
it("write stream start and end work together", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var ws = slicer.createWriteStream({start: 1, end: 1000});
ws.on('error', function(err) {
assert.strictEqual(err.code, 'ETOOBIG');
slicer.on('close', done);
});
ws.end(new Buffer(1000));
});
});
it("write stream emits progress events", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var ws = slicer.createWriteStream();
var progressEventCount = 0;
var prevBytesWritten = 0;
ws.on('progress', function() {
progressEventCount += 1;
assert.ok(ws.bytesWritten > prevBytesWritten);
prevBytesWritten = ws.bytesWritten;
});
slicer.on('close', function() {
assert.ok(progressEventCount > 5);
done();
});
for (var i = 0; i < 10; i += 1) {
ws.write(new Buffer(16 * 1024 * 2));
}
ws.end();
});
});
it("write stream unrefs when destroyed", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var ws = slicer.createWriteStream();
slicer.on('close', done);
ws.write(new Buffer(1000));
ws.destroy();
});
});
it("read stream unrefs when destroyed", function(done) {
fs.open(testBlobFile, 'r', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd, {autoClose: true});
var rs = slicer.createReadStream();
rs.on('error', function(err) {
assert.strictEqual(err.message, "stream destroyed");
slicer.on('close', done);
});
rs.destroy();
});
});
it("fdSlicer.read", function(done) {
fs.open(testBlobFile, 'r', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd);
var outBuf = new Buffer(1024);
slicer.read(outBuf, 0, 10, 0, function(err, bytesRead, buf) {
assert.strictEqual(bytesRead, 10);
fs.close(fd, done);
});
});
});
it("fdSlicer.write", function(done) {
fs.open(testOutBlobFile, 'w', function(err, fd) {
if (err) return done(err);
var slicer = fdSlicer.createFromFd(fd);
slicer.write(new Buffer("blah\n"), 0, 5, 0, function() {
if (err) return done(err);
fs.close(fd, done);
});
});
});
});
describe("BufferSlicer", function() {
it("invalid ref", function() {
var slicer = fdSlicer.createFromBuffer(new Buffer(16));
slicer.ref();
slicer.unref();
assert.throws(function() {
slicer.unref();
}, /invalid unref/);
});
it("read and write", function(done) {
var buf = new Buffer("through the tangled thread the needle finds its way");
var slicer = fdSlicer.createFromBuffer(buf);
var outBuf = new Buffer(1024);
slicer.read(outBuf, 10, 11, 8, function(err) {
if (err) return done(err);
assert.strictEqual(outBuf.toString('utf8', 10, 21), "the tangled");
slicer.write(new Buffer("derp"), 0, 4, 7, function(err) {
if (err) return done(err);
assert.strictEqual(buf.toString('utf8', 7, 19), "derp tangled");
done();
});
});
});
it("createReadStream", function(done) {
var str = "I never conquered rarely came, 16 just held such better days";
var buf = new Buffer(str);
var slicer = fdSlicer.createFromBuffer(buf);
var inStream = slicer.createReadStream();
var sink = new StreamSink();
inStream.pipe(sink);
sink.on('finish', function() {
assert.strictEqual(sink.toString(), str);
inStream.destroy();
done();
});
});
it("createWriteStream exceed buffer size", function(done) {
var slicer = fdSlicer.createFromBuffer(new Buffer(4));
var outStream = slicer.createWriteStream();
outStream.on('error', function(err) {
assert.strictEqual(err.code, 'ETOOBIG');
done();
});
outStream.write("hi!\n");
outStream.write("it warked\n");
outStream.end();
});
it("createWriteStream ok", function(done) {
var buf = new Buffer(1024);
var slicer = fdSlicer.createFromBuffer(buf);
var outStream = slicer.createWriteStream();
outStream.on('finish', function() {
assert.strictEqual(buf.toString('utf8', 0, "hi!\nit warked\n".length), "hi!\nit warked\n");
outStream.destroy();
done();
});
outStream.write("hi!\n");
outStream.write("it warked\n");
outStream.end();
});
});