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

Commit

Permalink
fix: allow disabling mfs preload from config (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored and Alan Shaw committed Nov 29, 2018
1 parent ac5fa8e commit 5f66538
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const schema = Joi.object().keys({
repoOwner: Joi.boolean().default(true),
preload: Joi.object().keys({
enabled: Joi.boolean().default(true),
addresses: Joi.array().items(Joi.multiaddr().options({ convert: false }))
addresses: Joi.array().items(Joi.multiaddr().options({ convert: false })),
interval: Joi.number().integer().default(30 * 1000)
}).allow(null),
init: Joi.alternatives().try(
Joi.boolean(),
Expand Down
12 changes: 10 additions & 2 deletions src/core/mfs-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const debug = require('debug')
const log = debug('jsipfs:mfs-preload')
log.error = debug('jsipfs:mfs-preload:error')

module.exports = (self, options) => {
options = options || {}
module.exports = (self) => {
const options = self._options.preload || {}
options.interval = options.interval || 30 * 1000

if (!options.enabled) {
log('MFS preload disabled')
return {
start: (cb) => setImmediate(cb),
stop: (cb) => setImmediate(cb)
}
}

let rootCid
let timeoutId

Expand Down
50 changes: 41 additions & 9 deletions test/core/mfs-preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,36 @@ const createMockPreload = () => {
}

describe('MFS preload', () => {
// CIDs returned from our mock files.stat function
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
let mockPreload
let mockFilesStat
let mockIpfs

beforeEach(() => {
mockPreload = createMockPreload()
mockFilesStat = createMockFilesStat(statCids)
mockIpfs = {
files: {
stat: mockFilesStat
},
_preload: mockPreload,
_options: {
preload: {
interval: 10
}
}
}
})

it('should preload MFS root periodically', function (done) {
this.timeout(80 * 1000)

// CIDs returned from our mock files.stat function
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
mockIpfs._options.preload.enabled = true

// 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 })
const preloader = mfsPreload(mockIpfs)

preloader.start((err) => {
expect(err).to.not.exist()
Expand All @@ -56,4 +72,20 @@ describe('MFS preload', () => {
})
})
})

it('should disable preloading MFS', function (done) {
mockIpfs._options.preload.enabled = false

const preloader = mfsPreload(mockIpfs)

preloader.start((err) => {
expect(err).to.not.exist()

setTimeout(() => {
expect(mockPreload.cids).to.be.empty()

done()
}, 500)
})
})
})

0 comments on commit 5f66538

Please sign in to comment.