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

Commit 74a79f3

Browse files
committed
fix: ensure MFS is continually preloaded
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 6a661e9 commit 74a79f3

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/core/mfs-preload.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ module.exports = (self, options) => {
2222
if (rootCid !== stats.hash) {
2323
log(`preloading updated MFS root ${rootCid} -> ${stats.hash}`)
2424

25-
self._preload(stats.hash, (err) => {
25+
return self._preload(stats.hash, (err) => {
2626
timeoutId = setTimeout(preloadMfs, options.interval)
2727
if (err) return log.error(`failed to preload MFS root ${stats.hash}`, err)
2828
rootCid = stats.hash
2929
})
3030
}
31+
32+
timeoutId = setTimeout(preloadMfs, options.interval)
3133
})
3234
}
3335

test/core/mfs-preload.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
9+
10+
const mfsPreload = require('../../src/core/mfs-preload')
11+
12+
const createMockFilesStat = (cids = []) => {
13+
let n = 0
14+
return (path, cb) => cb(null, { hash: cids[n++] || 'QmHash' })
15+
}
16+
17+
const createMockPreload = () => {
18+
return function preload (cid, cb) {
19+
preload.cids = preload.cids || []
20+
preload.cids.push(cid)
21+
cb()
22+
}
23+
}
24+
25+
describe('MFS preload', () => {
26+
it('should preload MFS root periodically', (done) => {
27+
// CIDs returned from our mock files.stat function
28+
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
29+
// The CIDs we expect to have been preloaded
30+
const expectedPreloadCids = ['QmSame', 'QmUpdated']
31+
32+
const mockPreload = createMockPreload()
33+
const mockFilesStat = createMockFilesStat(statCids)
34+
const mockIpfs = { files: { stat: mockFilesStat }, _preload: mockPreload }
35+
36+
const interval = 10
37+
const preloader = mfsPreload(mockIpfs, { interval })
38+
39+
preloader.start((err) => {
40+
expect(err).to.not.exist()
41+
42+
setTimeout(() => {
43+
preloader.stop((err) => {
44+
expect(err).to.not.exist()
45+
expect(
46+
// Slice off any extra CIDs it processed
47+
mockPreload.cids.slice(0, expectedPreloadCids.length)
48+
).to.deep.equal(expectedPreloadCids)
49+
done()
50+
})
51+
}, statCids.length * (interval + 5))
52+
})
53+
})
54+
})

0 commit comments

Comments
 (0)