foremantest

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: 11 Imported by: 0

Documentation

Overview

Package foremantest provides helpers for testing code that uses Foreman: asserting that jobs were inserted, running a worker synchronously through the real executor, and a fake clock.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequireInserted

func RequireInserted[T foreman.JobArgs](
	ctx context.Context, tb testing.TB, client *foreman.Client, expected T,
) *foreman.JobRow

RequireInserted fails the test unless a job of the kind of expected with equal args (compared as JSON) exists, and returns the newest such job.

Example
var (
	t      *testing.T
	client *foreman.Client
)
// ... call the code under test, which enqueues a mail job ...
row := foremantest.RequireInserted(context.Background(), t, client, mailArgs{To: "a@example.com"})
fmt.Println(row.Queue)

func RequireNotInserted

func RequireNotInserted[T foreman.JobArgs](ctx context.Context, tb testing.TB, client *foreman.Client, args T)

RequireNotInserted fails the test if a job of the kind of args with equal args exists.

Example
var (
	t      *testing.T
	client *foreman.Client
)
foremantest.RequireNotInserted(context.Background(), t, client, mailArgs{To: "blocked@example.com"})

func WorkInline

func WorkInline[T foreman.JobArgs](
	ctx context.Context, tb testing.TB, client *foreman.Client, args T, opts *foreman.InsertOpts,
) *foreman.JobRow

WorkInline inserts args with client and runs the job synchronously on that client through its middleware, executor and result mapping (Client.JobWork), then returns the job as written. The client needs a worker for the kind; it does not need to be started.

Example
var (
	t      *testing.T
	client *foreman.Client // with a worker registered for mailArgs
)
row := foremantest.WorkInline(context.Background(), t, client, mailArgs{To: "a@example.com"}, nil)
fmt.Println(row.State)

Types

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

Clock is a manually advanced clock for tests; pass it as Config.Clock. Every time a client writes or compares then comes from it.

func NewClock

func NewClock(start time.Time) *Clock

NewClock returns a fake clock set to start.

Example
package main

import (
	"fmt"
	"time"

	"github.com/thnhdatle/foreman/foremantest"
)

func main() {
	clk := foremantest.NewClock(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC))
	// foreman.NewClient(pool, foreman.Config{Clock: clk, ...})
	clk.Advance(90 * time.Minute)
	fmt.Println(clk.Now())
}
Output:
2026-01-01 01:30:00 +0000 UTC

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance moves the fake time forward and fires due timers and tickers.

func (*Clock) After

func (c *Clock) After(d time.Duration) <-chan time.Time

After fires once the fake time has advanced by d.

func (*Clock) NewTicker

func (c *Clock) NewTicker(d time.Duration) foreman.Ticker

NewTicker returns a ticker driven by the fake time.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns the fake time.

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set moves the fake time to t (never backwards).

func (*Clock) Waiters

func (c *Clock) Waiters() int

Waiters returns the number of pending timers and tickers.

type WorkOpts

type WorkOpts struct {
	// Attempt is the attempt number (default 1); MaxAttempts defaults to 25.
	Attempt     int
	MaxAttempts int
	// Timeout applies to Work; 0 = none.
	Timeout time.Duration
	// RetryPolicy defaults to foreman.ExponentialBackoff{}.
	RetryPolicy foreman.RetryPolicy
	// Clock defaults to the wall clock.
	Clock foreman.Clock
}

WorkOpts configures WorkJob.

type WorkResult

type WorkResult struct {
	// State is the state the job would be written in: completed, retryable,
	// scheduled (snoozed), discarded or cancelled.
	State foreman.State
	// ScheduledAt is the next run time for retryable and snoozed jobs.
	ScheduledAt time.Time
	// Err is the error recorded for the attempt, if any.
	Err string
}

WorkResult is the outcome of running a job with WorkJob.

func WorkJob

func WorkJob[T foreman.JobArgs](
	ctx context.Context, tb testing.TB, worker foreman.Worker[T], args T, opts *WorkOpts,
) *WorkResult

WorkJob runs worker on args in memory, through the same executor and result mapping as a client, without a database. CompleteTx and Heartbeat are unavailable (there is no client); use WorkInline for those.

Example
worker := foreman.WorkFunc[mailArgs](func(context.Context, *foreman.Job[mailArgs]) error {
	return errors.New("smtp unavailable")
})
res := foremantest.WorkJob(context.Background(), &testing.T{}, worker, mailArgs{To: "a@example.com"}, nil)
fmt.Println(res.State, res.Err)
Output:
retryable smtp unavailable

Jump to

Keyboard shortcuts

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