From a28c878f4aaadb2392e7386d5122db3320eb211d Mon Sep 17 00:00:00 2001 From: Samlior Date: Thu, 21 Jan 2021 19:09:53 +0800 Subject: [PATCH] chore: fix close for ConnectionManager (#861) --- src/connection-manager/index.js | 2 +- test/connection-manager/index.node.js | 36 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 99a6ae1d..1bdef27a 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -160,7 +160,7 @@ class ConnectionManager extends EventEmitter { } } - await tasks + await Promise.all(tasks) this.connections.clear() } diff --git a/test/connection-manager/index.node.js b/test/connection-manager/index.node.js index b2f3ba5d..aae0b346 100644 --- a/test/connection-manager/index.node.js +++ b/test/connection-manager/index.node.js @@ -3,6 +3,7 @@ const { expect } = require('aegir/utils/chai') const sinon = require('sinon') +const { CLOSED } = require('libp2p-interfaces/src/connection/status') const delay = require('delay') const pWaitFor = require('p-wait-for') @@ -268,5 +269,40 @@ describe('libp2p.connections', () => { 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() + }) }) })