Skip to content
Gasv0.10.0

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 1

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.

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.

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 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 (m *MockCache) CallCount(method string) int

CallCount returns how many calls were made to method.

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

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

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

CheckHealth records the call and returns CheckHealthFn’s result.

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

CheckReady records the call and returns CheckReadyFn’s result.

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

Delete records the call and returns DeleteFn’s result.

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

Exists records the call and returns ExistsFn’s result.

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

Get records the call and returns GetFn’s result.

func (m *MockCache) Reset()

Reset forgets every call.

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

Set records the call and returns SetFn’s result.