Skip to content

Commit

Permalink
Less is more. Reuse sleep fn.
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenay committed Jan 19, 2024
1 parent 2b25eda commit 620dd3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
18 changes: 5 additions & 13 deletions test/caching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,11 @@ describe('caching', () => {
});

await cache.wrap('refreshThreshold', async () => 0);
await new Promise((resolve) => {
setTimeout(resolve, 2 * 1000);
});
await sleep(2 * 1000);
await cache.wrap('refreshThreshold', async () => 1);
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await sleep(500);
await cache.wrap('refreshThreshold', async () => 2);
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await sleep(500);
return cache.wrap('refreshThreshold', async () => 3);
})(),
).resolves.toEqual(1);
Expand All @@ -404,22 +398,20 @@ describe('caching', () => {
resolve(value);
}, timeout),
);
const delay = (timeout: number) =>
new Promise((resolve) => setTimeout(resolve, timeout));

let value = await cache.wrap(key, resolveAfter(100, 1));
expect(value).toEqual(1);
expect(callCount).toEqual(1);

await delay(1100);
await sleep(1100);
for (let i = 0; i < 6; i++) {
// Only the first fn should be called - returning 2
value = await cache.wrap(key, resolveAfter(2000, 2 + i));
expect(value).toEqual(1);
expect(callCount).toEqual(1);
}

await delay(2100);
await sleep(2100);
value = await cache.wrap(key, resolveAfter(2000, 8));
expect(value).toEqual(2);
expect(callCount).toEqual(2);
Expand Down
12 changes: 3 additions & 9 deletions test/multi-caching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,11 @@ describe('multiCaching', () => {
const multi = multiCaching([cache0, cache1]);

await multi.wrap('refreshThreshold', async () => 0);
await new Promise((resolve) => {
setTimeout(resolve, 2 * 1000);
});
await sleep(2 * 1000);
await multi.wrap('refreshThreshold', async () => 1);
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await sleep(500);
await multi.wrap('refreshThreshold', async () => 2);
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await sleep(500);
return multi.wrap('refreshThreshold', async () => 3);
})(),
).resolves.toEqual(1);
Expand Down

0 comments on commit 620dd3f

Please sign in to comment.