Skip to content

Commit 4fb1175

Browse files
committed
refactor: clean up
1 parent 2ec3797 commit 4fb1175

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

packages/actor-memory-expression/src/memory_calculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const customGetFunc = (obj: any, path: string, defaultVal?: number) => {
103103
* @returns The closest power of 2 within min/max range.
104104
*/
105105
const roundToClosestPowerOf2 = (num: number): number => {
106-
if (typeof num !== 'number' || Number.isNaN(num)) {
106+
if (typeof num !== 'number' || Number.isNaN(num) || !Number.isFinite(num)) {
107107
throw new Error(`Calculated memory value is not a valid number: ${num}.`);
108108
}
109109

packages/actor-memory-expression/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export type MemoryEvaluationContext = {
1818
export type CompilationCache = {
1919
get: (expression: string) => Promise<EvalFunction | null>;
2020
set: (expression: string, compilationResult: EvalFunction) => Promise<void>;
21-
length: () => Promise<number>;
21+
size: () => Promise<number>;
2222
}

test/memory_calculator.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ describe('calculateDefaultMemoryFromExpression', () => {
196196
await expect(calculateRunDynamicMemory('0 / 0', emptyContext)).rejects.toThrow('Calculated memory value is not a valid number: NaN.');
197197
});
198198

199+
it('should throw error if result is Infinity', async () => {
200+
await expect(calculateRunDynamicMemory('Infinity', emptyContext)).rejects.toThrow('Calculated memory value is not a valid number: Infinity.');
201+
await expect(calculateRunDynamicMemory('-Infinity', emptyContext)).rejects.toThrow('Calculated memory value is not a valid number: -Infinity.');
202+
});
203+
199204
it('should throw error if result is a non-numeric (string)', async () => {
200205
await expect(calculateRunDynamicMemory("'hello'", emptyContext)).rejects.toThrow('Calculated memory value is not a valid number: hello.');
201206
});
@@ -215,29 +220,29 @@ describe('calculateDefaultMemoryFromExpression', () => {
215220
cache = {
216221
get: async (expression: string) => lruCache.get(expression),
217222
set: async (expression: string, compilationResult: EvalFunction) => { lruCache.add(expression, compilationResult); },
218-
length: async () => lruCache.length(),
223+
size: async () => lruCache.length(),
219224
};
220225
});
221226

222227
it('correctly works with cache passed in options', async () => {
223-
expect(await cache.length()).toBe(0);
228+
expect(await cache.size()).toBe(0);
224229

225230
// First call - cache miss
226231
const result1 = await calculateRunDynamicMemory(expr, context, { cache });
227232
expect(result1).toBe(8192);
228-
expect(await cache.length()).toBe(1); // Expression is now cached
233+
expect(await cache.size()).toBe(1); // Expression is now cached
229234

230235
// Second call - cache hit
231236
const result2 = await calculateRunDynamicMemory(expr, context, { cache });
232237
expect(result2).toBe(8192);
233-
expect(await cache.length()).toBe(1); // Cache length is unchanged
238+
expect(await cache.size()).toBe(1); // Cache length is unchanged
234239
});
235240

236241
it('should cache different expressions separately', async () => {
237242
const expr2 = 'input.size * 2048'; // 10 * 2048 = 20480 -> 16384
238243
await calculateRunDynamicMemory(expr, context, { cache });
239244
await calculateRunDynamicMemory(expr2, context, { cache });
240-
expect(await cache.length()).toBe(2);
245+
expect(await cache.size()).toBe(2);
241246
});
242247
});
243248
});

0 commit comments

Comments
 (0)