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

Commit 7c7a5a6

Browse files
authored
fix: mfs preload test (#1551)
JS makes no guarantees a function you schedule with `setTimeout` will execute before another function you schedule _even_ if the second function is scheduled for execution _after_ the first. This PR fixes the MFS preload test to wait for the expected CIDs to have been preloaded instead of assuming they would be preloaded after a certain time. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent ccad1c8 commit 7c7a5a6

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

test/core/mfs-preload.spec.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99

10+
const waitFor = require('../utils/wait-for')
1011
const mfsPreload = require('../../src/core/mfs-preload')
1112

1213
const createMockFilesStat = (cids = []) => {
@@ -15,11 +16,12 @@ const createMockFilesStat = (cids = []) => {
1516
}
1617

1718
const createMockPreload = () => {
18-
return function preload (cid, cb) {
19-
preload.cids = preload.cids || []
19+
const preload = (cid, cb) => {
2020
preload.cids.push(cid)
2121
cb()
2222
}
23+
preload.cids = []
24+
return preload
2325
}
2426

2527
describe('MFS preload', () => {
@@ -41,16 +43,17 @@ describe('MFS preload', () => {
4143
preloader.start((err) => {
4244
expect(err).to.not.exist()
4345

44-
setTimeout(() => {
45-
preloader.stop((err) => {
46-
expect(err).to.not.exist()
47-
expect(
48-
// Slice off any extra CIDs it processed
49-
mockPreload.cids.slice(0, expectedPreloadCids.length)
50-
).to.deep.equal(expectedPreloadCids)
51-
done()
52-
})
53-
}, statCids.length * (interval * 2))
46+
const test = (cb) => {
47+
// Slice off any extra CIDs it processed
48+
const cids = mockPreload.cids.slice(0, expectedPreloadCids.length)
49+
if (cids.length !== expectedPreloadCids.length) return cb(null, false)
50+
cb(null, cids.every((cid, i) => cid === expectedPreloadCids[i]))
51+
}
52+
53+
waitFor(test, { name: 'CIDs to be preloaded' }, (err) => {
54+
expect(err).to.not.exist()
55+
preloader.stop(done)
56+
})
5457
})
5558
})
5659
})

0 commit comments

Comments
 (0)