Skip to content
This repository was archived by the owner on Apr 22, 2026. It is now read-only.

Commit f19a20a

Browse files
author
Iztok
committed
test fix - issue with deep copy of closures in async cache
1 parent 32e1b77 commit f19a20a

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

packages/fasset-bots-common/src/utils/AsyncCache.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ export interface CacheEntry<R> {
66
}
77

88
export class AsyncCache<ARGS extends CachedMethodArg[], R> {
9+
static deepCopyWithObjectCreate = true;
10+
911
private cache: Map<string, CacheEntry<unknown>>;
1012

1113
constructor(
12-
public readonly maxAgeMS: number,
13-
public readonly computeValue: (...args: ARGS) => Promise<R>,
14+
public maxAgeMS: number,
15+
public computeValue: (...args: ARGS) => Promise<R>,
16+
public thisArg?: unknown,
1417
) {
1518
this.cache = new Map();
1619
}
@@ -21,7 +24,7 @@ export class AsyncCache<ARGS extends CachedMethodArg[], R> {
2124
if (entry != null && entry.expiresAt > Date.now()) {
2225
return entry.value as R;
2326
} else {
24-
const value = await this.computeValue(...args);
27+
const value = await this.computeValue.apply(this.thisArg, args);
2528
const expiresAt = Date.now() + this.maxAgeMS;
2629
this.cache.set(key, { value, expiresAt });
2730
return value;

packages/fasset-bots-common/test/unit/utils/AsyncCache.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,17 @@ describe('AsyncCache', () => {
9191
expect(v1b).to.equal(1);
9292
expect(compute.callCount).to.equal(2);
9393
});
94+
95+
it('supports thisArg for computeValue', async () => {
96+
const context = {
97+
factor: 10,
98+
async compute(n: number): Promise<number> {
99+
return n * this.factor;
100+
}
101+
};
102+
const cache = new AsyncCache<[number], number>(1000, context.compute, context);
103+
104+
const result = await cache.getOrCompute(5);
105+
expect(result).to.equal(50);
106+
});
94107
});

packages/fasset-bots-core/src/actors/AgentBotMinting.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ export class AgentBotMinting {
179179

180180
// for minting checks, 1 refresh of finalized block per minute is enough
181181
lastFinalizedUnderlyingBlockCached = new AsyncCache(1 * MINUTES_MS,
182-
async () => await lastFinalizedUnderlyingBlock(this.context.blockchainIndexer));
182+
async function (this: AgentBotMinting) { return await lastFinalizedUnderlyingBlock(this.context.blockchainIndexer); },
183+
this);
183184

184185
/**
185186
* Check if time for payment expired on underlying. If if did not expire, then it does nothing.

packages/fasset-bots-core/test-hardhat/integration/agentBot.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe("Agent bot tests", () => {
8181
await testWhitelistAndSetWorkAddress(context.agentOwnerRegistry, ownerManagementAddress, ownerManagementPrivateKey, ownerAddress);
8282
//
8383
agentBot = await createTestAgentBotAndMakeAvailable(context, governance, orm, ownerManagementAddress, undefined, false);
84+
agentBot.minting.lastFinalizedUnderlyingBlockCached.maxAgeMS = 0; // disable caching for tests
8485
minter = await createTestMinter(context, minterAddress, chain);
8586
redeemer = await createTestRedeemer(context, redeemerAddress);
8687
await proveAndUpdateUnderlyingBlock(context.attestationProvider, context.assetManager, ownerAddress);

0 commit comments

Comments
 (0)