Skip to content

Commit

Permalink
chore: fix flaky resource cache test case (#3124)
Browse files Browse the repository at this point in the history
* clean cache after each test case

* add changeset

* reduce test case ttl

* repating tests 20x for CI testing purposes

* use long ttl on test cases

* update test case

* add debug logs

* remove .only

* use beforeEach instead of afterEach

* increase repeats

* reduce ttl

* update test case

* add CI buffer to sleep

* use time mock instead of buffer

* restore all mocks

* use proper hook

* remove repeats 100

---------

Co-authored-by: Anderson Arboleya <[email protected]>
  • Loading branch information
Torres-ssf and arboleya authored Sep 8, 2024
1 parent fb4815e commit b4772ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .changeset/witty-masks-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: fix flaky resource cache test case
45 changes: 32 additions & 13 deletions packages/account/src/providers/resource-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,50 @@ describe('Resource Cache', () => {
expect(activeData.utxos).containSubset(EXPECTED.utxos);
});

it('should remove expired when getting active data', async () => {
const ttl = 1000;
it('should remove expired when getting active data', () => {
const ttl = 500;
const resourceCache = new ResourceCache(ttl);

const utxos = [randomValue(), randomValue()];
const messages = [randomValue()];

const txId1 = randomValue();
const txId1Resources = {
utxos: [randomValue()],
messages: [randomValue()],
utxos,
messages,
};

const originalTimeStamp = 946684800;
let dateSpy = vi.spyOn(Date, 'now').mockImplementation(() => originalTimeStamp);

resourceCache.set(txId1, txId1Resources);
let activeData = resourceCache.getActiveData();
const oldActiveData = resourceCache.getActiveData();

expect(activeData.utxos).containSubset(txId1Resources.utxos);
expect(activeData.messages).containSubset(txId1Resources.messages);
expect(dateSpy).toHaveBeenCalled();

await sleep(ttl);
expect(oldActiveData.utxos).containSubset(txId1Resources.utxos);
expect(oldActiveData.messages).containSubset(txId1Resources.messages);
expect(oldActiveData.messages).containSubset(txId1Resources.messages);

activeData = resourceCache.getActiveData();
const expiredTimeStamp = originalTimeStamp + ttl;
dateSpy = vi.spyOn(Date, 'now').mockImplementation(() => expiredTimeStamp);

const newActiveData = resourceCache.getActiveData();

txId1Resources.utxos.forEach((utxo) => {
expect(newActiveData.utxos).not.includes(utxo);
});

expect(activeData.utxos.length).toEqual(0);
expect(activeData.messages.length).toEqual(0);
txId1Resources.messages.forEach((message) => {
expect(newActiveData.utxos).not.includes(message);
});

vi.restoreAllMocks();
});

it('should remove cached data based on transaction ID', () => {
const ttl = 1000;
// use long ttl to avoid cache expiration
const ttl = 10_000;
const resourceCache = new ResourceCache(ttl);

const txId1 = randomValue();
Expand Down Expand Up @@ -140,7 +158,8 @@ describe('Resource Cache', () => {
});

it('can clear cache', () => {
const resourceCache = new ResourceCache(1000);
// use long ttl to avoid cache expiration
const resourceCache = new ResourceCache(10_000);

const txId1 = randomValue();
const txId2 = randomValue();
Expand Down

0 comments on commit b4772ab

Please sign in to comment.