initial commit taken from gitlab.lrz.de

This commit is contained in:
privatereese
2018-08-24 18:09:42 +02:00
parent ae54ed4c48
commit fc05486403
28494 changed files with 2159823 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
/**
* 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
* @flow
*/
'use strict';
const Cache = require('../Cache');
describe('Cache', () => {
function createStore(i) {
return {
get: jest.fn().mockImplementation(() => null),
set: jest.fn(),
};
}
afterEach(() => {
jest.restoreAllMocks();
});
it('returns null when no result is found', async () => {
const store1 = createStore();
const store2 = createStore();
const cache = new Cache([store1, store2]);
// Calling a wrapped method.
const result = await cache.get(Buffer.from('foo'));
expect(result).toBe(null);
expect(store1.get).toHaveBeenCalledTimes(1);
expect(store2.get).toHaveBeenCalledTimes(1);
});
it('sequentially searches up until it finds a valid result', async () => {
const store1 = createStore(1);
const store2 = createStore(2);
const store3 = createStore(3);
const cache = new Cache([store1, store2, store3]);
// Only cache 2 can return results.
store2.get.mockImplementation(() => 'hit!');
const result = await cache.get(Buffer.from('foo'));
expect(result).toBe('hit!');
expect(store1.get).toHaveBeenCalledTimes(1);
expect(store2.get).toHaveBeenCalledTimes(1);
expect(store3.get).not.toHaveBeenCalled();
});
it('skips all cache stores when a hit is produced, based on the same key', () => {
const store1 = createStore();
const store2 = createStore();
const store3 = createStore();
const cache = new Cache([store1, store2, store3]);
const key = Buffer.from('foo');
store2.get.mockImplementation(() => 'hit!');
// Get and set. Set should only affect store 1, not 2 (hit) and 3 (after).
cache.get(key);
cache.set(key);
expect(store1.set).toHaveBeenCalledTimes(1);
expect(store2.set).not.toHaveBeenCalled();
expect(store3.set).not.toHaveBeenCalled();
});
it('awaits for promises on stores, even if they return undefined', async () => {
let resolve;
const store1 = createStore();
const store2 = createStore();
const promise = new Promise((res, rej) => (resolve = res));
const cache = new Cache([store1, store2]);
store1.get.mockImplementation(() => promise);
const get = cache.get(Buffer.from('foo'));
// Store 1 returns a promise, so store 2 is not called until it resolves.
expect(store1.get).toHaveBeenCalledTimes(1);
expect(store2.get).not.toHaveBeenCalled();
if (!resolve) {
throw new Error('Flow needs this');
}
resolve(undefined);
await Promise.all([promise, get]);
expect(store1.get).toHaveBeenCalledTimes(1);
expect(store2.get).toHaveBeenCalledTimes(1);
});
it('throws on a buggy store set', async () => {
jest.useFakeTimers();
const store1 = createStore();
const store2 = createStore();
const cache = new Cache([store1, store2]);
let error = null;
store1.set.mockImplementation(() => null);
store2.set.mockImplementation(() => Promise.reject(new RangeError('foo')));
try {
cache.set(Buffer.from('foo'), 'arg');
jest.runAllTimers();
} catch (err) {
error = err;
}
expect(error).toBeInstanceOf(RangeError);
});
it('throws on a buggy store get', async () => {
const store1 = createStore();
const store2 = createStore();
const cache = new Cache([store1, store2]);
let error = null;
store1.get.mockImplementation(() => null);
store2.get.mockImplementation(() => Promise.reject(new TypeError('bar')));
try {
await cache.get(Buffer.from('foo'));
} catch (err) {
error = err;
}
expect(error).toBeInstanceOf(TypeError);
});
});

View File

@@ -0,0 +1,121 @@
/**
* 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);
});
});

View File

@@ -0,0 +1,37 @@
/**
* 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';
const stableHash = require('../stableHash');
describe('stableHash', () => {
it('ensures that the hash implementation supports switched order properties', () => {
const sortedHash = stableHash({
a: 3,
b: 4,
c: {
d: 'd',
e: 'e',
},
});
const unsortedHash = stableHash({
b: 4,
c: {
e: 'e',
d: 'd',
},
a: 3,
});
expect(unsortedHash).toEqual(sortedHash);
});
});