Help us shape the future of Go! Take the 2026 Go Developer Survey

cachetest

package
v0.19.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cachetest 是 data 门面子包:CacheAside 实现契约测试套件(8 场景,含 -race)。

re-export 自 github.com/oekit/pkg/cache/cachetest:类型为泛型 type alias(与源包**完全同一类型**, 双向赋值互通);非泛型函数/变量经 var 引用同一函数值。源包新增导出符号时 本壳需同步(门面跟版税,data 门面化决策已接受)。

Index

Constants

This section is empty.

Variables

View Source
var NewFakeClock = src.NewFakeClock

NewFakeClock 创建起点为 2026-01-01T00:00:00Z 的假时钟。

Functions

func Run

func Run[T any](t *testing.T, c Contract[T], factory func(t *testing.T, source pkgcache.SourceFunc[T]) pkgcache.CacheAside[T])

Run 跑完整契约场景(8 个子测试)。

func RunHitWithClock added in v0.15.0

func RunHitWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunHitWithClock 跑「命中语义」契约(pkg v0.16.0,pkg#25)——实现须提供 cache.HitGetter 能力(GetEx(ctx) (*T, bool, error)),未提供即判失败。

func RunNoBackgroundGoroutine added in v0.15.0

func RunNoBackgroundGoroutine[T any](t *testing.T, factory func(t *testing.T, source pkgcache.SourceFunc[T]) pkgcache.CacheAside[T])

RunNoBackgroundGoroutine 跑「不起后台 goroutine」契约(pkg v0.16.0,pkg#25)—— 构造缓存不得在进程内新增 goroutine。

func RunReadIsolation added in v0.15.0

func RunReadIsolation[T any](t *testing.T, c Contract[T], factory func(t *testing.T, source pkgcache.SourceFunc[T]) pkgcache.CacheAside[T])

RunReadIsolation 跑「读命中返回副本」契约(pkg v0.16.0,pkg#25)——调用方 改写 Get 返回值不得污染缓存内部条目。

func RunReadPromotion added in v0.15.0

func RunReadPromotion[T any](t *testing.T, c Contract[T], capacity int, factory func(t *testing.T, source pkgcache.SourceFunc[T]) pkgcache.CacheAside[T])

RunReadPromotion 跑「读命中提升顺序(LRU)」契约(pkg v0.16.0,pkg#25)—— factory 须构造容量为 capacity 的 LRU 缓存,场景能区分 FIFO 与 LRU。

func RunSourceInjectionWithClock added in v0.19.0

func RunSourceInjectionWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunSourceInjectionWithClock 跑「按调用注入回源」契约(pkg v0.24.0,pkg#77) ——实现须提供 cache.SourceInjector 能力;钉住注入生效 / 命中不回源 / 注入为 nil 退回构造期 source / 动态 TTL 生效 / 显式 ttl 覆盖动态 TTL。

func RunStaleIfErrorRepeatedWithClock added in v0.18.0

func RunStaleIfErrorRepeatedWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunStaleIfErrorRepeatedWithClock 跑「stale 兜底可反复生效」契约 (pkg v0.23.0,pkg#72)——兜底值必须留在表内,否则只生效第一次。

func RunStaleIfErrorWithClock added in v0.18.0

func RunStaleIfErrorWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunStaleIfErrorWithClock 跑「stale-if-error」契约(pkg v0.23.0,pkg#72)—— 回源失败时回退已过期的历史真值(fail-open),不得把回源错误原样抛出。 钉住的是「装饰器不得改变内层语义」:SingleFlightCache 包装 LocalCache 时, 其前置检查若走 Get(惰性删除路径)会把 stale 兜底需要的那份过期真值删掉。

func RunStatsWithClock added in v0.19.0

func RunStatsWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunStatsWithClock 跑「观测计数」契约(pkg v0.24.0,pkg#76)——实现须提供 cache.StatsProvider 能力(Stats() cache.Stats);计数口径自洽(每次读恰好 计入命中或未命中之一)且单调不减。

func RunSweepWithClock added in v0.15.0

func RunSweepWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunSweepWithClock 跑「手动清扫」契约(pkg v0.16.0,pkg#25)——实现须提供 cache.Sweeper 能力(Sweep(ctx) int),未提供即判失败。

func RunWithClock added in v0.11.0

func RunWithClock[T any](t *testing.T, c Contract[T], factory ClockFactory[T])

RunWithClock 跑时钟驱动的 TTL 边界场景(4 子测试:过期闭边界/TTL=0 永不过期/ null marker 过窗重源/重写刷新过期)。

Types

type ClockFactory added in v0.11.0

type ClockFactory[T any] = src.ClockFactory[T]

ClockFactory 是支持时钟注入的缓存工厂形态:实现把 clock.Now 路由到全部 TTL 判定。

type Contract

type Contract[T any] = src.Contract[T]

func Comparable

func Comparable[T comparable]() Contract[T]

Comparable 返回可比较类型的现成 Contract。

type FakeClock added in v0.11.0

type FakeClock = src.FakeClock

FakeClock 是套件提供的假时钟(并发安全;测试内手动推进)——供时钟注入实现 接入 RunWithClock 假时钟 TTL 套件(pkg v0.13.0)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL