@@ -49,42 +49,77 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
49
49
50
50
// Test allowHalfOpen
51
51
{
52
- let counter = 0 ;
52
+ let clientReceivedFIN = 0 ;
53
+ let serverConnections = 0 ;
54
+ let clientSentFIN = 0 ;
55
+ let serverReceivedFIN = 0 ;
53
56
const server = net . createServer ( {
54
57
allowHalfOpen : true
55
58
} )
56
- . on ( 'connection' , forAllClients ( function ( socket ) {
59
+ . on ( 'connection' , forAllClients ( function serverOnConnection ( socket ) {
60
+ const serverConnection = ++ serverConnections ;
61
+ let clientId ;
62
+ console . error ( `${ serverConnections } 'connection' emitted on server` ) ;
57
63
socket . resume ( ) ;
58
64
// 'end' on each socket must not be emitted twice
59
- socket . on ( 'end' , common . mustCall ( function ( ) { } , 1 ) ) ;
65
+ socket . on ( 'data' , common . mustCall ( function ( data ) {
66
+ clientId = data . toString ( ) ;
67
+ console . error ( `${ serverConnection } server connection is started ` +
68
+ `by client No. ${ clientId } ` ) ;
69
+ } ) ) ;
70
+ socket . on ( 'end' , common . mustCall ( function ( ) {
71
+ serverReceivedFIN ++ ;
72
+ console . error ( `Server recieved FIN sent by No. ${ clientId } ` ) ;
73
+ if ( serverReceivedFIN === CLIENT_VARIANTS ) {
74
+ setTimeout ( ( ) => {
75
+ server . close ( ) ;
76
+ console . error ( `No. ${ clientId } connection is closing server: ` +
77
+ `${ serverReceivedFIN } FIN received by server, ` +
78
+ `${ clientReceivedFIN } FIN received by client, ` +
79
+ `${ clientSentFIN } FIN sent by client, ` +
80
+ `${ serverConnections } FIN sent by server` ) ;
81
+ } , 50 ) ;
82
+ }
83
+ } , 1 ) ) ;
60
84
socket . end ( ) ;
85
+ console . error ( `Server has sent ${ serverConnections } FIN` ) ;
86
+ } ) )
87
+ . on ( 'close' , common . mustCall ( function serverOnClose ( ) {
88
+ console . error ( 'Server has been closed: ' +
89
+ `${ serverReceivedFIN } FIN received by server, ` +
90
+ `${ clientReceivedFIN } FIN received by client, ` +
91
+ `${ clientSentFIN } FIN sent by client, ` +
92
+ `${ serverConnections } FIN sent by server` ) ;
61
93
} ) )
62
- . listen ( 0 , common . mustCall ( function ( ) {
94
+ . listen ( 0 , 'localhost' , common . mustCall ( function serverOnListen ( ) {
95
+ const host = 'localhost' ;
96
+ const port = server . address ( ) . port ;
97
+
98
+ console . error ( `Server starts at ${ host } :${ port } ` ) ;
63
99
const getSocketOpt = ( ) => ( { allowHalfOpen : true } ) ;
64
- const getConnectOpt = ( ) => ( {
65
- host : server . address ( ) . address ,
66
- port : server . address ( ) . port ,
67
- } ) ;
68
- const getConnectCb = ( ) => common . mustCall ( function ( ) {
100
+ const getConnectOpt = ( ) => ( { host, port } ) ;
101
+ const getConnectCb = ( index ) => common . mustCall ( function clientOnConnect ( ) {
69
102
const client = this ;
103
+ console . error ( `'connect' emitted on Client ${ index } ` ) ;
70
104
client . resume ( ) ;
71
- client . on ( 'end' , common . mustCall ( function ( ) {
105
+ client . on ( 'end' , common . mustCall ( function clientOnEnd ( ) {
72
106
setTimeout ( function ( ) {
73
107
// when allowHalfOpen is true, client must still be writable
74
108
// after the server closes the connections, but not readable
109
+ console . error ( `No. ${ index } client received FIN` ) ;
75
110
assert ( ! client . readable ) ;
76
111
assert ( client . writable ) ;
77
- assert ( client . write ( 'foo ') ) ;
112
+ assert ( client . write ( index + ' ') ) ;
78
113
client . end ( ) ;
114
+ clientSentFIN ++ ;
115
+ console . error ( `No. ${ index } client sent FIN, ` +
116
+ `${ clientSentFIN } have been sent` ) ;
79
117
} , 50 ) ;
80
118
} ) ) ;
81
- client . on ( 'close' , common . mustCall ( function ( ) {
82
- counter ++ ;
83
- if ( counter === CLIENT_VARIANTS ) {
84
- setTimeout ( ( ) => {
85
- server . close ( ) ;
86
- } , 50 ) ;
87
- }
119
+ client . on ( 'close' , common . mustCall ( function clientOnClose ( ) {
120
+ clientReceivedFIN ++ ;
121
+ console . error ( `No. ${ index } connection has been closed by both ` +
122
+ `sides, ${ clientReceivedFIN } clients have closed` ) ;
88
123
} ) ) ;
89
124
} ) ;
90
125
@@ -96,10 +131,18 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
96
131
if ( ! common . isWindows ) { // Doesn't support this on windows
97
132
let counter = 0 ;
98
133
const server = net . createServer ( )
99
- . on ( 'connection' , forAllClients ( function ( socket ) {
134
+ . on ( 'connection' , forAllClients ( function serverOnConnection ( socket ) {
100
135
socket . end ( 'ok' ) ;
136
+ socket . on ( 'end' , common . mustCall ( function ( ) {
137
+ counter ++ ;
138
+ if ( counter === CLIENT_VARIANTS ) {
139
+ setTimeout ( ( ) => {
140
+ server . close ( ) ;
141
+ } , 50 ) ;
142
+ }
143
+ } , 1 ) ) ;
101
144
} ) )
102
- . listen ( 0 , common . mustCall ( function ( ) {
145
+ . listen ( 0 , 'localhost' , common . mustCall ( function serverOnListen ( ) {
103
146
const handleMap = new Map ( ) ;
104
147
const getSocketOpt = ( index ) => {
105
148
const handle = new TCP ( ) ;
@@ -118,11 +161,11 @@ if (!common.isWindows) { // Doesn't support this on windows
118
161
} ;
119
162
120
163
const getConnectOpt = ( ) => ( {
121
- host : server . address ( ) . address ,
164
+ host : 'localhost' ,
122
165
port : server . address ( ) . port ,
123
166
} ) ;
124
167
125
- const getConnectCb = ( index ) => common . mustCall ( function ( ) {
168
+ const getConnectCb = ( index ) => common . mustCall ( function clientOnConnect ( ) {
126
169
const client = this ;
127
170
// Test if it's wrapping an existing fd
128
171
assert ( handleMap . has ( index ) ) ;
@@ -131,10 +174,6 @@ if (!common.isWindows) { // Doesn't support this on windows
131
174
client . end ( ) ;
132
175
client . on ( 'close' , common . mustCall ( function ( ) {
133
176
oldHandle . close ( ) ;
134
- counter ++ ;
135
- if ( counter === CLIENT_VARIANTS ) {
136
- server . close ( ) ;
137
- }
138
177
} ) ) ;
139
178
} ) ;
140
179
@@ -149,10 +188,18 @@ if (!common.isWindows) { // Doesn't support this on windows
149
188
let counter = 0 ;
150
189
let socketCounter = 0 ;
151
190
const server = net . createServer ( )
152
- . on ( 'connection' , forAllClients ( function ( socket ) {
191
+ . on ( 'connection' , forAllClients ( function serverOnConnection ( socket ) {
153
192
socket . end ( 'ok' ) ;
193
+ socket . on ( 'end' , common . mustCall ( function ( ) {
194
+ counter ++ ;
195
+ if ( counter === CLIENT_VARIANTS ) {
196
+ setTimeout ( ( ) => {
197
+ server . close ( ) ;
198
+ } , 50 ) ;
199
+ }
200
+ } , 1 ) ) ;
154
201
} ) )
155
- . listen ( { path : serverPath } , common . mustCall ( function ( ) {
202
+ . listen ( { path : serverPath } , common . mustCall ( function serverOnListen ( ) {
156
203
const handleMap = new Map ( ) ;
157
204
const getSocketOpt = ( index ) => {
158
205
const handle = new Pipe ( ) ;
@@ -165,19 +212,15 @@ if (!common.isWindows) { // Doesn't support this on windows
165
212
const getConnectOpt = ( ) => ( {
166
213
path : serverPath
167
214
} ) ;
168
- const getConnectCb = ( index ) => common . mustCall ( function ( ) {
215
+ const getConnectCb = ( index ) => common . mustCall ( function clientOnConnect ( ) {
169
216
const client = this ;
170
217
// Test if it's wrapping an existing fd
171
218
assert ( handleMap . has ( index ) ) ;
172
219
const oldHandle = handleMap . get ( index ) ;
173
220
assert . strictEqual ( oldHandle . fd , this . _handle . fd ) ;
174
221
client . end ( ) ;
175
- client . on ( 'close' , common . mustCall ( function ( ) {
222
+ client . on ( 'close' , common . mustCall ( function clientOnClose ( ) {
176
223
oldHandle . close ( ) ;
177
- counter ++ ;
178
- if ( counter === CLIENT_VARIANTS ) {
179
- server . close ( ) ;
180
- }
181
224
} ) ) ;
182
225
} ) ;
183
226
0 commit comments