Documentation
¶
Overview ¶
Package cachetest provides conformance suites for implementations of the cache store and invalidation-bus interfaces.
Every backend in this module passes these suites, and third-party implementations of cache.Store, cache.BatchStore and cache.InvalidationBus should run them from their own tests:
func TestStoreContract(t *testing.T) {
cachetest.RunStoreContract(t, func(t *testing.T) cache.Store[string, int] {
store := mybackend.New[string, int](client)
t.Cleanup(func() { _ = store.Close() })
return store
})
}
The suites exercise stores with string keys and int values. Each subtest prefixes the namespaces it uses with the subtest name and a timestamp, so a factory may return stores that share one remote server or database.
The suites use the wall clock because they target implementations whose expiry or delivery may be enforced by a remote server: the logical-expiration case sleeps for a few milliseconds, and the invalidation-bus contract polls for delivery for up to a few seconds. RunStoreContract and RunBatchStoreContract call testing.T.Run and therefore must not be called inside a testing/synctest bubble. RunInvalidationBusContract may run inside a bubble when the bus under test is entirely in-process, which turns its waits into virtual time.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunBatchStoreContract ¶
func RunBatchStoreContract(t *testing.T, factory BatchStoreFactory)
RunBatchStoreContract verifies the behaviour required from every cache.BatchStore: results aligned with keys (including duplicates), batch writes visible through Get, expired entries skipped, ErrBatchLength for mismatched slices, idempotent batch invalidation, namespace invalidation, empty batches, and cancellation. Each case runs as a subtest of t. Run RunStoreContract as well; this suite does not repeat its cases.
func RunInvalidationBusContract ¶
func RunInvalidationBusContract[K comparable]( t *testing.T, newBus func(*testing.T) cache.InvalidationBus[K], event cache.InvalidationEvent[K], )
RunInvalidationBusContract verifies the behaviour required from every cache.InvalidationBus: Subscribe rejects a nil handler with cache.ErrNilInvalidationHandler, an event published through one bus instance reaches a subscriber on another instance unchanged, a repeated publication is delivered again, and Subscribe returns context.Canceled once its context is canceled. newBus is called twice, once for the subscriber and once for the publisher; both instances must share a transport.
func RunStoreContract ¶
func RunStoreContract(t *testing.T, factory StoreFactory)
RunStoreContract verifies the behaviour required from every cache.Store: hits and misses, negative entries, logical expiration (including an expired overwrite of a live entry), namespace isolation, idempotent key invalidation, namespace invalidation, cancellation, and entries without expiration. Each case runs as a subtest of t.
Types ¶
type BatchStoreFactory ¶
BatchStoreFactory returns the batch store under test for one contract subtest, with the same sharing rules as StoreFactory.