Files
coredns/plugin/cache/item_test.go
houyuwushang c0adbae99b plugin/cache: preserve monotonic time for TTL expiry (#8346)
Keep cache timestamps and TTL calculations on the time values returned by the cache clock. Converting them with UTC strips Go's monotonic clock reading and can extend cached entries when the wall clock moves backward.

Add a regression test that verifies new cache items retain the original monotonic timestamp.

Fixes #5478.

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>
2026-07-29 18:12:56 -07:00

24 lines
574 B
Go

package cache
import (
"reflect"
"testing"
"time"
"github.com/miekg/dns"
)
func TestNewItemPreservesMonotonicClock(t *testing.T) {
now := time.Now()
if reflect.DeepEqual(now, now.Round(0)) {
t.Fatal("time.Now did not include a monotonic clock reading")
}
i := newItem(new(dns.Msg), now, time.Minute)
// DeepEqual compares the complete time representation, including its
// monotonic clock reading. Time.Equal intentionally ignores that detail.
if !reflect.DeepEqual(i.stored, now) {
t.Fatalf("stored time = %v; want original time %v", i.stored, now)
}
}