mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-24 22:41:33 +00:00
fix: clean up peer discovery flow (#494)
* fix: clean up peer discovery flow * test(fix): let libp2p start after connecting * test(fix): dont auto dial in disco tests
This commit is contained in:
@ -47,7 +47,7 @@ describe('Identify', () => {
|
||||
protocols,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
update: () => {}
|
||||
replace: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -57,17 +57,17 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
const localConnectionMock = { newStream: () => {} }
|
||||
const localConnectionMock = { newStream: () => {}, remotePeer: remotePeer.id }
|
||||
const remoteConnectionMock = { remoteAddr: observedAddr }
|
||||
|
||||
const [local, remote] = duplexPair()
|
||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
||||
|
||||
sinon.spy(localIdentify.registrar.peerStore, 'update')
|
||||
sinon.spy(localIdentify.registrar.peerStore, 'replace')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
localIdentify.identify(localConnectionMock, remotePeer.id),
|
||||
localIdentify.identify(localConnectionMock),
|
||||
remoteIdentify.handleMessage({
|
||||
connection: remoteConnectionMock,
|
||||
stream: remote,
|
||||
@ -75,9 +75,9 @@ describe('Identify', () => {
|
||||
})
|
||||
])
|
||||
|
||||
expect(localIdentify.registrar.peerStore.update.callCount).to.equal(1)
|
||||
expect(localIdentify.registrar.peerStore.replace.callCount).to.equal(1)
|
||||
// Validate the remote peer gets updated in the peer store
|
||||
const call = localIdentify.registrar.peerStore.update.firstCall
|
||||
const call = localIdentify.registrar.peerStore.replace.firstCall
|
||||
expect(call.args[0].id.bytes).to.equal(remotePeer.id.bytes)
|
||||
})
|
||||
|
||||
@ -92,7 +92,7 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
const localConnectionMock = { newStream: () => {} }
|
||||
const localConnectionMock = { newStream: () => {}, remotePeer }
|
||||
const remoteConnectionMock = { remoteAddr: observedAddr }
|
||||
|
||||
const [local, remote] = duplexPair()
|
||||
@ -128,7 +128,7 @@ describe('Identify', () => {
|
||||
peerInfo: remotePeer,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
update: () => {}
|
||||
replace: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -148,7 +148,7 @@ describe('Identify', () => {
|
||||
|
||||
sinon.spy(IdentifyService, 'updatePeerAddresses')
|
||||
sinon.spy(IdentifyService, 'updatePeerProtocols')
|
||||
sinon.spy(remoteIdentify.registrar.peerStore, 'update')
|
||||
sinon.spy(remoteIdentify.registrar.peerStore, 'replace')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
@ -163,8 +163,8 @@ describe('Identify', () => {
|
||||
expect(IdentifyService.updatePeerAddresses.callCount).to.equal(1)
|
||||
expect(IdentifyService.updatePeerProtocols.callCount).to.equal(1)
|
||||
|
||||
expect(remoteIdentify.registrar.peerStore.update.callCount).to.equal(1)
|
||||
const [peerInfo] = remoteIdentify.registrar.peerStore.update.firstCall.args
|
||||
expect(remoteIdentify.registrar.peerStore.replace.callCount).to.equal(1)
|
||||
const [peerInfo] = remoteIdentify.registrar.peerStore.replace.firstCall.args
|
||||
expect(peerInfo.id.bytes).to.eql(localPeer.id.bytes)
|
||||
expect(peerInfo.multiaddrs.toArray()).to.eql([listeningAddr])
|
||||
expect(peerInfo.protocols).to.eql(localProtocols)
|
||||
@ -198,7 +198,7 @@ describe('Identify', () => {
|
||||
})
|
||||
|
||||
sinon.spy(libp2p.dialer.identifyService, 'identify')
|
||||
sinon.spy(libp2p.peerStore, 'update')
|
||||
sinon.spy(libp2p.peerStore, 'replace')
|
||||
|
||||
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
|
||||
expect(connection).to.exist()
|
||||
@ -207,7 +207,7 @@ describe('Identify', () => {
|
||||
expect(libp2p.dialer.identifyService.identify.callCount).to.equal(1)
|
||||
await libp2p.dialer.identifyService.identify.firstCall.returnValue
|
||||
|
||||
expect(libp2p.peerStore.update.callCount).to.equal(1)
|
||||
expect(libp2p.peerStore.replace.callCount).to.equal(1)
|
||||
await connection.close()
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user