## 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.

# queue/queuetest

> Package queuetest holds a mock of gas.JobQueueProvider, MockQueue, for tests of code that uses a queue.

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

```go
import "github.com/gasmod/gas/queue/queuetest"
```

Package queuetest holds a mock of gas.JobQueueProvider, [MockQueue](#MockQueue),
for tests of code that uses a queue.

## Usage

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

```go
m := &queuetest.MockQueue{
	DequeueFn: func(context.Context, string, int, time.Duration) ([]gas.Job, error) {
		return []gas.Job{{ID: "1", ReceiptHandle: "r1", Body: []byte("hello")}}, nil
	},
}
jobs, err := m.Dequeue(ctx, "orders", 10, 20*time.Second)
// jobs holds the one job, err is nil, and m.CallCount("Dequeue") is 1
```

The mock keeps none of the queue package's rules: it checks no
argument, and holds no messages. To test against those rules without
a server, use gas/queue/memory.

## Defaults

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

- Dequeue returns no jobs and nil.
- Enqueue, Ack and Nack 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:

- Enqueue: the queue, the payload, and the options, resolved into an
[Options](#Options).
- Dequeue: the queue, maxMessages and the wait.
- Ack and Nack: the queue and the job.

The payload, the attributes, and the job's body and attributes are
recorded as copies, nil for nil, so that a caller that reuses them does
not change a recorded call.

To read the record:

- Calls returns a deep copy of the calls in the order they were made,
so that changing what it returns does not change them either.
- 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="MockQueue"></a>

### type MockQueue

```go
type MockQueue struct {
	// EnqueueFn is what Enqueue returns. When it is nil, Enqueue returns
	// nil.
	EnqueueFn func(ctx context.Context, queue string, payload []byte, opts ...gas.EnqueueOption) error
	// DequeueFn is what Dequeue returns. When it is nil, Dequeue returns
	// no jobs and nil.
	DequeueFn func(ctx context.Context, queue string, maxMessages int, wait time.Duration) ([]gas.Job, error)
	// AckFn is what Ack returns. When it is nil, Ack returns nil.
	AckFn func(ctx context.Context, queue string, job gas.Job) error
	// NackFn is what Nack returns. When it is nil, Nack returns nil.
	NackFn func(ctx context.Context, queue string, job gas.Job) error
	// contains filtered or unexported fields
}
```

MockQueue is a gas.JobQueueProvider that records its calls, as
described in the package documentation.

<a id="MockQueue.Ack"></a>

#### func (*MockQueue) Ack

```go
func (m *MockQueue) Ack(ctx context.Context, queue string, job gas.Job) error
```

Ack records the call and returns AckFn's result.

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

#### func (*MockQueue) CallCount

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

CallCount returns how many calls were made to method.

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

#### func (*MockQueue) Calls

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

Calls returns a deep copy of the calls, in the order they were made:
changing a payload, an attribute map or a job it returns changes no
recorded call.

<a id="MockQueue.Dequeue"></a>

#### func (*MockQueue) Dequeue

```go
func (m *MockQueue) Dequeue(
	ctx context.Context,
	queue string,
	maxMessages int,
	wait time.Duration,
) ([]gas.Job, error)
```

Dequeue records the call and returns DequeueFn's result.

<a id="MockQueue.Enqueue"></a>

#### func (*MockQueue) Enqueue

```go
func (m *MockQueue) Enqueue(
	ctx context.Context,
	queue string,
	payload []byte,
	opts ...gas.EnqueueOption,
) error
```

Enqueue records the call and returns EnqueueFn's result.

<a id="MockQueue.Nack"></a>

#### func (*MockQueue) Nack

```go
func (m *MockQueue) Nack(ctx context.Context, queue string, job gas.Job) error
```

Nack records the call and returns NackFn's result.

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

#### func (*MockQueue) Reset

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

Reset forgets every call.

<a id="Options"></a>

### type Options

```go
type Options struct {
	// Attributes are from gas.WithJobAttributes, a copy, nil for none.
	Attributes map[string]string
	// GroupID is from gas.WithGroupID.
	GroupID string
	// DedupeID is from gas.WithDedupeID.
	DedupeID string
	// Delay is from gas.WithDelay.
	Delay time.Duration
}
```

Options are the options of an Enqueue, resolved, as a Call records
them.
