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

Commit

Permalink
fix: mfs preload test
Browse files Browse the repository at this point in the history
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 <alan@tableflip.io>
  • Loading branch information
alanshaw committed Sep 10, 2018
1 parent 2a06152 commit 13b4eca
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions test/core/mfs-preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

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

const createMockFilesStat = (cids = []) => {
Expand All @@ -15,11 +16,12 @@ const createMockFilesStat = (cids = []) => {
}

const createMockPreload = () => {
return function preload (cid, cb) {
preload.cids = preload.cids || []
const preload = (cid, cb) => {
preload.cids.push(cid)
cb()
}
preload.cids = []
return preload
}

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

setTimeout(() => {
preloader.stop((err) => {
expect(err).to.not.exist()
expect(
// Slice off any extra CIDs it processed
mockPreload.cids.slice(0, expectedPreloadCids.length)
).to.deep.equal(expectedPreloadCids)
done()
})
}, statCids.length * (interval * 2))
const test = (cb) => {
// Slice off any extra CIDs it processed
const cids = mockPreload.cids.slice(0, expectedPreloadCids.length)
if (cids.length !== expectedPreloadCids.length) return cb(null, false)
cb(null, cids.every((cid, i) => cid === expectedPreloadCids[i]))
}

waitFor(test, { name: 'CIDs to be preloaded' }, (err) => {
expect(err).to.not.exist()
preloader.stop(done)
})
})
})
})

0 comments on commit 13b4eca

Please sign in to comment.