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

Commit

Permalink
fix: ensure MFS is continually preloaded
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw committed Jul 27, 2018
1 parent d49f17e commit 9df1012
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/mfs-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ module.exports = (self, options) => {
if (rootCid !== stats.hash) {
log(`preloading updated MFS root ${rootCid} -> ${stats.hash}`)

self._preload(stats.hash, (err) => {
return self._preload(stats.hash, (err) => {
timeoutId = setTimeout(preloadMfs, options.interval)
if (err) return log.error(`failed to preload MFS root ${stats.hash}`, err)
rootCid = stats.hash
})
}

timeoutId = setTimeout(preloadMfs, options.interval)
})
}

Expand Down
54 changes: 54 additions & 0 deletions test/core/mfs-preload.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const mfsPreload = require('../../src/core/mfs-preload')

const createMockFilesStat = (cids = []) => {
let n = 0
return (path, cb) => cb(null, { hash: cids[n++] || 'QmHash' })
}

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

describe('MFS preload', () => {
it('should preload MFS root periodically', (done) => {
// CIDs returned from our mock files.stat function
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
// The CIDs we expect to have been preloaded
const expectedPreloadCids = ['QmSame', 'QmUpdated']

const mockPreload = createMockPreload()
const mockFilesStat = createMockFilesStat(statCids)
const mockIpfs = { files: { stat: mockFilesStat }, _preload: mockPreload }

const interval = 10
const preloader = mfsPreload(mockIpfs, { interval })

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 + 5))
})
})
})

0 comments on commit 9df1012

Please sign in to comment.