Skip to content

Commit 7e5d5b0

Browse files
committed
From testing and tests
1 parent 09efb40 commit 7e5d5b0

3 files changed

Lines changed: 582 additions & 31 deletions

File tree

cache/__tests__/cache-limits.test.js

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* @author thehabes
55
*/
66

7+
// Ensure cache runs in local mode (not PM2 cluster) for tests
8+
// This must be set before importing cache to avoid IPC timeouts
9+
delete process.env.pm_id
10+
711
import { jest } from '@jest/globals'
812
import cache from '../index.js'
913

@@ -91,19 +95,19 @@ describe('Cache TTL (Time-To-Live) Limit Enforcement', () => {
9195
const testId = Date.now()
9296

9397
// Set entries with short TTL
94-
await cache.clusterCache.set(
95-
cache.generateKey('query', { type: 'Test', testId }),
96-
[{ id: 1 }],
98+
await cache.set(
99+
cache.generateKey('query', { type: 'Test', testId }),
100+
[{ id: 1 }],
97101
shortTTL
98102
)
99-
await cache.clusterCache.set(
100-
cache.generateKey('search', { searchText: 'test', testId }),
101-
[{ id: 2 }],
103+
await cache.set(
104+
cache.generateKey('search', { searchText: 'test', testId }),
105+
[{ id: 2 }],
102106
shortTTL
103107
)
104-
await cache.clusterCache.set(
105-
cache.generateKey('id', `ttl-${testId}`),
106-
{ id: 3 },
108+
await cache.set(
109+
cache.generateKey('id', `ttl-${testId}`),
110+
{ id: 3 },
107111
shortTTL
108112
)
109113
await waitForCache(50)
@@ -301,4 +305,76 @@ describe('Cache Limit Breaking Change Detection', () => {
301305
expect(cache.maxBytes).toBeGreaterThan(0)
302306
expect(cache.ttl).toBeGreaterThan(0)
303307
})
308+
309+
it('should correctly calculate size for deeply nested query objects', async () => {
310+
await cache.clear()
311+
312+
// Create queries with deeply nested properties (5+ levels)
313+
const deeplyNestedQuery = cache.generateKey('query', {
314+
__cached: {
315+
'level1.level2.level3.level4.level5': 'deepValue',
316+
'body.target.source.metadata.author.name': 'John Doe',
317+
'nested.array.0.property.value': 123
318+
},
319+
limit: 100,
320+
skip: 0
321+
})
322+
323+
// Create a large result set with nested objects
324+
const nestedResults = Array.from({ length: 50 }, (_, i) => ({
325+
id: `obj${i}`,
326+
level1: {
327+
level2: {
328+
level3: {
329+
level4: {
330+
level5: 'deepValue',
331+
additionalData: new Array(100).fill('x').join('')
332+
}
333+
}
334+
}
335+
},
336+
body: {
337+
target: {
338+
source: {
339+
metadata: {
340+
author: {
341+
name: 'John Doe',
342+
email: 'john@example.com'
343+
}
344+
}
345+
}
346+
}
347+
}
348+
}))
349+
350+
await cache.set(deeplyNestedQuery, nestedResults)
351+
352+
// Verify the cache entry exists
353+
expect(await cache.get(deeplyNestedQuery)).not.toBeNull()
354+
355+
// Add more deeply nested queries until we approach maxBytes
356+
const queries = []
357+
for (let i = 0; i < 10; i++) {
358+
const key = cache.generateKey('query', {
359+
__cached: {
360+
[`level1.level2.level3.property${i}`]: `value${i}`,
361+
'deep.nested.structure.array.0.id': i
362+
},
363+
limit: 100,
364+
skip: 0
365+
})
366+
queries.push(key)
367+
await cache.set(key, nestedResults)
368+
}
369+
370+
// Verify cache entries exist - check a few queries to confirm caching works
371+
expect(await cache.get(deeplyNestedQuery)).not.toBeNull()
372+
expect(await cache.get(queries[queries.length - 1])).not.toBeNull()
373+
374+
// Verify maxBytes enforcement: cache operations should continue working
375+
// even if some entries were evicted due to byte limits
376+
const midpoint = Math.floor(queries.length / 2)
377+
expect(await cache.get(queries[midpoint])).toBeTruthy()
378+
})
304379
})
380+

0 commit comments

Comments
 (0)