Skip to content

Commit

Permalink
chore: use explicit resource management on resource-ache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Dec 9, 2024
1 parent 1e2051e commit 830646d
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/account/src/providers/resource-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { ResourceCache } from './resource-cache';
describe('Resource Cache', () => {
const randomValue = () => hexlify(randomBytes(32));

afterEach(() => {
vi.clearAllMocks();
});

it('should throw error if TTL is not a positive number', () => {
expect(() => new ResourceCache(0)).toThrow(
new FuelError(ErrorCode.INVALID_TTL, 'Invalid TTL: 0. Use a value greater than zero.')
Expand Down Expand Up @@ -38,7 +42,7 @@ describe('Resource Cache', () => {
});

it('can validade if it is cached [UTXO]', () => {
const resourceCache = new ResourceCache(1000);
using resourceCache = new ResourceCache(1000);
const utxoId = randomValue();

expect(resourceCache.isCached(utxoId)).toBeFalsy();
Expand All @@ -50,7 +54,7 @@ describe('Resource Cache', () => {
});

it('can validade if it is cached [Message]', () => {
const resourceCache = new ResourceCache(1000);
using resourceCache = new ResourceCache(1000);
const messageNonce = randomValue();

expect(resourceCache.isCached(messageNonce)).toBeFalsy();
Expand All @@ -63,7 +67,7 @@ describe('Resource Cache', () => {

it('can get active [no data]', async () => {
const EXPECTED = { utxos: [], messages: [] };
const resourceCache = new ResourceCache(1);
using resourceCache = new ResourceCache(1);

await sleep(1);

Expand All @@ -75,7 +79,7 @@ describe('Resource Cache', () => {
utxos: [randomValue(), randomValue()],
messages: [randomValue(), randomValue(), randomValue()],
};
const resourceCache = new ResourceCache(1000);
using resourceCache = new ResourceCache(1000);

const txId = randomValue();
resourceCache.set(txId, EXPECTED);
Expand All @@ -88,7 +92,7 @@ describe('Resource Cache', () => {

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

const utxos = [randomValue(), randomValue()];
const messages = [randomValue()];
Expand Down Expand Up @@ -130,7 +134,7 @@ describe('Resource Cache', () => {
it('should remove cached data based on transaction ID', () => {
// use long ttl to avoid cache expiration
const ttl = 10_000;
const resourceCache = new ResourceCache(ttl);
using resourceCache = new ResourceCache(ttl);

const txId1 = randomValue();
const txId2 = randomValue();
Expand Down Expand Up @@ -169,7 +173,7 @@ describe('Resource Cache', () => {

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

const txId1 = randomValue();
const txId2 = randomValue();
Expand Down Expand Up @@ -216,7 +220,7 @@ describe('Resource Cache', () => {
messages: [randomValue(), randomValue()],
};

const newInstance = new ResourceCache(300);
using newInstance = new ResourceCache(300);
newInstance.set(newTxId, newCache);

const activeData = newInstance.getActiveData();
Expand Down

0 comments on commit 830646d

Please sign in to comment.