|
| 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