-
Notifications
You must be signed in to change notification settings - Fork 30
/
test.js
283 lines (233 loc) · 6.2 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
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
var test = require('tape')
var concat = require('concat-stream')
var through = require('through2')
var multiplex = require('./')
var net = require('net')
var chunky = require('chunky')
test('one way piping work with 2 sub-streams', function (t) {
var plex1 = multiplex()
var stream1 = plex1.createStream()
var stream2 = plex1.createStream()
var plex2 = multiplex(function onStream (stream, id) {
stream.pipe(collect())
})
plex1.pipe(plex2)
stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
stream1.end()
stream2.end()
var pending = 2
var results = []
function collect () {
return concat(function (data) {
results.push(data.toString())
if (--pending === 0) {
results.sort()
t.equal(results[0].toString(), 'hello')
t.equal(results[1].toString(), 'world')
t.end()
}
})
}
})
test('two way piping works with 2 sub-streams', function (t) {
var plex1 = multiplex()
var plex2 = multiplex(function onStream (stream, id) {
var uppercaser = through(function (chunk, e, done) {
this.push(new Buffer(chunk.toString().toUpperCase()))
this.end()
done()
})
stream.pipe(uppercaser).pipe(stream)
})
plex1.pipe(plex2).pipe(plex1)
var stream1 = plex1.createStream()
var stream2 = plex1.createStream()
stream1.pipe(collect())
stream2.pipe(collect())
stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
var pending = 2
var results = []
function collect () {
return concat(function (data) {
results.push(data.toString())
if (--pending === 0) {
results.sort()
t.equal(results[0].toString(), 'HELLO')
t.equal(results[1].toString(), 'WORLD')
t.end()
}
})
}
})
test('stream id should be exposed as stream.name', function (t) {
var plex1 = multiplex()
var stream1 = plex1.createStream('5')
t.equal(stream1.name, '5')
var plex2 = multiplex(function onStream (stream, id) {
t.equal(stream.name, '5')
t.equal(id, '5')
t.end()
})
plex1.pipe(plex2)
stream1.write(new Buffer('hello'))
stream1.end()
})
test('stream id can be a long string', function (t) {
var plex1 = multiplex()
var stream1 = plex1.createStream('hello-yes-this-is-dog')
t.equal(stream1.name, 'hello-yes-this-is-dog')
var plex2 = multiplex(function onStream (stream, id) {
t.equal(stream.name, 'hello-yes-this-is-dog')
t.equal(id, 'hello-yes-this-is-dog')
t.end()
})
plex1.pipe(plex2)
stream1.write(new Buffer('hello'))
stream1.end()
})
test('destroy', function (t) {
var plex1 = multiplex()
var stream1 = plex1.createStream()
var plex2 = multiplex(function onStream (stream, id) {
stream.on('error', function (err) {
t.equal(err.message, '0 had an error')
t.end()
})
})
plex1.pipe(plex2)
stream1.write(new Buffer('hello'))
stream1.destroy(new Error('0 had an error'))
})
test('testing invalid data error', function (t) {
var plex = multiplex()
plex.on('error', function (err) {
if (err) {
t.equal(err.message, 'Incoming message is too big')
t.end()
}
})
// a really stupid thing to do
plex.write(Array(50000).join('\xff'))
})
test('overflow', function (t) {
var plex1 = multiplex()
var plex2 = multiplex({limit: 10})
plex2.on('error', function (err) {
if (err) {
t.equal(err.message, 'Incoming message is too big')
t.end()
}
})
plex1.pipe(plex2).pipe(plex1)
plex1.createStream().write(new Buffer(11))
})
test('2 buffers packed into 1 chunk', function (t) {
var plex1 = multiplex()
var plex2 = multiplex(function (b) {
b.pipe(concat(function (body) {
t.equal(body.toString('utf8'), 'abc\n123\n')
t.end()
server.close()
plex1.end()
}))
})
var a = plex1.createStream(1337)
a.write('abc\n')
a.write('123\n')
a.end()
var server = net.createServer(function (stream) {
plex2.pipe(stream).pipe(plex2)
})
server.listen(0, function () {
var port = server.address().port
plex1.pipe(net.connect(port)).pipe(plex1)
})
})
test('chunks', function (t) {
var times = 100
;(function chunk () {
var collect = collector(function () {
if (--times === 0) t.end()
else chunk()
})
var plex1 = multiplex()
var stream1 = plex1.createStream()
var stream2 = plex1.createStream()
var plex2 = multiplex(function onStream (stream, id) {
stream.pipe(collect())
})
plex1.pipe(through(function (buf, enc, next) {
var bufs = chunky(buf)
for (var i = 0; i < bufs.length; i++) this.push(bufs[i])
next()
})).pipe(plex2)
stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
stream1.end()
stream2.end()
})()
function collector (cb) {
var pending = 2
var results = []
return function () {
return concat(function (data) {
results.push(data.toString())
if (--pending === 0) {
results.sort()
t.equal(results[0].toString(), 'hello')
t.equal(results[1].toString(), 'world')
cb()
}
})
}
}
})
test('prefinish + corking', function (t) {
var plex = multiplex()
var async = false
plex.on('prefinish', function () {
plex.cork()
process.nextTick(function () {
async = true
plex.uncork()
})
})
plex.on('finish', function () {
t.ok(async, 'finished')
t.end()
})
plex.end()
})
test('quick message', function (t) {
var plex2 = multiplex()
var plex1 = multiplex(function (stream) {
stream.write('hello world')
})
plex1.pipe(plex2).pipe(plex1)
setTimeout(function () {
var stream = plex2.createStream()
stream.on('data', function (data) {
t.same(data, new Buffer('hello world'))
t.end()
})
}, 100)
})
test('if onstream is not passed, stream is emitted', function (t) {
var plex1 = multiplex()
var plex2 = multiplex()
plex1.pipe(plex2).pipe(plex1)
plex2.on('stream', function (stream, id) {
t.ok(stream, 'received stream')
t.ok(id, 'has id')
stream.write('hello world')
stream.end()
})
var stream = plex1.createStream()
stream.on('data', function (data) {
t.same(data, new Buffer('hello world'))
stream.end()
t.end()
})
})