queuetest

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package queuetest holds a mock of gas.JobQueueProvider, 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:

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

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

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 MockQueue

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 (*MockQueue) Ack

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

Ack records the call and returns AckFn's result.

func (*MockQueue) CallCount

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

CallCount returns how many calls were made to method.

func (*MockQueue) Calls

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 (*MockQueue) Dequeue

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 (*MockQueue) Enqueue

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 (*MockQueue) Nack

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

Nack records the call and returns NackFn's result.

func (*MockQueue) Reset

func (m *MockQueue) Reset()

Reset forgets every call.

type Options added in v0.6.0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL