Skip to content

Commit

Permalink
chore: add identify test for multiaddr change
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 25, 2020
1 parent 618998c commit 173367b
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,42 @@ describe('Identify', () => {
// Verify the streams close
await pWaitFor(() => connection.streams.length === 0)
})

it('should push multiaddr updates to an already connected peer', async () => {
libp2p = new Libp2p({
...baseOptions,
peerId
})

await libp2p.start()

sinon.spy(libp2p.identifyService, 'identify')
sinon.spy(libp2p.identifyService, 'push')

const connection = await libp2p.dialer.connectToPeer(remoteAddr)
expect(connection).to.exist()
// Wait for nextTick to trigger the identify call
await delay(1)

// Wait for identify to finish
await libp2p.identifyService.identify.firstCall.returnValue
sinon.stub(libp2p, 'isStarted').returns(true)

libp2p.peerStore.addressBook.add(libp2p.peerId, [multiaddr('/ip4/180.0.0.1/tcp/15001/ws')])

// Verify the remote peer is notified of change
expect(libp2p.identifyService.push.callCount).to.equal(1)
for (const call of libp2p.identifyService.push.getCalls()) {
const [connections] = call.args
expect(connections.length).to.equal(1)
expect(connections[0].remotePeer.toB58String()).to.equal(remoteAddr.getPeerId())
const results = await call.returnValue
expect(results.length).to.equal(1)
}

// Verify the streams close
await pWaitFor(() => connection.streams.length === 0)
})
})
})

Expand All @@ -424,8 +460,5 @@ const _createSelfPeerRecord = async (libp2p) => {
})
const envelope = await Envelope.seal(peerRecord, libp2p.peerId)
libp2p.peerStore.addressBook.consumePeerRecord(envelope)

return libp2p.peerStore.addressBook.getRawEnvelope(libp2p.peerId)
} catch (_) {}
return null
}

0 comments on commit 173367b

Please sign in to comment.