UNPKG

3.22 kBJavaScriptView Raw
1var tape = require('tape')
2var through = require('through2')
3var pumpify = require('./')
4var stream = require('stream')
5
6tape('basic', function(t) {
7 t.plan(3)
8
9 var pipeline = pumpify(
10 through(function(data, enc, cb) {
11 t.same(data.toString(), 'hello')
12 cb(null, data.toString().toUpperCase())
13 }),
14 through(function(data, enc, cb) {
15 t.same(data.toString(), 'HELLO')
16 cb(null, data.toString().toLowerCase())
17 })
18 )
19
20 pipeline.write('hello')
21 pipeline.on('data', function(data) {
22 t.same(data.toString(), 'hello')
23 t.end()
24 })
25})
26
27tape('3 times', function(t) {
28 t.plan(4)
29
30 var pipeline = pumpify(
31 through(function(data, enc, cb) {
32 t.same(data.toString(), 'hello')
33 cb(null, data.toString().toUpperCase())
34 }),
35 through(function(data, enc, cb) {
36 t.same(data.toString(), 'HELLO')
37 cb(null, data.toString().toLowerCase())
38 }),
39 through(function(data, enc, cb) {
40 t.same(data.toString(), 'hello')
41 cb(null, data.toString().toUpperCase())
42 })
43 )
44
45 pipeline.write('hello')
46 pipeline.on('data', function(data) {
47 t.same(data.toString(), 'HELLO')
48 t.end()
49 })
50})
51
52tape('destroy', function(t) {
53 var test = through()
54 test.destroy = function() {
55 t.ok(true)
56 t.end()
57 }
58
59 var pipeline = pumpify(through(), test)
60
61 pipeline.destroy()
62})
63
64tape('close', function(t) {
65 var test = through()
66 var pipeline = pumpify(through(), test)
67
68 pipeline.on('error', function(err) {
69 t.same(err.message, 'lol')
70 t.end()
71 })
72
73 test.emit('error', new Error('lol'))
74})
75
76tape('end waits for last one', function(t) {
77 var ran = false
78
79 var a = through()
80 var b = through()
81 var c = through(function(data, enc, cb) {
82 setTimeout(function() {
83 ran = true
84 cb()
85 }, 100)
86 })
87
88 var pipeline = pumpify(a, b, c)
89
90 pipeline.write('foo')
91 pipeline.end(function() {
92 t.ok(ran)
93 t.end()
94 })
95
96 t.ok(!ran)
97})
98
99tape('always wait for finish', function(t) {
100 var a = new stream.Readable()
101 a._read = function() {}
102 a.push('hello')
103
104 var pipeline = pumpify(a, through(), through())
105 var ran = false
106
107 pipeline.on('finish', function() {
108 t.ok(ran)
109 t.end()
110 })
111
112 setTimeout(function() {
113 ran = true
114 a.push(null)
115 }, 100)
116})
117
118tape('async', function(t) {
119 var pipeline = pumpify()
120
121 t.plan(4)
122
123 pipeline.write('hello')
124 pipeline.on('data', function(data) {
125 t.same(data.toString(), 'HELLO')
126 t.end()
127 })
128
129 setTimeout(function() {
130 pipeline.setPipeline(
131 through(function(data, enc, cb) {
132 t.same(data.toString(), 'hello')
133 cb(null, data.toString().toUpperCase())
134 }),
135 through(function(data, enc, cb) {
136 t.same(data.toString(), 'HELLO')
137 cb(null, data.toString().toLowerCase())
138 }),
139 through(function(data, enc, cb) {
140 t.same(data.toString(), 'hello')
141 cb(null, data.toString().toUpperCase())
142 })
143 )
144 }, 100)
145})
146
147tape('early destroy', function(t) {
148 var a = through()
149 var b = through()
150 var c = through()
151
152 b.destroy = function() {
153 t.ok(true)
154 t.end()
155 }
156
157 var pipeline = pumpify()
158
159 pipeline.destroy()
160 setTimeout(function() {
161 pipeline.setPipeline(a, b, c)
162 }, 100)
163})
\No newline at end of file