Add LRUCache test for get() recency promotion.#1898
Open
anxkhn wants to merge 1 commit into
Open
Conversation
get(_:) refreshes recency by moving a read entry to most-recently-used, but the existing LRUCacheTest only observed eviction order driven by put(), never after a get(). Removing the promotion in get() left every assertion green, so a regression that stopped counting reads as use would ship undetected. Add testGetPromotesToMostRecentlyUsed: read an entry, then force an eviction and assert the older entry is evicted instead. This fails if the get() promotion is removed and passes with it.
teerawad020841
approved these changes
Jul 5, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Type of Change
Motivation and Context
LRUCache.get(_:)is a real LRU read: on a hit it refreshes recency by movingthe node to most-recently-used before returning
(
Sources/SocketForwarder/LRUCache.swift, thelistRemove+listInsert(after: tail)pair in
get). The existingLRUCacheTest.testLRUCachenever observes thatpromotion, though: every
getcall sits at the end of the test purely to readfinal values after all
put-driven evictions are done. No test reads an existingkey and then forces an eviction to check that the just-read key survived.
Because of that, the recency update in
getis a mutation survivor: removing it(making
geta plain lookup that no longer counts as use) keeps every currentassertion green, since
putalready promotes on insert/replace and the test onlyrelies on
put-driven ordering. A regression that stopped treating reads as usewould ship undetected.
This is a test-only change: it adds one focused test and leaves production code
untouched.
Testing
testGetPromotesToMostRecentlyUsedfills a size-3 cache withfoo, bar, baz,reads
foo(promoting it to most-recently-used), then insertsquxand assertsthe evicted entry is
("bar", "2")rather than("foo", "1"), thatfoosurvived, and that
baris gone. It fails if the promotion ingetis removedand passes with it.
Run:
Both
testLRUCacheand the new test pass. To confirm it closes the gap, removingthe two promotion lines in
get(_:)leavestestLRUCachegreen but makes the newtest fail, then reverting restores both to green.