Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
fix: add delays for gossipsub (#202)
Browse files Browse the repository at this point in the history
GossipSub needs the heartbeat to run before the mesh overlay is built - we need to wait for this to happen otherwise messages don't get gossiped onwards to peers.
  • Loading branch information
achingbrain committed Apr 26, 2022
1 parent a7a979b commit cf85799
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
import type { PubSubArgs } from './index.js'
import type { Components } from '@libp2p/interfaces/components'
import { start, stop } from '../index.js'
import delay from 'delay'

export default (common: TestSetup<PubSub, PubSubArgs>) => {
describe('pubsub with multiple nodes', function () {
Expand Down Expand Up @@ -137,18 +138,21 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
psB.subscribe(topic)
psC.subscribe(topic)

let counter = 0

psA.addEventListener('message', incMsg)
psB.addEventListener('message', incMsg)
psC.addEventListener('message', incMsg)

await Promise.all([
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
waitForSubscriptionUpdate(psC, componentsB.getPeerId())
])

// GossipSub needs time to build the mesh overlay
await delay(1000)

let counter = 0

psA.addEventListener('message', incMsg)
psB.addEventListener('message', incMsg)
psC.addEventListener('message', incMsg)

const result = await psA.publish(topic, uint8ArrayFromString('hey'))

expect(result).to.have.property('recipients').with.property('length').greaterThanOrEqual(1)
Expand Down Expand Up @@ -190,10 +194,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
const defer = pDefer()
let counter = 0

psA.addEventListener('message', incMsg)
psB.addEventListener('message', incMsg)
psC.addEventListener('message', incMsg)

psA.subscribe(topic)
psB.subscribe(topic)
psC.subscribe(topic)
Expand All @@ -204,6 +204,13 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
waitForSubscriptionUpdate(psC, componentsB.getPeerId())
])

// GossipSub needs time to build the mesh overlay
await delay(1000)

psA.addEventListener('message', incMsg)
psB.addEventListener('message', incMsg)
psC.addEventListener('message', incMsg)

await psB.publish(topic, uint8ArrayFromString('hey'))

function incMsg (evt: CustomEvent<Message>) {
Expand Down Expand Up @@ -352,6 +359,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
waitForSubscriptionUpdate(psE, componentsD.getPeerId())
])

// GossipSub needs time to build the mesh overlay
await delay(1000)

await psC.publish('Z', uint8ArrayFromString('hey from c'))

function incMsg (evt: CustomEvent<Message>) {
Expand Down

0 comments on commit cf85799

Please sign in to comment.