forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-stream-pump.js
More file actions
165 lines (143 loc) · 3.28 KB
/
test-stream-pump.js
File metadata and controls
165 lines (143 loc) · 3.28 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
'use strict';
const stream = require('stream');
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const crypto = require('crypto');
const pump = stream.pump;
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
test('basic pump', (t) => {
const rs = new stream.Readable({
read(size) {
this.push(crypto.randomBytes(size));
}
});
const ws = new stream.Writable({
write(chunk, enc, next) {
setImmediate(next);
}
});
function toHex() {
const reverse = new stream.Transform();
reverse._transform = function(chunk, enc, callback) {
reverse.push(chunk.toString('hex'));
callback();
};
return reverse;
}
let wsClosed = false;
let rsClosed = false;
let callbackCalled = false;
const timeout = setTimeout(function() {
throw new Error('timeout');
}, 5000);
function check() {
if (wsClosed && rsClosed && callbackCalled) {
clearTimeout(timeout);
t.end();
}
}
ws.on('finish', function() {
wsClosed = true;
check();
});
rs.on('end', function() {
rsClosed = true;
check();
});
pump(rs, toHex(), toHex(), toHex(), ws, function() {
callbackCalled = true;
check();
});
setTimeout(function() {
rs.push(null);
}, 1000);
});
test('call destroy if error', (t) => {
let wsDestroyCalled = false;
let rsDestroyCalled = false;
let callbackCalled = false;
class RS extends stream.Readable {
_read(size) {
this.push(crypto.randomBytes(size));
}
_destroy(err, cb) {
rsDestroyCalled = true;
check();
super._destroy(err, cb);
}
}
class WS extends stream.Writable {
_write(chunk, enc, next) {
setImmediate(next);
}
_destroy(error, cb) {
t.equal(error && error.message, 'lets end this');
this.emit('close');
wsDestroyCalled = true;
check();
super._destroy(error, cb);
}
}
const rs = new RS();
const ws = new WS();
function throwError() {
const reverse = new stream.Transform();
let i = 0;
reverse._transform = function(chunk, enc, callback) {
i++;
if (i > 5) {
return callback(new Error('lets end this'));
}
reverse.push(chunk.toString('hex'));
callback();
};
return reverse;
}
function toHex() {
const reverse = new stream.Transform();
reverse._transform = function(chunk, enc, callback) {
reverse.push(chunk.toString('hex'));
callback();
};
return reverse;
}
function check() {
if (wsDestroyCalled && rsDestroyCalled && callbackCalled) {
t.end();
}
}
pump(rs, toHex(), throwError(), toHex(), toHex(), ws, function(err) {
t.equal(err && err.message, 'lets end this');
callbackCalled = true;
check();
});
});