This repository was archived by the owner on Aug 29, 2023. It is now read-only.
File tree 2 files changed +34
-3
lines changed
2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ const contains = require('lodash.contains')
12
12
exports = module . exports = TCP
13
13
14
14
const IPFS_CODE = 421
15
+ const CLOSE_TIMEOUT = 300
15
16
16
17
function TCP ( ) {
17
18
if ( ! ( this instanceof TCP ) ) {
@@ -97,8 +98,7 @@ function TCP () {
97
98
}
98
99
99
100
this . close = ( callback ) => {
100
- const closeTimeout = 300
101
-
101
+ log ( 'closing' )
102
102
if ( listeners . length === 0 ) {
103
103
log ( 'Called close with no active listeners' )
104
104
return callback ( )
@@ -110,7 +110,7 @@ function TCP () {
110
110
log ( 'destroying %s' , key )
111
111
listener . __connections [ key ] . destroy ( )
112
112
} )
113
- } , closeTimeout )
113
+ } , CLOSE_TIMEOUT )
114
114
115
115
listener . close ( cb )
116
116
} ) , callback )
Original file line number Diff line number Diff line change @@ -148,4 +148,35 @@ describe('libp2p-tcp', function () {
148
148
expect ( valid [ 0 ] ) . to . deep . equal ( mh1 )
149
149
done ( )
150
150
} )
151
+
152
+ it ( 'destroys after timeout' , ( done ) => {
153
+ const server = new TCPlibp2p ( )
154
+ const mh = multiaddr ( '/ip4/127.0.0.1/tcp/9090' )
155
+ server . createListener ( mh , ( conn ) => {
156
+ const i = setInterval ( ( ) => {
157
+ conn . read ( )
158
+ conn . write ( 'hi\n' )
159
+ } , 100 )
160
+ i . unref ( )
161
+ } , ( ) => {
162
+ let connected = 0
163
+ const connectHandler = ( ) => {
164
+ connected ++
165
+ if ( connected === 10 ) {
166
+ setTimeout ( ( ) => {
167
+ server . close ( done )
168
+ } , 1 )
169
+ }
170
+ }
171
+ const errorHandler = ( ) => { }
172
+
173
+ for ( let i = 0 ; i < 10 ; i ++ ) {
174
+ const client = net . connect ( 9090 )
175
+ client . on ( 'connect' , connectHandler )
176
+
177
+ // just ignore the resets
178
+ client . on ( 'error' , errorHandler )
179
+ }
180
+ } )
181
+ } )
151
182
} )
You can’t perform that action at this time.
0 commit comments