## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# cache/cachetest

> Package cachetest holds a mock of gas.CacheProvider, MockCache, for tests of code that uses a cache.

The Markdown of https://gasmod.dev/reference/cache/cachetest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/cache/cachetest"
```

Package cachetest holds a mock of gas.CacheProvider, [MockCache](#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:

```go
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](#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.


## API reference

<a id="Call"></a>

### type Call

```go
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.

<a id="MockCache"></a>

### type MockCache

```go
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.

<a id="MockCache.CallCount"></a>

#### func (*MockCache) CallCount

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

CallCount returns how many calls were made to method.

<a id="MockCache.Calls"></a>

#### func (*MockCache) Calls

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

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

<a id="MockCache.CheckHealth"></a>

#### func (*MockCache) CheckHealth

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

CheckHealth records the call and returns CheckHealthFn's result.

<a id="MockCache.CheckReady"></a>

#### func (*MockCache) CheckReady

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

CheckReady records the call and returns CheckReadyFn's result.

<a id="MockCache.Delete"></a>

#### func (*MockCache) Delete

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

Delete records the call and returns DeleteFn's result.

<a id="MockCache.Exists"></a>

#### func (*MockCache) Exists

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

Exists records the call and returns ExistsFn's result.

<a id="MockCache.Get"></a>

#### func (*MockCache) Get

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

Get records the call and returns GetFn's result.

<a id="MockCache.Reset"></a>

#### func (*MockCache) Reset

```go
func (m *MockCache) Reset()
```

Reset forgets every call.

<a id="MockCache.Set"></a>

#### func (*MockCache) Set

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

Set records the call and returns SetFn's result.
