-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
95 lines (70 loc) · 2.5 KB
/
test.js
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
'use strict';
const stream = require('stream');
const DequeReadableStream = require('./deque-readable-stream');
const CHUNK_SIZE = 1024 * 4096;
const QUEUE_BUFFER_SIZE = 32 * 4096;
const MAX_IN_BYTES = 256 * 1024 * 1024;
describe('Deque readable stream', function () {
before(function () {
this.dataChunk = new Array(CHUNK_SIZE).fill('X').join('');
});
it('should pipe data', function (done) {
let inByteCounter = 0;
let outByteCounter = 0;
const dequeReadableStream = new DequeReadableStream({bufferSize: QUEUE_BUFFER_SIZE});
const writeableStream = new stream.Writable({
write(chunk, encoding, callback) {
outByteCounter += chunk.length;
callback();
}
});
stream.pipeline(dequeReadableStream, writeableStream, err => {
if (err) return done(err);
console.log({
inBytes: inByteCounter,
outBytes: outByteCounter,
});
done();
});
const writeInterval = setInterval(() => {
dequeReadableStream.write(this.dataChunk);
inByteCounter += this.dataChunk.length;
if (inByteCounter > MAX_IN_BYTES) {
dequeReadableStream.end();
clearInterval(writeInterval);
}
}, 10);
});
it('should keep data in order', function (done) {
let inCounter = 0;
let outCounter = 0;
const dequeReadableStream = new DequeReadableStream({bufferSize: QUEUE_BUFFER_SIZE});
const writeableStream = new stream.Writable({
write(chunk, encoding, callback) {
outCounter++;
if (parseInt(chunk) !== outCounter) {
done(new Error(`chunk (${chunk}) !== outCounter (${outCounter})`));
} else {
callback();
}
}
});
stream.pipeline(dequeReadableStream, writeableStream, err => {
if (err) return done(err);
console.log({
inCounter,
outCounter
});
done();
});
const writeInterval = setInterval(() => {
for (let i = 0; i < 10000; i++) {
dequeReadableStream.write((++inCounter).toString());
}
if (inCounter > 500000) {
dequeReadableStream.end();
clearInterval(writeInterval);
}
}, 10);
})
});