Skip to content
Gasv0.10.0

gas/queue

queue/queuetest

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

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

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

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.

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.

Each method appends a 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.
  • 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.

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

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

Ack records the call and returns AckFn’s result.

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

CallCount returns how many calls were made to method.

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.

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.

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

Enqueue records the call and returns EnqueueFn’s result.

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

Nack records the call and returns NackFn’s result.

func (m *MockQueue) Reset()

Reset forgets every call.

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.