Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: await pubsub unsubscribe to fix flaky test #3755

Closed
wants to merge 1 commit into from
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
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"multiformats": "^9.4.1",
"nanoid": "^3.1.12",
"native-abort-controller": "^1.0.3",
"p-defer": "^3.0.0",
"parse-duration": "^1.0.0",
"stream-to-it": "^0.2.2",
"uint8arrays": "^2.1.6"
Expand All @@ -77,7 +78,6 @@
"it-concat": "^2.0.0",
"it-first": "^1.0.4",
"nock": "^13.0.2",
"p-defer": "^3.0.0",
"rimraf": "^3.0.2"
},
"engines": {
Expand Down
8 changes: 7 additions & 1 deletion packages/ipfs-http-client/src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const uint8ArrayToString = require('uint8arrays/to-string')
const log = require('debug')('ipfs-http-client:pubsub:subscribe')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const defer = require('p-defer')

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
Expand All @@ -24,7 +25,9 @@ module.exports = (options, subsTracker) => {
* @type {PubsubAPI["subscribe"]}
*/
async function subscribe (topic, handler, options = {}) { // eslint-disable-line require-await
options.signal = subsTracker.subscribe(topic, handler, options.signal)
const end = defer()

options.signal = subsTracker.subscribe(topic, handler, end.promise, options.signal)

/** @type {(value?: any) => void} */
let done
Expand Down Expand Up @@ -73,6 +76,9 @@ module.exports = (options, subsTracker) => {

done()
})
.finally(() => {
end.resolve()
})
}, 0)

return result
Expand Down
12 changes: 9 additions & 3 deletions packages/ipfs-http-client/src/pubsub/subscription-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { AbortController } = require('native-abort-controller')
* @typedef {Object} Subscription
* @property {MessageHandlerFn} handler
* @property {AbortController} controller
* @property {Promise<void>} end
*/

class SubscriptionTracker {
Expand All @@ -19,9 +20,10 @@ class SubscriptionTracker {
/**
* @param {string} topic
* @param {MessageHandlerFn} handler
* @param {Promise<void>} end
* @param {AbortSignal} [signal]
*/
subscribe (topic, handler, signal) {
subscribe (topic, handler, end, signal) {
const topicSubs = this._subs.get(topic) || []

if (topicSubs.find(s => s.handler === handler)) {
Expand All @@ -31,7 +33,7 @@ class SubscriptionTracker {
// Create controller so a call to unsubscribe can cancel the request
const controller = new AbortController()

this._subs.set(topic, [{ handler, controller }].concat(topicSubs))
this._subs.set(topic, [{ handler, controller, end }].concat(topicSubs))

// If there is an external signal, forward the abort event
if (signal) {
Expand All @@ -45,7 +47,7 @@ class SubscriptionTracker {
* @param {string} topic
* @param {MessageHandlerFn} [handler]
*/
unsubscribe (topic, handler) {
async unsubscribe (topic, handler) {
const subs = this._subs.get(topic) || []
let unsubs

Expand All @@ -62,6 +64,10 @@ class SubscriptionTracker {
}

unsubs.forEach(s => s.controller.abort())

await Promise.all(
unsubs.map(sub => sub.end)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/pubsub/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (options, subsTracker) => {
* @type {PubsubAPI["unsubscribe"]}
*/
async function unsubscribe (topic, handler) {
subsTracker.unsubscribe(topic, handler)
await subsTracker.unsubscribe(topic, handler)
}
return unsubscribe
}