Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix flaky tests #1137

Merged
merged 4 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions test/connection-manager/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('libp2p.connections', () => {
}
})

libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
await libp2p.dial(remoteLibp2p.peerId)

expect(libp2p.connections.size).to.eql(1)
Expand Down Expand Up @@ -161,8 +161,8 @@ describe('libp2p.connections', () => {
})

// Populate PeerStore before starting
libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
libp2p.peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
await libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
await libp2p.peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)

await libp2p.start()

Expand All @@ -188,8 +188,8 @@ describe('libp2p.connections', () => {
})

// Populate PeerStore before starting
libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
libp2p.peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
await libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
await libp2p.peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)

await libp2p.start()

Expand Down Expand Up @@ -253,7 +253,7 @@ describe('libp2p.connections', () => {
})

// Populate PeerStore after starting (discovery)
libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
await libp2p.peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)

// Wait for peer to connect
const conn = await libp2p.dial(nodes[0].peerId)
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('libp2p.connections', () => {
}
})

libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
await libp2p.dial(remoteLibp2p.peerId)

const totalConns = Array.from(libp2p.connections.values())
Expand Down
4 changes: 2 additions & 2 deletions test/core/ping.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe('ping', () => {
config: baseOptions
})

nodes[0].peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
nodes[1].peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
await nodes[0].peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
await nodes[1].peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
})

afterEach(() => Promise.all(nodes.map(n => n.stop())))
Expand Down
8 changes: 4 additions & 4 deletions test/dialing/direct.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('Dialing (direct, TCP)', () => {
peerStore
})

peerStore.addressBook.set(peerId, remoteTM.getAddrs())
await peerStore.addressBook.set(peerId, remoteTM.getAddrs())

const connection = await dialer.connectToPeer(peerId)
expect(connection).to.exist()
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('Dialing (direct, TCP)', () => {
})

sinon.spy(libp2p.dialer, 'connectToPeer')
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
await libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)

const connection = await libp2p.dial(remotePeerId)
expect(connection).to.exist()
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('Dialing (direct, TCP)', () => {

const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerId.toB58String()}`)

libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
await libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
const dialResults = await Promise.all([...new Array(dials)].map((_, index) => {
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerId)
return libp2p.dial(fullAddress)
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('Dialing (direct, TCP)', () => {
const error = new Error('Boom')
sinon.stub(libp2p.transportManager, 'dial').callsFake(() => Promise.reject(error))

libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
await libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
const dialResults = await pSettle([...new Array(dials)].map((_, index) => {
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerId)
return libp2p.dial(remoteAddr)
Expand Down
4 changes: 2 additions & 2 deletions test/dialing/direct.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('Dialing (direct, WebSockets)', () => {
localTM.add(Transport.prototype[Symbol.toStringTag], Transport, { filter: filters.all })
})

afterEach(() => {
peerStore.delete(peerId)
afterEach(async () => {
await peerStore.delete(peerId)
sinon.restore()
})

Expand Down
6 changes: 3 additions & 3 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ describe('Identify', () => {
peerId: localPeer,
datastore: new MemoryDatastore()
})
localPeerStore.protoBook.set(localPeer, protocols)
await localPeerStore.protoBook.set(localPeer, protocols)

remotePeerStore = new PeerStore({
peerId: remotePeer,
datastore: new MemoryDatastore()
})
remotePeerStore.protoBook.set(remotePeer, protocols)
await remotePeerStore.protoBook.set(remotePeer, protocols)

localAddressManager = new AddressManager(localPeer)
remoteAddressManager = new AddressManager(remotePeer)
Expand Down Expand Up @@ -372,7 +372,7 @@ describe('Identify', () => {
peerId: remotePeer,
datastore: new MemoryDatastore()
})
remotePeerStore.protoBook.set(remotePeer, storedProtocols)
await remotePeerStore.protoBook.set(remotePeer, storedProtocols)

const remoteIdentify = new IdentifyService({
libp2p: {
Expand Down
4 changes: 2 additions & 2 deletions test/peer-discovery/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ describe('peer discovery scenarios', () => {
remoteLibp2p2.start()
])

libp2p.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.multiaddrs)
remoteLibp2p2.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.multiaddrs)
await libp2p.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.multiaddrs)
await remoteLibp2p2.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.multiaddrs)

// Topology:
// A -> B
Expand Down
2 changes: 1 addition & 1 deletion test/peer-discovery/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('peer discovery', () => {
}
})

libp2p.peerStore.addressBook.set(remotePeerId, [new Multiaddr('/ip4/165.1.1.1/tcp/80')])
await libp2p.peerStore.addressBook.set(remotePeerId, [new Multiaddr('/ip4/165.1.1.1/tcp/80')])

const deferred = defer()
sinon.stub(libp2p.dialer, 'connectToPeer').callsFake((remotePeerId) => {
Expand Down
20 changes: 10 additions & 10 deletions test/relay/auto-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('auto-relay', () => {
await pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1)
expect(relayLibp2p1.multiaddrs[originalMultiaddrs1Length].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

// Only one will be used for listeninng
// Only one will be used for listening
expect(relayLibp2p1.transportManager.listen.callCount).to.equal(1)

// Spy if relay from listen map was removed
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('auto-relay', () => {
expect(autoRelay1._listenRelays.size).to.equal(1)
expect(relayLibp2p1.connectionManager.size).to.equal(2)

// Only one will be used for listeninng
// Only one will be used for listening
expect(relayLibp2p1.transportManager.listen.callCount).to.equal(1)

// Disconnect not used listen relay
Expand All @@ -363,16 +363,16 @@ describe('auto-relay', () => {
sinon.spy(relayLibp2p1, 'dial')

// Remove peer used as relay from peerStore and disconnect it
relayLibp2p1.peerStore.delete(relayLibp2p2.peerId)
await relayLibp2p1.peerStore.delete(relayLibp2p2.peerId)
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
expect(autoRelay1._listenRelays.size).to.equal(0)
expect(relayLibp2p1.connectionManager.size).to.equal(0)

// Wait for other peer connected to be added as listen addr
await pWaitFor(() => relayLibp2p1.transportManager.listen.callCount === 2)
expect(autoRelay1._tryToListenOnRelay.callCount).to.equal(1)
expect(autoRelay1._listenRelays.size).to.equal(1)
expect(relayLibp2p1.connectionManager.size).to.eql(1)
await pWaitFor(() => autoRelay1._tryToListenOnRelay.callCount === 1)
await pWaitFor(() => autoRelay1._listenRelays.size === 1)
await pWaitFor(() => relayLibp2p1.connectionManager.size === 1)
})

it('should not fail when trying to dial unreachable peers to add as hop relay and replaced removed ones', async () => {
Expand All @@ -394,7 +394,7 @@ describe('auto-relay', () => {
expect(autoRelay1._listenRelays.size).to.equal(1)
expect(relayLibp2p1.connectionManager.size).to.equal(2)

// Only one will be used for listeninng
// Only one will be used for listening
expect(relayLibp2p1.transportManager.listen.callCount).to.equal(1)

// Disconnect not used listen relay
Expand All @@ -410,7 +410,7 @@ describe('auto-relay', () => {
})

// Remove peer used as relay from peerStore and disconnect it
relayLibp2p1.peerStore.delete(relayLibp2p2.peerId)
await relayLibp2p1.peerStore.delete(relayLibp2p2.peerId)
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
expect(autoRelay1._listenRelays.size).to.equal(0)
expect(relayLibp2p1.connectionManager.size).to.equal(0)
Expand Down Expand Up @@ -492,7 +492,7 @@ describe('auto-relay', () => {

// Wait for peer added as listen relay
await pWaitFor(() => autoRelay2._addListenRelay.callCount === 1)
expect(autoRelay2._listenRelays.size).to.equal(1)
await pWaitFor(() => autoRelay2._listenRelays.size === 1)

// Relay 1 discovers Relay 2 relayed multiaddr via Relay 3
const ma2RelayedBy3 = relayLibp2p2.multiaddrs[relayLibp2p2.multiaddrs.length - 1]
Expand Down Expand Up @@ -619,7 +619,7 @@ describe('auto-relay', () => {
await pWaitFor(() => local.multiaddrs.length === originalMultiaddrsLength + 1)

const relayedAddr = local.multiaddrs[local.multiaddrs.length - 1]
remote.peerStore.addressBook.set(local.peerId, [relayedAddr])
await remote.peerStore.addressBook.set(local.peerId, [relayedAddr])

// Dial from remote through the relayed address
const conn = await remote.dial(local.peerId)
Expand Down
8 changes: 1 addition & 7 deletions test/relay/relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ describe('Dialing (via relay, TCP)', () => {

afterEach(async () => {
// Stop each node
return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(async libp2p => {
await libp2p.stop()
// Clear the peer stores
for await (const peer of libp2p.peerStore.getPeers()) {
libp2p.peerStore.delete(peer.id)
}
}))
return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(libp2p => libp2p.stop()))
})

it('should be able to connect to a peer over a relay with active connections', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/utils/creators/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ async function createPeer ({ number = 1, fixture = true, started = true, populat
if (started) {
await Promise.all(peers.map((p) => p.start()))

populateAddressBooks && _populateAddressBooks(peers)
populateAddressBooks && await _populateAddressBooks(peers)
}

return peers
}

function _populateAddressBooks (peers) {
async function _populateAddressBooks (peers) {
for (let i = 0; i < peers.length; i++) {
for (let j = 0; j < peers.length; j++) {
if (i !== j) {
peers[i].peerStore.addressBook.set(peers[j].peerId, peers[j].multiaddrs)
await peers[i].peerStore.addressBook.set(peers[j].peerId, peers[j].multiaddrs)
}
}
}
Expand Down