Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Use Sinon Fake Timer to Avoid Flaky Time Issue #1667

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions test/test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
// limitations under the License.

import {strict as assert} from 'assert';
import * as sinon from 'sinon';

import {LRUCache} from '../src/util';

describe('util', () => {
let sandbox: sinon.SinonSandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
});

afterEach(() => {
sandbox.restore();
});

describe('LRUCache', () => {
it('should set and get a cached item', () => {
const expected = 'value';
Expand Down Expand Up @@ -50,18 +61,23 @@ describe('util', () => {
it('should evict items older than a supplied `maxAge`', async () => {
const maxAge = 50;

sandbox.clock = sinon.useFakeTimers();

const lru = new LRUCache({capacity: 5, maxAge});

lru.set('first', 1);
lru.set('second', 2);

await new Promise(res => setTimeout(res, maxAge + 1));
// back to the future 🏎️
sandbox.clock.tick(maxAge + 1);

// just set, so should be fine
lru.set('third', 3);
assert.equal(lru.get('third'), 3);

// these are too old
assert.equal(lru.get('first'), undefined);
assert.equal(lru.get('second'), undefined);
assert.equal(lru.get('third'), 3);
});
});
});