cachextest

package
v2.0.0-...-605f64f Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 10 Imported by: 0 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestBackend

func TestBackend[T any](t *testing.T, newBackend func(t *testing.T) cachex.Backend[T], values []T)

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

func TestBroadcast(t *testing.T, newPair func(t *testing.T) (a, b cachex.Broadcast))

TestBroadcast is the contract test for a cachex.Broadcast. newPair returns two Broadcasts that reach each other, standing for two instances.

func TestByteBackend

func TestByteBackend(t *testing.T, newBackend func(t *testing.T) cachex.ByteBackend)

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

func TestLeaser[T any](t *testing.T, newBackend func(t *testing.T) cachex.Backend[T], value T)

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 NewBus

func NewBus() *Bus

NewBus returns a connected Bus.

func (*Bus) Broadcast

func (b *Bus) Broadcast() cachex.Broadcast

Broadcast returns a cachex.Broadcast on the bus, for one instance.

func (*Bus) Disconnect

func (b *Bus) Disconnect()

Disconnect drops every subscription: messages published until Reconnect reach no one.

func (*Bus) FailPublish

func (b *Bus) FailPublish(err error)

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

func (b *Bus) Subscribed() int

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).

func NewClock

func NewClock(start time.Time) *Clock

NewClock returns a Clock reading start.

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance moves the clock forward by d.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns the clock's time.

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]) Del

func (l *LeasingMap[T]) Del(ctx context.Context, key string) error

func (*LeasingMap[T]) DelMany

func (l *LeasingMap[T]) DelMany(ctx context.Context, keys []string) error

func (*LeasingMap[T]) Get

func (l *LeasingMap[T]) Get(ctx context.Context, key string) (cachex.Entry[T], bool, error)

func (*LeasingMap[T]) GetMany

func (l *LeasingMap[T]) GetMany(ctx context.Context, keys []string) (map[string]cachex.Entry[T], 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.

func (*LeasingMap[T]) Set

func (l *LeasingMap[T]) Set(ctx context.Context, key string, e cachex.Entry[T]) error

func (*LeasingMap[T]) SetMany

func (l *LeasingMap[T]) SetMany(ctx context.Context, entries map[string]cachex.Entry[T]) error

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 NewMap

func NewMap[T any]() *Map[T]

NewMap returns an empty Map.

func (*Map[T]) Clear

func (m *Map[T]) Clear(context.Context) error

Clear drops every entry: a Map is local to the process (see cachex.LocalBackend).

func (*Map[T]) Del

func (m *Map[T]) Del(_ context.Context, key string) error

func (*Map[T]) DelMany

func (m *Map[T]) DelMany(_ context.Context, keys []string) error

func (*Map[T]) Get

func (m *Map[T]) Get(_ context.Context, key string) (cachex.Entry[T], bool, error)

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).

func (*Map[T]) GetMany

func (m *Map[T]) GetMany(ctx context.Context, keys []string) (map[string]cachex.Entry[T], error)

func (*Map[T]) Len

func (m *Map[T]) Len() int

Len is the number of stored entries, expired ones included.

func (*Map[T]) Set

func (m *Map[T]) Set(_ context.Context, key string, e cachex.Entry[T]) error

func (*Map[T]) SetMany

func (m *Map[T]) SetMany(_ context.Context, entries map[string]cachex.Entry[T]) error

Jump to

Keyboard shortcuts

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