@@ -103,6 +103,60 @@ test('ws: client.stop()', function (t) {
103103 testClientStop ( t , 'ws' )
104104} )
105105
106+ function testClientStopDestroy ( t , serverType ) {
107+ t . plan ( 2 )
108+
109+ common . createServer ( t , serverType , function ( server , announceUrl ) {
110+ var client = new Client ( {
111+ infoHash : fixtures . leaves . parsedTorrent . infoHash ,
112+ announce : announceUrl ,
113+ peerId : peerId1 ,
114+ port : port ,
115+ wrtc : { }
116+ } )
117+
118+ if ( serverType === 'ws' ) common . mockWebsocketTracker ( client )
119+ client . on ( 'error' , function ( err ) { t . error ( err ) } )
120+ client . on ( 'warning' , function ( err ) { t . error ( err ) } )
121+
122+ client . start ( )
123+
124+ client . once ( 'update' , function ( ) {
125+ t . pass ( 'client received response to "start" message' )
126+
127+ client . stop ( )
128+
129+ client . on ( 'update' , function ( ) { t . fail ( 'client should not receive update after destroy is called' ) } )
130+
131+ // Call destroy() in the same tick as stop(), but the message should still
132+ // be received by the server, though obviously the client won't receive the
133+ // response.
134+ client . destroy ( )
135+
136+ server . once ( 'stop' , function ( peer , params ) {
137+ t . pass ( 'server received "stop" message' )
138+ setTimeout ( function ( ) {
139+ // give the websocket server time to finish in progress (stream) messages
140+ // to peers
141+ server . close ( )
142+ } , 100 )
143+ } )
144+ } )
145+ } )
146+ }
147+
148+ test ( 'http: client.stop(); client.destroy()' , function ( t ) {
149+ testClientStopDestroy ( t , 'http' )
150+ } )
151+
152+ test ( 'udp: client.stop(); client.destroy()' , function ( t ) {
153+ testClientStopDestroy ( t , 'udp' )
154+ } )
155+
156+ test ( 'ws: client.stop(); client.destroy()' , function ( t ) {
157+ testClientStopDestroy ( t , 'ws' )
158+ } )
159+
106160function testClientUpdate ( t , serverType ) {
107161 t . plan ( 4 )
108162
0 commit comments