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

Commit 8bcf56f

Browse files
authored
fix: fix flaky pubsub test (#3761)
In the http client, when we subscribe to a topic we open a HTTP connection which we keep open for the duration of the subscription. When we unsubscribe we abort the connection but it can remain open for a little while after the abort, even if we try to wait on the `fetch` command ending before continuting, which leads to the topic still being present in the subs list, so retry asserting that the subs list is empty in the tests within a certain time window. Fixes this sort of error: ``` ipfs: 1) interface-ipfs-core over ipfs-http-client tests against go-ipfs ipfs: .pubsub.unsubscribe ipfs: should subscribe 5 handlers and unsubscribe once with no reference to the handlers: ipfs: AssertionError: expected [ Array(1) ] to deeply equal [] ipfs: + expected - actual ipfs: -[ ipfs: - "pubsub-tests-SVOFzpM5DtbcI7jBETrmm" ipfs: -] ipfs: +[] ```
1 parent 6fd7776 commit 8bcf56f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

packages/interface-ipfs-core/src/pubsub/unsubscribe.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env')
55
const { getTopic } = require('./utils')
6-
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
const { getDescribe, getIt } = require('../utils/mocha')
7+
const waitFor = require('../utils/wait-for')
78

89
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
910
/**
@@ -40,7 +41,18 @@ module.exports = (common, options) => {
4041
await ipfs.pubsub.unsubscribe(someTopic, handlers[i])
4142
}
4243

43-
return expect(ipfs.pubsub.ls()).to.eventually.eql([])
44+
// Unsubscribing in the http client aborts the connection we hold open
45+
// but does not wait for it to close so the subscription list sometimes
46+
// takes a little time to empty
47+
await waitFor(async () => {
48+
const subs = await ipfs.pubsub.ls()
49+
50+
return subs.length === 0
51+
}, {
52+
interval: 1000,
53+
timeout: 30000,
54+
name: 'subscriptions to be empty'
55+
})
4456
})
4557

4658
it(`should subscribe ${count} handlers and unsubscribe once with no reference to the handlers`, async () => {
@@ -50,7 +62,18 @@ module.exports = (common, options) => {
5062
}
5163
await ipfs.pubsub.unsubscribe(someTopic)
5264

53-
return expect(ipfs.pubsub.ls()).to.eventually.eql([])
65+
// Unsubscribing in the http client aborts the connection we hold open
66+
// but does not wait for it to close so the subscription list sometimes
67+
// takes a little time to empty
68+
await waitFor(async () => {
69+
const subs = await ipfs.pubsub.ls()
70+
71+
return subs.length === 0
72+
}, {
73+
interval: 1000,
74+
timeout: 30000,
75+
name: 'subscriptions to be empty'
76+
})
5477
})
5578
})
5679
}

0 commit comments

Comments
 (0)