chore: fix close for ConnectionManager (#861)

This commit is contained in:
Samlior 2021-01-21 19:09:53 +08:00 committed by GitHub
parent 67067c97d5
commit a28c878f4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -160,7 +160,7 @@ class ConnectionManager extends EventEmitter {
} }
} }
await tasks await Promise.all(tasks)
this.connections.clear() this.connections.clear()
} }

View File

@ -3,6 +3,7 @@
const { expect } = require('aegir/utils/chai') const { expect } = require('aegir/utils/chai')
const sinon = require('sinon') const sinon = require('sinon')
const { CLOSED } = require('libp2p-interfaces/src/connection/status')
const delay = require('delay') const delay = require('delay')
const pWaitFor = require('p-wait-for') const pWaitFor = require('p-wait-for')
@ -268,5 +269,40 @@ describe('libp2p.connections', () => {
await libp2p.stop() await libp2p.stop()
}) })
it('should be closed status once immediately stopping', async () => {
const [libp2p] = await peerUtils.createPeer({
config: {
peerId: peerIds[0],
addresses: {
listen: ['/ip4/127.0.0.1/tcp/15003/ws']
},
modules: baseOptions.modules
}
})
const [remoteLibp2p] = await peerUtils.createPeer({
config: {
peerId: peerIds[1],
addresses: {
listen: ['/ip4/127.0.0.1/tcp/15004/ws']
},
modules: baseOptions.modules
}
})
libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
await libp2p.dial(remoteLibp2p.peerId)
const totalConns = Array.from(libp2p.connections.values())
expect(totalConns.length).to.eql(1)
const conns = totalConns[0]
expect(conns.length).to.eql(1)
const conn = conns[0]
await libp2p.stop()
expect(conn.stat.status).to.eql(CLOSED)
await remoteLibp2p.stop()
})
}) })
}) })