From 65b515702c7bbead75dba003d1d8ac010e066c92 Mon Sep 17 00:00:00 2001 From: Anas Khan <83116240+anxkhn@users.noreply.github.com> Date: Fri, 3 Jul 2026 17:24:24 +0530 Subject: [PATCH] Add LRUCache test for get() recency promotion. 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. --- Tests/SocketForwarderTests/LRUCacheTest.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/SocketForwarderTests/LRUCacheTest.swift b/Tests/SocketForwarderTests/LRUCacheTest.swift index 3603d81f9..3b07bdd61 100644 --- a/Tests/SocketForwarderTests/LRUCacheTest.swift +++ b/Tests/SocketForwarderTests/LRUCacheTest.swift @@ -51,4 +51,21 @@ struct LRUCacheTest { #expect(cache.get("qux") == "5") #expect(cache.get("quux") == "6") } + + @Test + func testGetPromotesToMostRecentlyUsed() throws { + let cache = LRUCache(size: 3) + #expect(cache.put(key: "foo", value: "1") == nil) + #expect(cache.put(key: "bar", value: "2") == nil) + #expect(cache.put(key: "baz", value: "3") == nil) + + // Reading "foo" must promote it to most-recently-used, so the oldest + // remaining entry ("bar") is evicted next, not "foo". + #expect(cache.get("foo") == "1") + + let evicted = try #require(cache.put(key: "qux", value: "4")) + #expect(evicted == ("bar", "2")) + #expect(cache.get("foo") == "1") + #expect(cache.get("bar") == nil) + } }