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 ¶
- type Call
- type MockCache
- func (m *MockCache) CallCount(method string) int
- func (m *MockCache) Calls() []Call
- func (m *MockCache) CheckHealth(ctx context.Context) error
- func (m *MockCache) CheckReady(ctx context.Context) error
- func (m *MockCache) Delete(ctx context.Context, key string) error
- func (m *MockCache) Exists(ctx context.Context, key string) (bool, error)
- func (m *MockCache) Get(ctx context.Context, key string) ([]byte, error)
- func (m *MockCache) Reset()
- func (m *MockCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
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) CheckHealth ¶
CheckHealth records the call and returns CheckHealthFn's result.
func (*MockCache) CheckReady ¶
CheckReady records the call and returns CheckReadyFn's result.