UXInteract / task_014 /tests /validate_enhanced_ttl_test.go
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
4.31 kB
//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")
}
}