From a5425430217cc1c4b747f3d0511b2b76a519cfd2 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 28 Nov 2018 07:21:26 +0000 Subject: [PATCH] fix: allow disabling mfs preload from config --- src/core/config.js | 3 +- src/core/mfs-preload.js | 12 ++++++-- test/core/mfs-preload.spec.js | 52 +++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/core/config.js b/src/core/config.js index 8c34ad51cd..a39432d53e 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -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(), diff --git a/src/core/mfs-preload.js b/src/core/mfs-preload.js index 4f0cfd16a5..850830afbe 100644 --- a/src/core/mfs-preload.js +++ b/src/core/mfs-preload.js @@ -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 diff --git a/test/core/mfs-preload.spec.js b/test/core/mfs-preload.spec.js index 0b47551856..97708f8fad 100644 --- a/test/core/mfs-preload.spec.js +++ b/test/core/mfs-preload.spec.js @@ -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() @@ -56,4 +72,22 @@ describe('MFS preload', () => { }) }) }) + + it('should disable preloading MFS', function (done) { + this.timeout(80 * 1000) + + 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) + }) + }) })