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

Commit cd4c409

Browse files
authored
fix: make tests more reliable (#103)
await finishing processing rpc messages instead of using setTimeout. Also return promises and await during processing message stream so we don't get unhandled promise rejections (potentially).
1 parent d8d46f0 commit cd4c409

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

packages/compliance-tests/src/pubsub/messages.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,11 @@ module.exports = (common) => {
6666
}
6767

6868
pubsub.subscribe(topic)
69-
pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
69+
await pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
7070

71-
return new Promise((resolve) => {
72-
setTimeout(() => {
73-
expect(pubsub.validate.callCount).to.eql(1)
74-
expect(pubsub._emitMessage.called).to.eql(false)
75-
expect(pubsub._publish.called).to.eql(false)
76-
77-
resolve()
78-
}, 50)
79-
})
71+
expect(pubsub.validate.callCount).to.eql(1)
72+
expect(pubsub._emitMessage.called).to.eql(false)
73+
expect(pubsub._publish.called).to.eql(false)
8074
})
8175

8276
it('should not drop unsigned messages if strict signing is disabled', async () => {
@@ -99,17 +93,11 @@ module.exports = (common) => {
9993
}
10094

10195
pubsub.subscribe(topic)
102-
pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
96+
await pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
10397

104-
return new Promise((resolve) => {
105-
setTimeout(() => {
106-
expect(pubsub.validate.callCount).to.eql(1)
107-
expect(pubsub._emitMessage.called).to.eql(true)
108-
expect(pubsub._publish.called).to.eql(true)
109-
110-
resolve()
111-
}, 50)
112-
})
98+
expect(pubsub.validate.callCount).to.eql(1)
99+
expect(pubsub._emitMessage.called).to.eql(true)
100+
expect(pubsub._publish.called).to.eql(true)
113101
})
114102
})
115103
}

packages/interfaces/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"@types/bl": "^5.0.1",
7373
"@types/debug": "^4.1.5",
7474
"aegir": "^33.0.0",
75-
"cids": "^1.1.6",
7675
"events": "^3.3.0",
7776
"it-pair": "^1.0.0",
7877
"p-wait-for": "^3.2.0",

packages/interfaces/src/pubsub/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class PubsubBaseProtocol extends EventEmitter {
356356
const rpcBytes = data instanceof Uint8Array ? data : data.slice()
357357
const rpcMsg = this._decodeRpc(rpcBytes)
358358

359-
this._processRpc(idB58Str, peerStreams, rpcMsg)
359+
await this._processRpc(idB58Str, peerStreams, rpcMsg)
360360
}
361361
}
362362
)
@@ -371,9 +371,9 @@ class PubsubBaseProtocol extends EventEmitter {
371371
* @param {string} idB58Str
372372
* @param {PeerStreams} peerStreams
373373
* @param {RPC} rpc
374-
* @returns {boolean}
374+
* @returns {Promise<boolean>}
375375
*/
376-
_processRpc (idB58Str, peerStreams, rpc) {
376+
async _processRpc (idB58Str, peerStreams, rpc) {
377377
this.log('rpc from', idB58Str)
378378
const subs = rpc.subscriptions
379379
const msgs = rpc.msgs
@@ -393,14 +393,14 @@ class PubsubBaseProtocol extends EventEmitter {
393393

394394
if (msgs.length) {
395395
// @ts-ignore RPC message is modified
396-
msgs.forEach((message) => {
396+
for (const message of msgs) {
397397
if (!(this.canRelayMessage || (message.topicIDs && message.topicIDs.some((topic) => this.subscriptions.has(topic))))) {
398398
this.log('received message we didn\'t subscribe to. Dropping.')
399-
return
399+
continue
400400
}
401401
const msg = utils.normalizeInRpcMessage(message, idB58Str)
402-
this._processRpcMessage(msg)
403-
})
402+
await this._processRpcMessage(msg)
403+
}
404404
}
405405
return true
406406
}
@@ -455,7 +455,7 @@ class PubsubBaseProtocol extends EventEmitter {
455455
// Emit to self
456456
this._emitMessage(msg)
457457

458-
this._publish(utils.normalizeOutRpcMessage(msg))
458+
return this._publish(utils.normalizeOutRpcMessage(msg))
459459
}
460460

461461
/**

0 commit comments

Comments
 (0)