Skip to content

Add LRUCache test for get() recency promotion.#1898

Open
anxkhn wants to merge 1 commit into
apple:mainfrom
anxkhn:test/lru-cache-get-promotes-mru
Open

Add LRUCache test for get() recency promotion.#1898
anxkhn wants to merge 1 commit into
apple:mainfrom
anxkhn:test/lru-cache-get-promotes-mru

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 5, 2026

Copy link
Copy Markdown

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Test coverage

Motivation and Context

LRUCache.get(_:) is a real LRU read: on a hit it refreshes recency by moving
the node to most-recently-used before returning
(Sources/SocketForwarder/LRUCache.swift, the listRemove + listInsert(after: tail)
pair in get). The existing LRUCacheTest.testLRUCache never observes that
promotion, though: every get call sits at the end of the test purely to read
final values after all put-driven evictions are done. No test reads an existing
key and then forces an eviction to check that the just-read key survived.

Because of that, the recency update in get is a mutation survivor: removing it
(making get a plain lookup that no longer counts as use) keeps every current
assertion green, since put already promotes on insert/replace and the test only
relies on put-driven ordering. A regression that stopped treating reads as use
would ship undetected.

This is a test-only change: it adds one focused test and leaves production code
untouched.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

testGetPromotesToMostRecentlyUsed fills a size-3 cache with foo, bar, baz,
reads foo (promoting it to most-recently-used), then inserts qux and asserts
the evicted entry is ("bar", "2") rather than ("foo", "1"), that foo
survived, and that bar is gone. It fails if the promotion in get is removed
and passes with it.

Run:

swift test --filter LRUCacheTest

Both testLRUCache and the new test pass. To confirm it closes the gap, removing
the two promotion lines in get(_:) leaves testLRUCache green but makes the new
test fail, then reverting restores both to green.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants