File size: 4,308 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | //go:build enhanced_validator
package lru
import (
"testing"
"time"
)
type fakeClock struct{ t time.Time }
func (c *fakeClock) Now() time.Time { return c.t }
func (c *fakeClock) Advance(d time.Duration) { c.t = c.t.Add(d) }
func TestEnhancedRenewingReadRestartsOriginalTTL(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("session", "value", 10*time.Second)
clock.Advance(7 * time.Second)
if value, ok := cache.GetAndRenewTTL("session"); !ok || value != "value" {
t.Fatalf("renew failed")
}
clock.Advance(4 * time.Second)
if _, ok := cache.Get("session"); !ok {
t.Fatal("entry expired at original deadline")
}
clock.Advance(7 * time.Second)
if _, ok := cache.Get("session"); ok {
t.Fatal("entry survived renewed deadline")
}
}
func TestEnhancedExpiredEntryCannotBeRenewed(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("expired", "gone", time.Second)
clock.Advance(2 * time.Second)
if _, ok := cache.GetAndRenewTTL("expired"); ok {
t.Fatal("expired entry revived")
}
}
func TestEnhancedOrdinaryGetDoesNotRenewTTL(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("ordinary", "value", 10*time.Second)
clock.Advance(7 * time.Second)
if _, ok := cache.Get("ordinary"); !ok {
t.Fatal("early expiry")
}
clock.Advance(4 * time.Second)
if _, ok := cache.Get("ordinary"); ok {
t.Fatal("ordinary Get renewed TTL")
}
}
func TestEnhancedExactDeadlineIsExpired(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("boundary", "value", 10*time.Second)
clock.Advance(10 * time.Second)
if _, ok := cache.GetAndRenewTTL("boundary"); ok {
t.Fatal("exact deadline revived an expired entry")
}
}
func TestEnhancedNonTTLDoesNotGainExpiration(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.Set("persistent", "value")
if value, ok := cache.GetAndRenewTTL("persistent"); !ok || value != "value" {
t.Fatal("non-TTL read failed")
}
clock.Advance(24 * time.Hour)
if _, ok := cache.Get("persistent"); !ok {
t.Fatal("non-TTL entry acquired an expiration")
}
}
func TestEnhancedRenewalPreservesLRURecency(t *testing.T) {
clock := &fakeClock{t: time.Unix(1000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("a", "A", time.Hour)
cache.Set("b", "B")
if _, ok := cache.GetAndRenewTTL("a"); !ok {
t.Fatal("renew read failed")
}
cache.Set("c", "C")
if _, ok := cache.Get("b"); ok {
t.Fatal("renew read did not refresh LRU recency")
}
if _, ok := cache.Get("a"); !ok {
t.Fatal("renewed entry was evicted")
}
}
func TestEnhancedOverwriteChangesOriginalTTL(t *testing.T) {
clock := &fakeClock{t: time.Unix(2000, 0)}
cache := NewCacheWithClock(3, clock.Now)
cache.SetWithTTL("updated", "old", time.Hour)
cache.SetWithTTL("updated", "new", 3*time.Second)
clock.Advance(2 * time.Second)
if value, ok := cache.GetAndRenewTTL("updated"); !ok || value != "new" {
t.Fatal("overwriting did not replace the cached value")
}
clock.Advance(3 * time.Second)
if _, ok := cache.GetAndRenewTTL("updated"); ok {
t.Fatal("renewal used a stale TTL after overwrite")
}
cache.SetWithTTL("persistent", "timed", time.Second)
cache.Set("persistent", "untimed")
clock.Advance(time.Hour)
if value, ok := cache.GetAndRenewTTL("persistent"); !ok || value != "untimed" {
t.Fatal("Set did not remove a previously assigned TTL")
}
}
func TestEnhancedRepeatedRenewalUsesOriginalDuration(t *testing.T) {
clock := &fakeClock{t: time.Unix(3000, 0)}
cache := NewCacheWithClock(2, clock.Now)
cache.SetWithTTL("repeated", "value", 7*time.Second)
for i := 0; i < 5; i++ {
clock.Advance(6 * time.Second)
if value, ok := cache.GetAndRenewTTL("repeated"); !ok || value != "value" {
t.Fatalf("renewal %d shortened the original TTL", i)
}
}
clock.Advance(7 * time.Second)
if _, ok := cache.Get("repeated"); ok {
t.Fatal("repeated renewals extended TTL beyond its original duration")
}
if value, ok := cache.GetAndRenewTTL("missing"); ok || value != "" {
t.Fatal("a missing key must remain a miss")
}
}
|