cachetest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

type BatchStoreFactory func(t *testing.T) cache.BatchStore[string, int]

BatchStoreFactory returns the batch store under test for one contract subtest, with the same sharing rules as StoreFactory.

type StoreFactory

type StoreFactory func(t *testing.T) cache.Store[string, int]

StoreFactory returns the store under test for one contract subtest. It may return a new store or one shared with other subtests; the suite isolates subtests by namespace. Use t.Cleanup to release resources.

Jump to

Keyboard shortcuts

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