@@ -47,15 +47,17 @@ function Readable() {
47
47
}
48
48
util . inherits ( Readable , stream . Stream ) ;
49
49
50
+ function Duplex ( ) {
51
+ this . readable = true ;
52
+ Writable . call ( this ) ;
53
+ }
54
+ util . inherits ( Duplex , Writable ) ;
55
+
50
56
var i = 0 ;
51
57
var limit = 100 ;
52
58
53
59
var w = new Writable ( ) ;
54
60
55
- console . error = function ( text ) {
56
- throw new Error ( text ) ;
57
- } ;
58
-
59
61
var r ;
60
62
61
63
for ( i = 0 ; i < limit ; i ++ ) {
@@ -80,17 +82,41 @@ w.endCalls = 0;
80
82
81
83
r = new Readable ( ) ;
82
84
83
- for ( i = 0 ; i < limit ; i ++ ) {
84
- w = new Writable ( ) ;
85
- r . pipe ( w ) ;
86
- w . emit ( 'end' ) ;
87
- }
88
- assert . equal ( 0 , w . listeners ( 'end' ) . length ) ;
89
-
90
85
for ( i = 0 ; i < limit ; i ++ ) {
91
86
w = new Writable ( ) ;
92
87
r . pipe ( w ) ;
93
88
w . emit ( 'close' ) ;
94
89
}
95
90
assert . equal ( 0 , w . listeners ( 'close' ) . length ) ;
96
91
92
+ r = new Readable ( ) ;
93
+ w = new Writable ( ) ;
94
+ var d = new Duplex ( ) ;
95
+ r . pipe ( d ) ; // pipeline A
96
+ d . pipe ( w ) ; // pipeline B
97
+ assert . equal ( r . listeners ( 'end' ) . length , 2 ) ; // A.onend, A.cleanup
98
+ assert . equal ( r . listeners ( 'close' ) . length , 2 ) ; // A.onclose, A.cleanup
99
+ assert . equal ( d . listeners ( 'end' ) . length , 2 ) ; // B.onend, B.cleanup
100
+ assert . equal ( d . listeners ( 'close' ) . length , 3 ) ; // A.cleanup, B.onclose, B.cleanup
101
+ assert . equal ( w . listeners ( 'end' ) . length , 0 ) ;
102
+ assert . equal ( w . listeners ( 'close' ) . length , 1 ) ; // B.cleanup
103
+
104
+ r . emit ( 'end' ) ;
105
+ assert . equal ( d . endCalls , 1 ) ;
106
+ assert . equal ( w . endCalls , 0 ) ;
107
+ assert . equal ( r . listeners ( 'end' ) . length , 0 ) ;
108
+ assert . equal ( r . listeners ( 'close' ) . length , 0 ) ;
109
+ assert . equal ( d . listeners ( 'end' ) . length , 2 ) ; // B.onend, B.cleanup
110
+ assert . equal ( d . listeners ( 'close' ) . length , 2 ) ; // B.onclose, B.cleanup
111
+ assert . equal ( w . listeners ( 'end' ) . length , 0 ) ;
112
+ assert . equal ( w . listeners ( 'close' ) . length , 1 ) ; // B.cleanup
113
+
114
+ d . emit ( 'end' ) ;
115
+ assert . equal ( d . endCalls , 1 ) ;
116
+ assert . equal ( w . endCalls , 1 ) ;
117
+ assert . equal ( r . listeners ( 'end' ) . length , 0 ) ;
118
+ assert . equal ( r . listeners ( 'close' ) . length , 0 ) ;
119
+ assert . equal ( d . listeners ( 'end' ) . length , 0 ) ;
120
+ assert . equal ( d . listeners ( 'close' ) . length , 0 ) ;
121
+ assert . equal ( w . listeners ( 'end' ) . length , 0 ) ;
122
+ assert . equal ( w . listeners ( 'close' ) . length , 0 ) ;
0 commit comments