1 | var stream = require('stream')
|
---|
2 | var pump = require('./index')
|
---|
3 |
|
---|
4 | var rs = new stream.Readable()
|
---|
5 | var ws = new stream.Writable()
|
---|
6 |
|
---|
7 | rs._read = function (size) {
|
---|
8 | this.push(Buffer(size).fill('abc'))
|
---|
9 | }
|
---|
10 |
|
---|
11 | ws._write = function (chunk, encoding, cb) {
|
---|
12 | setTimeout(function () {
|
---|
13 | cb()
|
---|
14 | }, 100)
|
---|
15 | }
|
---|
16 |
|
---|
17 | var toHex = function () {
|
---|
18 | var reverse = new (require('stream').Transform)()
|
---|
19 |
|
---|
20 | reverse._transform = function (chunk, enc, callback) {
|
---|
21 | reverse.push(chunk.toString('hex'))
|
---|
22 | callback()
|
---|
23 | }
|
---|
24 |
|
---|
25 | return reverse
|
---|
26 | }
|
---|
27 |
|
---|
28 | var wsClosed = false
|
---|
29 | var rsClosed = false
|
---|
30 | var callbackCalled = false
|
---|
31 |
|
---|
32 | var check = function () {
|
---|
33 | if (wsClosed && rsClosed && callbackCalled) {
|
---|
34 | console.log('test-browser.js passes')
|
---|
35 | clearTimeout(timeout)
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | ws.on('finish', function () {
|
---|
40 | wsClosed = true
|
---|
41 | check()
|
---|
42 | })
|
---|
43 |
|
---|
44 | rs.on('end', function () {
|
---|
45 | rsClosed = true
|
---|
46 | check()
|
---|
47 | })
|
---|
48 |
|
---|
49 | var res = pump(rs, toHex(), toHex(), toHex(), ws, function () {
|
---|
50 | callbackCalled = true
|
---|
51 | check()
|
---|
52 | })
|
---|
53 |
|
---|
54 | if (res !== ws) {
|
---|
55 | throw new Error('should return last stream')
|
---|
56 | }
|
---|
57 |
|
---|
58 | setTimeout(function () {
|
---|
59 | rs.push(null)
|
---|
60 | rs.emit('close')
|
---|
61 | }, 1000)
|
---|
62 |
|
---|
63 | var timeout = setTimeout(function () {
|
---|
64 | check()
|
---|
65 | throw new Error('timeout')
|
---|
66 | }, 5000)
|
---|