Remove PersistedMapStore

Reviewed By: cpojer

Differential Revision: D7628723

fbshipit-source-id: 0efb8d42da621870e46ab57a2c23775d5f0e373c
This commit is contained in:
Miguel Jimenez Esun 2018-04-14 05:17:52 -07:00 committed by Facebook Github Bot
parent 9922584f45
commit f5e93c6e91
3 changed files with 0 additions and 198 deletions

View File

@ -13,7 +13,6 @@
const Cache = require('./Cache');
const FileStore = require('./stores/FileStore');
const HttpStore = require('./stores/HttpStore');
const PersistedMapStore = require('./stores/PersistedMapStore');
const stableHash = require('./stableHash');
@ -22,6 +21,5 @@ export type {CacheStore} from './types.flow';
module.exports.Cache = Cache;
module.exports.FileStore = FileStore;
module.exports.HttpStore = HttpStore;
module.exports.PersistedMapStore = PersistedMapStore;
module.exports.stableHash = stableHash;

View File

@ -1,75 +0,0 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const fs = require('fs');
const serializer = require('jest-serializer');
export type Options = {|
path: string,
writeDelay: ?number,
|};
class PersistedMapStore {
_map: ?Map<string, mixed>;
_path: string;
_store: () => void;
_timeout: ?TimeoutID;
_writeDelay: number;
constructor(options: Options) {
this._path = options.path;
this._writeDelay = options.writeDelay || 5000;
this._store = this._store.bind(this);
this._timeout = null;
this._map = null;
}
get(key: Buffer): mixed {
this._getMap();
if (this._map) {
return this._map.get(key.toString('hex'));
}
return null;
}
set(key: Buffer, value: mixed) {
this._getMap();
if (this._map) {
this._map.set(key.toString('hex'), value);
}
if (!this._timeout) {
this._timeout = setTimeout(this._store, this._writeDelay);
}
}
_getMap() {
if (!this._map) {
if (fs.existsSync(this._path)) {
this._map = serializer.readFileSync(this._path);
} else {
this._map = new Map();
}
}
}
_store() {
serializer.writeFileSync(this._path, this._map);
this._timeout = null;
}
}
module.exports = PersistedMapStore;

View File

@ -1,121 +0,0 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+javascript_foundation
* @format
*/
'use strict';
describe('PersistedMapStore', () => {
const key1 = Buffer.from('foo');
const key2 = Buffer.from('bar');
let now;
let serializer;
let fs;
let PersistedMapStore;
function advance(time) {
now += time;
jest.advanceTimersByTime(time);
}
Date.now = () => now;
beforeEach(() => {
jest
.resetModules()
.resetAllMocks()
.useFakeTimers();
jest.mock('fs', () => ({
existsSync: jest.fn(),
}));
jest.mock('jest-serializer', () => ({
readFileSync: jest.fn(),
writeFileSync: jest.fn(),
}));
fs = require('fs');
serializer = require('jest-serializer');
PersistedMapStore = require('../PersistedMapStore');
now = 0;
});
it('ensures that the persisted map file is checked first', () => {
const store = new PersistedMapStore({path: '/foo'});
fs.existsSync.mockReturnValue(false);
store.get(key1);
expect(fs.existsSync).toHaveBeenCalledTimes(1);
expect(serializer.readFileSync).not.toBeCalled();
});
it('loads the file when it exists', () => {
const store = new PersistedMapStore({path: '/foo'});
fs.existsSync.mockReturnValue(true);
serializer.readFileSync.mockReturnValue(new Map());
store.get(key1);
expect(fs.existsSync).toHaveBeenCalledTimes(1);
expect(serializer.readFileSync).toHaveBeenCalledTimes(1);
expect(serializer.readFileSync.mock.calls[0]).toEqual(['/foo']);
});
it('throws if the file is invalid', () => {
const store = new PersistedMapStore({path: '/foo'});
fs.existsSync.mockReturnValue(true);
serializer.readFileSync.mockImplementation(() => {
throw new Error();
});
expect(() => store.get(key1)).toThrow();
});
it('deserializes and serializes correctly from/to disk', () => {
let file;
fs.existsSync.mockReturnValue(false);
serializer.readFileSync.mockImplementation(() => file);
serializer.writeFileSync.mockImplementation((_, data) => (file = data));
const store1 = new PersistedMapStore({path: '/foo'});
store1.set(key1, 'value1');
store1.set(key2, 123456);
// Force throttle to kick in and perform the file storage.
advance(7500);
fs.existsSync.mockReturnValue(true);
const store2 = new PersistedMapStore({path: '/foo'});
expect(store2.get(key1)).toBe('value1');
expect(store2.get(key2)).toBe(123456);
});
it('ensures that the throttling is working correctly', () => {
const store1 = new PersistedMapStore({
path: '/foo',
writeDelay: 1234,
});
// Triggers the write, multiple times (only one write should happen).
store1.set(key1, 'foo');
store1.set(key1, 'bar');
store1.set(key1, 'baz');
advance(1233);
expect(serializer.writeFileSync).toHaveBeenCalledTimes(0);
advance(1);
expect(serializer.writeFileSync).toHaveBeenCalledTimes(1);
});
});