gas/cache
cache/cachetest
import "github.com/gasmod/gas/cache/cachetest"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.
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 1Defaults
Section titled “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.
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
Section titled “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.
API reference
Section titled “API reference”type Call
Section titled “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
Section titled “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
Section titled “func (*MockCache) CallCount”func (m *MockCache) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockCache) Calls
Section titled “func (*MockCache) Calls”func (m *MockCache) Calls() []CallCalls returns a copy of the calls, in the order they were made.
func (*MockCache) CheckHealth
Section titled “func (*MockCache) CheckHealth”func (m *MockCache) CheckHealth(ctx context.Context) errorCheckHealth records the call and returns CheckHealthFn’s result.
func (*MockCache) CheckReady
Section titled “func (*MockCache) CheckReady”func (m *MockCache) CheckReady(ctx context.Context) errorCheckReady records the call and returns CheckReadyFn’s result.
func (*MockCache) Delete
Section titled “func (*MockCache) Delete”func (m *MockCache) Delete(ctx context.Context, key string) errorDelete records the call and returns DeleteFn’s result.
func (*MockCache) Exists
Section titled “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
Section titled “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
Section titled “func (*MockCache) Reset”func (m *MockCache) Reset()Reset forgets every call.
func (*MockCache) Set
Section titled “func (*MockCache) Set”func (m *MockCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) errorSet records the call and returns SetFn’s result.