From b4772abf0fdffb9289e8ce775dfdbc70da1bd07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Sun, 8 Sep 2024 15:42:53 -0300 Subject: [PATCH] chore: fix flaky resource cache test case (#3124) * 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 --- .changeset/witty-masks-hide.md | 4 ++ .../src/providers/resource-cache.test.ts | 45 +++++++++++++------ 2 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 .changeset/witty-masks-hide.md diff --git a/.changeset/witty-masks-hide.md b/.changeset/witty-masks-hide.md new file mode 100644 index 00000000000..ee074cccb50 --- /dev/null +++ b/.changeset/witty-masks-hide.md @@ -0,0 +1,4 @@ +--- +--- + +chore: fix flaky resource cache test case diff --git a/packages/account/src/providers/resource-cache.test.ts b/packages/account/src/providers/resource-cache.test.ts index 5ee63be5054..9b024055d1a 100644 --- a/packages/account/src/providers/resource-cache.test.ts +++ b/packages/account/src/providers/resource-cache.test.ts @@ -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(); @@ -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();