Commit 4222c49 1 parent 2f2ba42 commit 4222c49 Copy full SHA for 4222c49
File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -218,7 +218,14 @@ class Libp2p extends EventEmitter {
218
218
log ( 'libp2p is stopping' )
219
219
220
220
try {
221
+ for ( const service of this . _discovery . values ( ) ) {
222
+ service . removeListener ( 'peer' , this . _onDiscoveryPeer )
223
+ }
224
+
225
+ await Promise . all ( Array . from ( this . _discovery . values ( ) , s => s . stop ( ) ) )
226
+
221
227
this . connectionManager . stop ( )
228
+
222
229
await Promise . all ( [
223
230
this . pubsub && this . pubsub . stop ( ) ,
224
231
this . _dht && this . _dht . stop ( ) ,
Original file line number Diff line number Diff line change @@ -65,6 +65,32 @@ describe('peer discovery', () => {
65
65
66
66
expect ( discoverySpy . called ) . to . eql ( false )
67
67
} )
68
+
69
+ it ( 'should stop discovery on libp2p start/stop' , async ( ) => {
70
+ const mockDiscovery = {
71
+ tag : 'mock' ,
72
+ start : ( ) => { } ,
73
+ stop : ( ) => { } ,
74
+ on : ( ) => { } ,
75
+ removeListener : ( ) => { }
76
+ }
77
+ const startSpy = sinon . spy ( mockDiscovery , 'start' )
78
+ const stopSpy = sinon . spy ( mockDiscovery , 'stop' )
79
+
80
+ libp2p = new Libp2p ( mergeOptions ( baseOptions , {
81
+ peerInfo,
82
+ modules : {
83
+ peerDiscovery : [ mockDiscovery ]
84
+ }
85
+ } ) )
86
+
87
+ await libp2p . start ( )
88
+ expect ( startSpy ) . to . have . property ( 'callCount' , 1 )
89
+ expect ( stopSpy ) . to . have . property ( 'callCount' , 0 )
90
+ await libp2p . stop ( )
91
+ expect ( startSpy ) . to . have . property ( 'callCount' , 1 )
92
+ expect ( stopSpy ) . to . have . property ( 'callCount' , 1 )
93
+ } )
68
94
} )
69
95
70
96
describe ( 'discovery modules from transports' , ( ) => {
You can’t perform that action at this time.
0 commit comments