File tree 3 files changed +33
-0
lines changed
3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,7 @@ class Libp2p extends EventEmitter {
187
187
this . pubsub && await this . pubsub . stop ( )
188
188
this . _dht && await this . _dht . stop ( )
189
189
await this . transportManager . close ( )
190
+ await this . registrar . close ( )
190
191
} catch ( err ) {
191
192
if ( err ) {
192
193
log . error ( err )
Original file line number Diff line number Diff line change @@ -46,6 +46,23 @@ class Registrar {
46
46
this . _handle = handle
47
47
}
48
48
49
+ /**
50
+ * Cleans up the registrar
51
+ * @async
52
+ */
53
+ async close ( ) {
54
+ // Close all connections we're tracking
55
+ const tasks = [ ]
56
+ for ( const connectionList of this . connections . values ( ) ) {
57
+ for ( const connection of connectionList ) {
58
+ tasks . push ( connection . close ( ) )
59
+ }
60
+ }
61
+
62
+ await tasks
63
+ this . connections . clear ( )
64
+ }
65
+
49
66
/**
50
67
* Add a new connected peer to the record
51
68
* TODO: this should live in the ConnectionManager
Original file line number Diff line number Diff line change @@ -54,4 +54,19 @@ describe('registrar on dial', () => {
54
54
const remoteConn = remoteLibp2p . registrar . getConnection ( peerInfo )
55
55
expect ( remoteConn ) . to . exist ( )
56
56
} )
57
+
58
+ it ( 'should be closed on libp2p stop' , async ( ) => {
59
+ libp2p = new Libp2p ( mergeOptions ( baseOptions , {
60
+ peerInfo
61
+ } ) )
62
+
63
+ await libp2p . dial ( remoteAddr )
64
+ expect ( libp2p . registrar . connections . size ) . to . equal ( 1 )
65
+
66
+ sinon . spy ( libp2p . registrar , 'close' )
67
+
68
+ await libp2p . stop ( )
69
+ expect ( libp2p . registrar . close . callCount ) . to . equal ( 1 )
70
+ expect ( libp2p . registrar . connections . size ) . to . equal ( 0 )
71
+ } )
57
72
} )
You can’t perform that action at this time.
0 commit comments