This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure MFS is continually preloaded
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) |