Skip to content
Closed
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
16 changes: 7 additions & 9 deletions 2.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ node3.services.pubsub.addEventListener('message', (evt) => {
})
node3.services.pubsub.subscribe(topic)

// wait for subscriptions to propagate
await hasSubscription(node1, node2, topic)
await hasSubscription(node2, node3, topic)
// Subscription announcements can arrive before the forwarding mesh is ready.
await hasMeshPeer(node1, node2, topic)
await hasMeshPeer(node2, node3, topic)

const validateFruit = (msgTopic, msg) => {
const fruit = uint8ArrayToString(msg.data)
Expand Down Expand Up @@ -127,16 +127,14 @@ async function waitForMessage (node, topic, data, timeout = 100) {
})
}

// Wait for node1 to see that node2 has subscribed to the topic
async function hasSubscription (node1, node2, topic) {
// Wait for the mesh link needed to forward messages to the next node.
async function hasMeshPeer (node1, node2, topic) {
while (true) {
const subs = await node1.services.pubsub.getSubscribers(topic)

if (subs.map(peer => peer.toString()).includes(node2.peerId.toString())) {
if (node1.services.pubsub.getMeshPeers(topic).includes(node2.peerId.toString())) {
return
}

// wait for subscriptions to propagate
// Wait for mesh maintenance to include the peer.
await delay(100)
}
}
6 changes: 6 additions & 0 deletions test/test-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export async function test () {
})
proc.kill()

for (const fruit of ['banana', 'apple', 'orange']) {
if (!matches.input.includes(`node3 received: ${fruit}`)) {
throw new Error(`node3 should have received '${fruit}'`)
}
}

// 'car' fails validation, so node2 never re-shares it - it must be absent
// from everything printed up to the final valid message
if (matches.input.includes('node3 received: car')) {
Expand Down