Skip to content

Commit

Permalink
Merge pull request #1384 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu committed Dec 5, 2022
2 parents d951b95 + cf77fcb commit 555dd90
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions .structignore
@@ -0,0 +1 @@
test/util/cache-clearer-disabled.spec.js
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -176,6 +176,13 @@ loaded from `envVarsFile` (if allowed).

To allow overwriting of environment variables, prefix the name of the environment variable with `^`.

#### clearCache

Type: `boolean`<br>
Default: `true`

Known accessed caches will be cleared after test has executed when set to `true`.

#### timestamp

Type: `number|string`<br>
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
"joi-strict": "2.0.1",
"lodash.clonedeep": "4.5.0",
"lodash.get": "4.4.2",
"lru-cache-ext": "3.0.2",
"minimist": "1.2.6",
"nock": "13.2.4",
"object-scan": "18.0.1",
Expand Down
57 changes: 57 additions & 0 deletions src/modules/cache-clearer.js
@@ -0,0 +1,57 @@
import assert from 'assert';
import LRU from 'lru-cache-ext';

const getFns = (obj) => {
const result = [];
const properties = [];
let o = obj;
while (o instanceof Object) {
properties.push(...Object.getOwnPropertyNames(o));
o = Object.getPrototypeOf(o);
}
for (let i = 0; i < properties.length; i += 1) {
const key = properties[i];
const value = LRU.prototype[key];
if (typeof value !== 'function') {
// eslint-disable-next-line no-continue
continue;
}
result.push({ obj, key, value });
}
return result;
};

export default () => {
let injected = false;
const fns = [
...getFns(LRU.prototype)
];
const caches = [];

return {
inject: () => {
assert(injected === false);
fns.forEach(({ obj, key, value }) => {
try {
// eslint-disable-next-line no-param-reassign,func-names
obj[key] = function (...args) {
caches.push(this);
return value.call(this, ...args);
};
} catch (e) { /* ignored */ }
});
injected = true;
},
release: () => {
assert(injected === true);
fns.forEach(({ obj, key, value }) => {
try {
// eslint-disable-next-line no-param-reassign
obj[key] = value;
} catch (e) { /* ignored */ }
});
caches.splice(0).forEach((c) => c.clear());
injected = false;
}
};
};
13 changes: 13 additions & 0 deletions src/util/desc.js
Expand Up @@ -12,6 +12,7 @@ import EnvManager from '../modules/env-manager.js';
import TimeKeeper from '../modules/time-keeper.js';
import LogRecorder from '../modules/log-recorder.js';
import RandomSeeder from '../modules/random-seeder.js';
import CacheClearer from '../modules/cache-clearer.js';
import { getParents, genCassetteName } from './mocha-test.js';

const mocha = {
Expand Down Expand Up @@ -53,6 +54,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
fixtureFolder: Joi.string().optional(),
envVarsFile: Joi.string().optional(),
envVars: Joi.object().optional().unknown(true).pattern(Joi.string(), Joi.string()),
clearCache: Joi.boolean().optional(),
timestamp: Joi.alternatives(
Joi.number().integer().min(0),
Joi.date().iso()
Expand All @@ -75,6 +77,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
const fixtureFolder = resolve(get(opts, 'fixtureFolder', '$FILENAME__fixtures'));
const envVarsFile = resolve(get(opts, 'envVarsFile', '$FILENAME.env.yml'));
const envVars = get(opts, 'envVars', null);
const clearCache = get(opts, 'clearCache', true);
const timestamp = get(opts, 'timestamp', null);
const record = get(opts, 'record', false);
const cryptoSeed = get(opts, 'cryptoSeed', null);
Expand All @@ -84,6 +87,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {

let dir = null;
let requestRecorder = null;
let cacheClearer = null;
let envManagerFile = null;
let envManagerDesc = null;
let timeKeeper = null;
Expand Down Expand Up @@ -130,6 +134,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
// eslint-disable-next-line func-names
mocha.before(function () {
return (async () => {
if (clearCache !== false) {
cacheClearer = CacheClearer();
}
if (getParents(this.test).length === 3 && fs.existsSync(envVarsFile)) {
envManagerFile = EnvManager({ envVars: fs.smartRead(envVarsFile), allowOverwrite: false });
envManagerFile.apply();
Expand Down Expand Up @@ -190,6 +197,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
// eslint-disable-next-line func-names
mocha.beforeEach(function () {
return (async () => {
if (cacheClearer !== null) {
cacheClearer.inject();
}
if (useTmpDir === true) {
tmp.setGracefulCleanup();
dir = tmp.dirSync({ keep: false, unsafeCleanup: true }).name;
Expand Down Expand Up @@ -222,6 +232,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
if (dir !== null) {
dir = null;
}
if (cacheClearer !== null) {
cacheClearer.release();
}
})();
});

Expand Down
19 changes: 19 additions & 0 deletions test/util/cache-clearer-disabled.spec.js
@@ -0,0 +1,19 @@
import LRU from 'lru-cache-ext';
import { expect } from 'chai';
import describe from '../../src/util/desc.js';

describe('Testing Clear Cache Disabled', { clearCache: false }, () => {
let cache;
before(() => {
cache = new LRU({ ttl: 100, max: 100 });
});

it('First', () => {
expect(cache.has('a')).to.equal(false);
cache.set('a', 1);
});

it('Second', () => {
expect(cache.has('a')).to.equal(true);
});
});
18 changes: 18 additions & 0 deletions test/util/desc.spec.js
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import axios from 'axios';
import fancyLog from 'fancy-log';
import { expect } from 'chai';
import LRU from 'lru-cache-ext';
import describe from '../../src/util/desc.js';

const dirPrefix = path.join(os.tmpdir(), 'tmp-');
Expand Down Expand Up @@ -261,4 +262,21 @@ describe('Testing { describe }', () => {
state.push('testTwo');
});
});

describe('Testing Clear Cache', () => {
let cache;
before(() => {
cache = new LRU({ ttl: 100, max: 100 });
});

it('First', () => {
expect(cache.has('a')).to.equal(false);
cache.set('a', 1);
});

it('Second', () => {
expect(cache.has('a')).to.equal(false);
cache.set('a', 1);
});
});
});
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -2734,6 +2734,18 @@ lowercase-keys@^1.0.0:
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.1.tgz#6f9e30b47084d971a7c820ff15a6c5167b74c26f"
integrity sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==

lru-cache-ext@3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lru-cache-ext/-/lru-cache-ext-3.0.2.tgz#5dc1fb94bc22431c8a8fe602226155838d940b52"
integrity sha512-PQ0kInomO2I3HZuiagNTkaNyiDQkcIyeEahHRWSBVsEaxPqXb4FTPU4ajl9znX+C5antzQ0UWcfWrxy8kAhUrw==
dependencies:
lru-cache "7.8.1"

lru-cache@7.8.1:
version "7.8.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.8.1.tgz#68ee3f4807a57d2ba185b7fd90827d5c21ce82bb"
integrity sha512-E1v547OCgJvbvevfjgK9sNKIVXO96NnsTsFPBlg4ZxjhsJSODoH9lk8Bm0OxvHNm6Vm5Yqkl/1fErDxhYL8Skg==

lru-cache@^4.0.0, lru-cache@^4.0.1:
version "4.1.5"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd"
Expand Down

0 comments on commit 555dd90

Please sign in to comment.