testutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package testutil holds helpers shared by tests only: polling assertions, in-memory fakes of domain ports and Postgres test containers. It is never imported by non-test code.

testutil.Eventually(t, func() bool { return done.Load() }, time.Second)

Index

Constants

This section is empty.

Variables

View Source
var ErrInjected = errors.New("fakestore: injected failure")

ErrInjected is returned by FakeStore when a failure is injected.

Functions

func Eventually

func Eventually(tb testing.TB, cond func() bool, timeout time.Duration, msgAndArgs ...any)

Eventually polls cond every 5ms until it returns true or timeout elapses, in which case the test fails. It replaces time.Sleep-based waiting.

Types

type FakeStore

type FakeStore struct {
	FetchHook func(rows []*job.Row) // called with fetched rows while the lock is held
	// contains filtered or unexported fields
}

FakeStore is an in-memory implementation of the job store ports with the same semantics as the Postgres adapter: fetch order, per-key limits, the completion guard, leases and periodic ticks. One mutex makes every operation atomic, which models row locks and advisory locks.

func NewFakeStore

func NewFakeStore() *FakeStore

NewFakeStore returns an empty store.

func (*FakeStore) AcquireLease

func (s *FakeStore) AcquireLease(_ context.Context, _, id string, now time.Time, ttl time.Duration) (bool, error)

AcquireLease implements job.LeaseStore.

func (*FakeStore) CancelRequested

func (s *FakeStore) CancelRequested(_ context.Context, ids []int64) ([]int64, error)

CancelRequested implements job.CancelPoller.

func (*FakeStore) Complete

func (s *FakeStore) Complete(_ context.Context, clientID string, cs []job.Completion) ([]int64, error)

Complete implements job.CompletionWriter.

func (*FakeStore) DeleteFinalized

func (s *FakeStore) DeleteFinalized(_ context.Context, state job.State, cutoff time.Time, limit int) (int, error)

DeleteFinalized implements job.MaintenanceStore.

func (*FakeStore) EnqueueTick

func (s *FakeStore) EnqueueTick(_ context.Context, name string, tick time.Time, p job.InsertParams) (bool, error)

EnqueueTick implements job.MaintenanceStore.

func (*FakeStore) FailNext

func (s *FakeStore) FailNext(n int)

FailNext makes the next n operations fail with ErrInjected.

func (*FakeStore) Fetch

func (s *FakeStore) Fetch(_ context.Context, p job.FetchParams) ([]*job.Row, error)

Fetch implements job.Fetcher.

func (*FakeStore) Heartbeat

func (s *FakeStore) Heartbeat(_ context.Context, clientID string, now time.Time, refs []job.Ref) (int, error)

Heartbeat implements job.HeartbeatWriter.

func (*FakeStore) Insert

func (s *FakeStore) Insert(p job.InsertParams) int64

Insert adds a job and returns its id.

func (*FakeStore) Job

func (s *FakeStore) Job(id int64) (job.Row, bool)

Job returns a copy of a job.

func (*FakeStore) Jobs

func (s *FakeStore) Jobs() []job.Row

Jobs returns copies of all jobs ordered by id.

func (*FakeStore) KeyStats

func (s *FakeStore) KeyStats(_ context.Context, queue string, limit int, _ bool) ([]job.KeyStat, error)

KeyStats implements job.Fetcher.

func (*FakeStore) LastTicks

func (s *FakeStore) LastTicks(context.Context) (map[string]time.Time, error)

LastTicks implements job.MaintenanceStore.

func (*FakeStore) LeaseHolder

func (s *FakeStore) LeaseHolder() (string, time.Time)

LeaseHolder returns the current lease holder id and expiry.

func (*FakeStore) MarkPeriodicStart

func (s *FakeStore) MarkPeriodicStart(_ context.Context, name string, now time.Time) error

MarkPeriodicStart implements job.MaintenanceStore.

func (*FakeStore) PromoteDue

func (s *FakeStore) PromoteDue(_ context.Context, now time.Time, limit int) (int, error)

PromoteDue implements job.MaintenanceStore.

func (*FakeStore) PruneTicks

func (s *FakeStore) PruneTicks(_ context.Context, cutoff time.Time) (int, error)

PruneTicks implements job.MaintenanceStore.

func (*FakeStore) QueueDepths

func (s *FakeStore) QueueDepths(context.Context) ([]job.QueueDepth, error)

QueueDepths implements job.MaintenanceStore.

func (*FakeStore) QueueHeartbeat

func (s *FakeStore) QueueHeartbeat(_ context.Context, queue string, _ time.Time) (bool, error)

QueueHeartbeat implements job.Fetcher.

func (*FakeStore) Reindex

func (s *FakeStore) Reindex(context.Context) (bool, error)

Reindex implements job.MaintenanceStore.

func (*FakeStore) ReleaseLease

func (s *FakeStore) ReleaseLease(_ context.Context, _, id string) error

ReleaseLease implements job.LeaseStore.

func (*FakeStore) RenewLease

func (s *FakeStore) RenewLease(_ context.Context, _, id string, now time.Time, ttl time.Duration) (bool, error)

RenewLease implements job.LeaseStore.

func (*FakeStore) Rescue

func (s *FakeStore) Rescue(_ context.Context, cutoff time.Time, cs []job.Completion) (int, error)

Rescue implements job.MaintenanceStore.

func (*FakeStore) RunningByKey

func (s *FakeStore) RunningByKey(queue string) map[string]int

RunningByKey counts running jobs per (queue, key).

func (*FakeStore) SetPaused

func (s *FakeStore) SetPaused(queue string, paused bool)

SetPaused pauses or resumes a queue.

func (*FakeStore) StuckJobs

func (s *FakeStore) StuckJobs(_ context.Context, cutoff time.Time, limit int) ([]*job.Row, error)

StuckJobs implements job.MaintenanceStore.

func (*FakeStore) Ticks

func (s *FakeStore) Ticks(name string) []time.Time

Ticks returns the enqueued ticks of a periodic job (not start markers), oldest first.

func (*FakeStore) Update

func (s *FakeStore) Update(id int64, fn func(*job.Row))

Update mutates a job under the store lock.

Directories

Path Synopsis
Package pgtest provides Postgres for integration tests: a Testcontainers server per test package (or FOREMAN_TEST_DATABASE_URL) and a freshly migrated schema per test.
Package pgtest provides Postgres for integration tests: a Testcontainers server per test package (or FOREMAN_TEST_DATABASE_URL) and a freshly migrated schema per test.

Jump to

Keyboard shortcuts

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