Skip to content

Commit

Permalink
test(node): Use fake timers to speed up unit tests (#13607)
Browse files Browse the repository at this point in the history
Changes the rate-limiting tests to use fake timers. This takes the test
time from 24s to 4s.
  • Loading branch information
timfish committed Sep 9, 2024
1 parent 4e9533e commit 15dd865
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/node/test/integrations/localvariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRateLimiter } from '../../src/integrations/local-variables/common
import { createCallbackList } from '../../src/integrations/local-variables/local-variables-sync';
import { NODE_MAJOR } from '../../src/nodeVersion';

jest.setTimeout(20_000);
jest.useFakeTimers();

const describeIf = (condition: boolean) => (condition ? describe : describe.skip);

Expand Down Expand Up @@ -87,10 +87,13 @@ describeIf(NODE_MAJOR >= 18)('LocalVariables', () => {

for (let i = 0; i < 7; i++) {
increment();
jest.advanceTimersByTime(100);
}

jest.advanceTimersByTime(1_000);
});

it('does not call disable if not exceeded', done => {
it('does not call disable if not exceeded', () => {
const increment = createRateLimiter(
5,
() => {
Expand All @@ -101,20 +104,17 @@ describeIf(NODE_MAJOR >= 18)('LocalVariables', () => {
},
);

let count = 0;

const timer = setInterval(() => {
for (let i = 0; i < 4; i++) {
increment();
}
for (let i = 0; i < 4; i++) {
increment();
jest.advanceTimersByTime(200);
}

count += 1;
jest.advanceTimersByTime(600);

if (count >= 5) {
clearInterval(timer);
done();
}
}, 1_000);
for (let i = 0; i < 4; i++) {
increment();
jest.advanceTimersByTime(200);
}
});

it('re-enables after timeout', done => {
Expand All @@ -134,7 +134,10 @@ describeIf(NODE_MAJOR >= 18)('LocalVariables', () => {

for (let i = 0; i < 10; i++) {
increment();
jest.advanceTimersByTime(100);
}

jest.advanceTimersByTime(10_000);
});
});
});

0 comments on commit 15dd865

Please sign in to comment.