3
3
const common = require ( '../common' ) ;
4
4
const stream = require ( 'stream' ) ;
5
5
const {
6
- Readable,
7
- Writable,
8
- promises,
6
+ Readable, Writable, promises,
9
7
} = stream ;
10
8
const {
11
- finished,
12
- pipeline,
9
+ finished, pipeline,
13
10
} = require ( 'stream/promises' ) ;
14
11
const fs = require ( 'fs' ) ;
15
12
const assert = require ( 'assert' ) ;
@@ -24,14 +21,11 @@ assert.strictEqual(finished, promisify(stream.finished));
24
21
{
25
22
let finished = false ;
26
23
const processed = [ ] ;
27
- const expected = [
28
- Buffer . from ( 'a' ) ,
29
- Buffer . from ( 'b' ) ,
30
- Buffer . from ( 'c' ) ,
31
- ] ;
24
+ const expected = [ Buffer . from ( 'a' ) , Buffer . from ( 'b' ) , Buffer . from ( 'c' ) ] ;
32
25
33
26
const read = new Readable ( {
34
- read ( ) { }
27
+ read ( ) {
28
+ }
35
29
} ) ;
36
30
37
31
const write = new Writable ( {
@@ -59,7 +53,8 @@ assert.strictEqual(finished, promisify(stream.finished));
59
53
// pipeline error
60
54
{
61
55
const read = new Readable ( {
62
- read ( ) { }
56
+ read ( ) {
57
+ }
63
58
} ) ;
64
59
65
60
const write = new Writable ( {
@@ -101,3 +96,50 @@ assert.strictEqual(finished, promisify(stream.finished));
101
96
code : 'ENOENT'
102
97
} ) . then ( common . mustCall ( ) ) ;
103
98
}
99
+
100
+ {
101
+ const streamObj = new Readable ( ) ;
102
+ assert . throws ( ( ) => {
103
+ // Passing cleanup option not as boolean
104
+ // should throw error
105
+ finished ( streamObj , { cleanup : 2 } ) ;
106
+ } , { code : 'ERR_INVALID_ARG_TYPE' } ) ;
107
+ }
108
+
109
+ // Below code should not throw any errors as the
110
+ // streamObj is `Stream` and cleanup is boolean
111
+ {
112
+ const streamObj = new Readable ( ) ;
113
+ finished ( streamObj , { cleanup : true } ) ;
114
+ }
115
+
116
+
117
+ // Cleanup function should not be called when cleanup is set to false
118
+ // listenerCount should be 1 after calling finish
119
+ {
120
+ const streamObj = new Writable ( ) ;
121
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
122
+ finished ( streamObj , { cleanup : false } ) . then ( ( ) => {
123
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 1 ) ;
124
+ } ) ;
125
+ }
126
+
127
+ // Cleanup function should be called when cleanup is set to true
128
+ // listenerCount should be 0 after calling finish
129
+ {
130
+ const streamObj = new Writable ( ) ;
131
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
132
+ finished ( streamObj , { cleanup : true } ) . then ( ( ) => {
133
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
134
+ } ) ;
135
+ }
136
+
137
+ // Cleanup function should not be called when cleanup has not been set
138
+ // listenerCount should be 1 after calling finish
139
+ {
140
+ const streamObj = new Writable ( ) ;
141
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
142
+ finished ( streamObj ) . then ( ( ) => {
143
+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 1 ) ;
144
+ } ) ;
145
+ }
0 commit comments