cachetest

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 8 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 cachetest holds a mock of gas.CacheProvider, MockCache, for tests of code that uses a cache. It also implements gas.HealthReporter and gas.ReadyReporter.

Usage

The mock records each call, and returns what the function in the field named after the method returns:

m := &cachetest.MockCache{
	GetFn: func(context.Context, string) ([]byte, error) {
		return []byte("hello"), nil
	},
}
b, err := m.Get(ctx, "greeting")
// b is hello, err is nil, and m.CallCount("Get") is 1

Defaults

With a field nil, its method behaves as an empty cache that accepts everything:

  • Get returns an error wrapping cache.ErrKeyNotFound, `cachetest: get: key not found`.
  • Exists returns false and nil.
  • Set, Delete, CheckHealth and CheckReady return nil.

Calls

Each method appends a Call with its name and its arguments after the context, before it calls its field. The arguments recorded are:

  • Get, Delete and Exists: the key.
  • Set: the key, the value and the TTL.
  • CheckHealth and CheckReady: none.

The value is recorded as a copy, nil for nil, so that a caller that reuses its slice does not change a recorded call.

To read the calls:

  • Calls returns a copy of the calls in the order they were made.
  • CallCount counts those of one method.
  • Reset forgets them all.

Concurrency

The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	// Method is the name of the method called.
	Method string
	// Args are the method's arguments after the context, in order.
	Args []any
}

Call is one call of the mock's method.

type MockCache

type MockCache struct {
	// GetFn is what Get returns. When it is nil, Get returns an error
	// wrapping cache.ErrKeyNotFound.
	GetFn func(ctx context.Context, key string) ([]byte, error)
	// SetFn is what Set returns. When it is nil, Set returns nil.
	SetFn func(ctx context.Context, key string, value []byte, ttl time.Duration) error
	// DeleteFn is what Delete returns. When it is nil, Delete returns nil.
	DeleteFn func(ctx context.Context, key string) error
	// ExistsFn is what Exists returns. When it is nil, Exists returns
	// false and nil.
	ExistsFn func(ctx context.Context, key string) (bool, error)
	// CheckHealthFn is what CheckHealth returns. When it is nil,
	// CheckHealth returns nil.
	CheckHealthFn func(ctx context.Context) error
	// CheckReadyFn is what CheckReady returns. When it is nil, CheckReady
	// returns nil.
	CheckReadyFn func(ctx context.Context) error
	// contains filtered or unexported fields
}

MockCache is a gas.CacheProvider, gas.HealthReporter and gas.ReadyReporter that records its calls, as described in the package documentation.

func (*MockCache) CallCount

func (m *MockCache) CallCount(method string) int

CallCount returns how many calls were made to method.

func (*MockCache) Calls

func (m *MockCache) Calls() []Call

Calls returns a copy of the calls, in the order they were made.

func (*MockCache) CheckHealth

func (m *MockCache) CheckHealth(ctx context.Context) error

CheckHealth records the call and returns CheckHealthFn's result.

func (*MockCache) CheckReady

func (m *MockCache) CheckReady(ctx context.Context) error

CheckReady records the call and returns CheckReadyFn's result.

func (*MockCache) Delete

func (m *MockCache) Delete(ctx context.Context, key string) error

Delete records the call and returns DeleteFn's result.

func (*MockCache) Exists

func (m *MockCache) Exists(ctx context.Context, key string) (bool, error)

Exists records the call and returns ExistsFn's result.

func (*MockCache) Get

func (m *MockCache) Get(ctx context.Context, key string) ([]byte, error)

Get records the call and returns GetFn's result.

func (*MockCache) Reset

func (m *MockCache) Reset()

Reset forgets every call.

func (*MockCache) Set

func (m *MockCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set records the call and returns SetFn's result.

Jump to

Keyboard shortcuts

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