Skip to content

Commit

Permalink
chore: add new peer store tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 6, 2020
1 parent f938535 commit 1662065
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions test/peer-store/peer-store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('peer-store', () => {
let peerIds
before(async () => {
peerIds = await peerUtils.createPeerId({
number: 3
number: 4
})
})

Expand Down Expand Up @@ -64,18 +64,23 @@ describe('peer-store', () => {
peerStore.addressBook.set(peerIds[1], [addr3])
peerStore.protoBook.set(peerIds[1], [proto2, proto3])

// Add peer2 { addr4 }
// Add peer2 with { addr4 }
peerStore.addressBook.set(peerIds[2], [addr4])

// Add peer3 with { addr4 } and { proto2 }
peerStore.addressBook.set(peerIds[3], [addr4])
peerStore.protoBook.set(peerIds[3], [proto2])
})

it('has peers', () => {
const peers = peerStore.peers

expect(peers.size).to.equal(3)
expect(peers.size).to.equal(4)
expect(Array.from(peers.keys())).to.have.members([
peerIds[0].toB58String(),
peerIds[1].toB58String(),
peerIds[2].toB58String()
peerIds[2].toB58String(),
peerIds[3].toB58String()
])
})

Expand All @@ -84,16 +89,16 @@ describe('peer-store', () => {
expect(deleted).to.equal(true)

const peers = peerStore.peers
expect(peers.size).to.equal(2)
expect(Array.from(peers.keys())).to.not.have.members([peerIds[0].toString()])
expect(peers.size).to.equal(3)
expect(Array.from(peers.keys())).to.not.have.members([peerIds[0].toB58String()])
})

it('returns true on deleting a stored peer which is only on one book', () => {
const deleted = peerStore.delete(peerIds[2])
expect(deleted).to.equal(true)

const peers = peerStore.peers
expect(peers.size).to.equal(2)
expect(peers.size).to.equal(3)
})

it('finds the stored information of a peer in all its books', () => {
Expand All @@ -113,5 +118,33 @@ describe('peer-store', () => {
const peerMultiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr)
expect(peerMultiaddrs).to.have.members([addr4])
})

it('can find all the peers supporting a protocol', () => {
const peerSupporting2 = []

for (const [, peerInfo] of peerStore.peers.entries()) {
if (peerInfo.protocols.has(proto2)) {
peerSupporting2.push(peerInfo)
}
}

expect(peerSupporting2.length).to.eql(2)
expect(peerSupporting2[0].id.toB58String()).to.eql(peerIds[1].toB58String())
expect(peerSupporting2[1].id.toB58String()).to.eql(peerIds[3].toB58String())
})

it('can find all the peers listening on a given address', () => {
const peerListenint4 = []

for (const [, peerInfo] of peerStore.peers.entries()) {
if (peerInfo.multiaddrs.has(addr4)) {
peerListenint4.push(peerInfo)
}
}

expect(peerListenint4.length).to.eql(2)
expect(peerListenint4[0].id.toB58String()).to.eql(peerIds[2].toB58String())
expect(peerListenint4[1].id.toB58String()).to.eql(peerIds[3].toB58String())
})
})
})

0 comments on commit 1662065

Please sign in to comment.