Documentation
¶
Overview ¶
Package cachextest has helpers for testing code that uses cachex: an in-memory Backend (with leases, NewLeasingMap), an in-memory broadcast (NewBus), a manual clock, and the contract tests for Backend, ByteBackend, Leaser and Broadcast implementations (TestBackend, TestByteBackend, TestLeaser, TestBroadcast).
Index ¶
- func TestBackend[T any](t *testing.T, newBackend func(t *testing.T) cachex.Backend[T], values []T)
- func TestBroadcast(t *testing.T, newPair func(t *testing.T) (a, b cachex.Broadcast))
- func TestByteBackend(t *testing.T, newBackend func(t *testing.T) cachex.ByteBackend)
- func TestLeaser[T any](t *testing.T, newBackend func(t *testing.T) cachex.Backend[T], value T)
- type Bus
- type Clock
- type LeasingMap
- func (l *LeasingMap[T]) Del(ctx context.Context, key string) error
- func (l *LeasingMap[T]) DelMany(ctx context.Context, keys []string) error
- func (l *LeasingMap[T]) Get(ctx context.Context, key string) (cachex.Entry[T], bool, error)
- func (l *LeasingMap[T]) GetMany(ctx context.Context, keys []string) (map[string]cachex.Entry[T], error)
- func (l *LeasingMap[T]) Leaser() cachex.Leaser[T]
- func (l *LeasingMap[T]) Len() int
- func (l *LeasingMap[T]) Set(ctx context.Context, key string, e cachex.Entry[T]) error
- func (l *LeasingMap[T]) SetMany(ctx context.Context, entries map[string]cachex.Entry[T]) error
- type Map
- func (m *Map[T]) Clear(context.Context) error
- func (m *Map[T]) Del(_ context.Context, key string) error
- func (m *Map[T]) DelMany(_ context.Context, keys []string) error
- func (m *Map[T]) Get(_ context.Context, key string) (cachex.Entry[T], bool, error)
- func (m *Map[T]) GetInto(ctx context.Context, keys []string, entries []cachex.Entry[T], found []bool) error
- func (m *Map[T]) GetMany(ctx context.Context, keys []string) (map[string]cachex.Entry[T], error)
- func (m *Map[T]) Len() int
- func (m *Map[T]) Set(_ context.Context, key string, e cachex.Entry[T]) error
- func (m *Map[T]) SetMany(_ context.Context, entries map[string]cachex.Entry[T]) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TestBackend ¶
TestBackend checks that a Backend keeps the contract a Cache relies on. newBackend returns an empty backend; it is called once per subtest. values are at least three different values of T that the backend stores; run it with a string and with a struct type, so that a backend that encodes values is tested with its codec's main paths.
func TestBroadcast ¶
TestBroadcast is the contract test for a cachex.Broadcast. newPair returns two Broadcasts that reach each other, standing for two instances.
func TestByteBackend ¶
TestByteBackend checks that a ByteBackend keeps the contract cachex.Encoded relies on. newBackend returns an empty backend; it is called once per subtest.
func TestLeaser ¶
TestLeaser is the contract test for a Backend whose leases are on (see cachex.Leaser): a lease is taken once, until it ends or expires; a write of the key ends it; PutLeased writes only under the lease's token; Release ends it without writing. newBackend returns an empty backend; value is a value of T.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus is an in-memory hub for cachex.Broadcast, for tests: each Broadcast it hands out stands for one instance. Messages reach the listening instances in the order they were published. Disconnect and Reconnect stand for a lost subscription: what is published in between is lost.
func (*Bus) Disconnect ¶
func (b *Bus) Disconnect()
Disconnect drops every subscription: messages published until Reconnect reach no one.
func (*Bus) FailPublish ¶
FailPublish makes Publish fail with err (nil: succeed again).
func (*Bus) Reconnect ¶
func (b *Bus) Reconnect()
Reconnect resubscribes every listener, each of which calls its ready.
func (*Bus) Subscribed ¶
Subscribed is how many listeners have subscribed (and run their first ready) so far: wait for an instance's to know that a message published from then on reaches it and that its first clear is behind it.
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is a manual clock: pass its Now to cachex.WithNow. With a backend that expires entries by the wall clock, start it at time.Now() (see cachex.WithNow).
type LeasingMap ¶
type LeasingMap[T any] struct { // contains filtered or unexported fields }
LeasingMap is a Map whose leases are on (see cachex.Leaser), standing for a layer that several instances share: build each instance's Cache over the same LeasingMap. Unlike a Map it is not a cachex.LocalBackend.
func NewLeasingMap ¶
func NewLeasingMap[T any]() *LeasingMap[T]
NewLeasingMap returns an empty LeasingMap.
func (*LeasingMap[T]) DelMany ¶
func (l *LeasingMap[T]) DelMany(ctx context.Context, keys []string) error
func (*LeasingMap[T]) Leaser ¶
func (l *LeasingMap[T]) Leaser() cachex.Leaser[T]
Leaser returns the map's Leaser.
func (*LeasingMap[T]) Len ¶
func (l *LeasingMap[T]) Len() int
Len is the number of stored entries, expired ones included.
type Map ¶
type Map[T any] struct { // contains filtered or unexported fields }
Map is an unbounded in-memory Backend for tests. It never drops entries, not even expired ones (the Cache does not serve those), so it is not meant for production.
func (*Map[T]) Clear ¶
Clear drops every entry: a Map is local to the process (see cachex.LocalBackend).
func (*Map[T]) GetInto ¶
func (m *Map[T]) GetInto(ctx context.Context, keys []string, entries []cachex.Entry[T], found []bool) error
GetInto is GetMany into slices (see cachex.SliceGetter).