-
-
Notifications
You must be signed in to change notification settings - Fork 687
Add ability to detect when MemoryCacheStore reaches max size #4224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| 'use strict' | ||
|
|
||
| const { describe, test } = require('node:test') | ||
| const { equal } = require('node:assert') | ||
| const MemoryCacheStore = require('../lib/cache/memory-cache-store') | ||
|
|
||
| describe('Cache Store', () => { | ||
| test('size getter returns correct total size', async () => { | ||
| const store = new MemoryCacheStore() | ||
| const testData = 'test data' | ||
|
|
||
| equal(store.size, 0, 'Initial size should be 0') | ||
|
|
||
| const writeStream = store.createWriteStream( | ||
| { origin: 'test', path: '/', method: 'GET' }, | ||
| { | ||
| statusCode: 200, | ||
| statusMessage: 'OK', | ||
| headers: {}, | ||
| cachedAt: Date.now(), | ||
| staleAt: Date.now() + 1000, | ||
| deleteAt: Date.now() + 2000 | ||
| } | ||
| ) | ||
|
|
||
| writeStream.write(testData) | ||
| writeStream.end() | ||
|
|
||
| equal(store.size, testData.length, 'Size should match written data length') | ||
| }) | ||
|
|
||
| test('isFull returns false when under limits', () => { | ||
| const store = new MemoryCacheStore({ | ||
| maxSize: 1000, | ||
| maxCount: 10 | ||
| }) | ||
|
|
||
| equal(store.isFull(), false, 'Should not be full when empty') | ||
| }) | ||
|
|
||
| test('isFull returns true when maxSize reached', async () => { | ||
| const maxSize = 10 | ||
| const store = new MemoryCacheStore({ maxSize }) | ||
| const testData = 'x'.repeat(maxSize + 1) // Exceed maxSize | ||
|
|
||
| const writeStream = store.createWriteStream( | ||
| { origin: 'test', path: '/', method: 'GET' }, | ||
| { | ||
| statusCode: 200, | ||
| statusMessage: 'OK', | ||
| headers: {}, | ||
| cachedAt: Date.now(), | ||
| staleAt: Date.now() + 1000, | ||
| deleteAt: Date.now() + 2000 | ||
| } | ||
| ) | ||
|
|
||
| writeStream.write(testData) | ||
| writeStream.end() | ||
|
|
||
| equal(store.isFull(), true, 'Should be full when maxSize exceeded') | ||
| }) | ||
|
|
||
| test('isFull returns true when maxCount reached', async () => { | ||
| const maxCount = 2 | ||
| const store = new MemoryCacheStore({ maxCount }) | ||
|
|
||
| // Add maxCount + 1 entries | ||
| for (let i = 0; i <= maxCount; i++) { | ||
| const writeStream = store.createWriteStream( | ||
| { origin: 'test', path: `/${i}`, method: 'GET' }, | ||
| { | ||
| statusCode: 200, | ||
| statusMessage: 'OK', | ||
| headers: {}, | ||
| cachedAt: Date.now(), | ||
| staleAt: Date.now() + 1000, | ||
| deleteAt: Date.now() + 2000 | ||
| } | ||
| ) | ||
| writeStream.end('test') | ||
| } | ||
|
|
||
| equal(store.isFull(), true, 'Should be full when maxCount exceeded') | ||
| }) | ||
|
|
||
| test('emits maxSizeExceeded event when limits exceeded', async () => { | ||
| const maxSize = 10 | ||
| const store = new MemoryCacheStore({ maxSize }) | ||
|
|
||
| let eventFired = false | ||
| let eventPayload = null | ||
|
|
||
| store.on('maxSizeExceeded', (payload) => { | ||
| eventFired = true | ||
| eventPayload = payload | ||
| }) | ||
|
|
||
| const testData = 'x'.repeat(maxSize + 1) // Exceed maxSize | ||
|
|
||
| const writeStream = store.createWriteStream( | ||
| { origin: 'test', path: '/', method: 'GET' }, | ||
| { | ||
| statusCode: 200, | ||
| statusMessage: 'OK', | ||
| headers: {}, | ||
| cachedAt: Date.now(), | ||
| staleAt: Date.now() + 1000, | ||
| deleteAt: Date.now() + 2000 | ||
| } | ||
| ) | ||
|
|
||
| writeStream.write(testData) | ||
| writeStream.end() | ||
|
|
||
| equal(eventFired, true, 'maxSizeExceeded event should fire') | ||
| equal(typeof eventPayload, 'object', 'Event should have payload') | ||
| equal(typeof eventPayload.size, 'number', 'Payload should have size') | ||
| equal(typeof eventPayload.maxSize, 'number', 'Payload should have maxSize') | ||
| equal(typeof eventPayload.count, 'number', 'Payload should have count') | ||
| equal(typeof eventPayload.maxCount, 'number', 'Payload should have maxCount') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please move this test together to the others of the cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I moved them to
memory-cache-store-tests.js. Let me know if you had somewhere else in mind.