From 13b4eca0b1bbabf068b44517bd9db07ed03c7e83 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 10 Sep 2018 11:57:28 +0100 Subject: [PATCH] fix: mfs preload test 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 --- test/core/mfs-preload.spec.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/test/core/mfs-preload.spec.js b/test/core/mfs-preload.spec.js index d35bffd628..0b47551856 100644 --- a/test/core/mfs-preload.spec.js +++ b/test/core/mfs-preload.spec.js @@ -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 = []) => { @@ -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', () => { @@ -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) + }) }) }) })