fix: emit peer:connect after all (#1171)

**Motivation**

In lodestar, when we handle "peer:connect" event, we dial the peer which gives another "peer:connect" event and it causes other issues

**Motivation**

In `onConnect` function, "peer:connect" event should be emitted after we add connection to the `connections` map so that when app dial the peer in "peer:connect" event handler, it uses the same/existing connection
This commit is contained in:
tuyennhv
2022-04-23 02:56:47 +07:00
committed by GitHub
parent fab4f1385c
commit d16817ca44
5 changed files with 17 additions and 12 deletions

View File

@ -29,6 +29,7 @@ import { createFromJSON } from '@libp2p/peer-id-factory'
import Peers from '../fixtures/peers.js'
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import { pEvent } from 'p-event'
const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999')
@ -430,21 +431,15 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
const identifySpy = sinon.spy(libp2p.identifyService, 'identify')
const protobookSetSpy = sinon.spy(libp2p.components.getPeerStore().protoBook, 'set')
const connectionPromise = pDefer()
const connectionPromise = pEvent(libp2p.connectionManager, 'peer:connect')
await libp2p.start()
libp2p.components.getUpgrader().addEventListener('connection', () => {
connectionPromise.resolve()
}, {
once: true
})
const connection = await libp2p.dial(MULTIADDRS_WEBSOCKETS[0])
expect(connection).to.exist()
// Wait for connection event to be emitted
await connectionPromise.promise
await connectionPromise
expect(identifySpy.callCount).to.equal(1)
await identifySpy.firstCall.returnValue