foremantest

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package foremantest provides helpers for testing code that uses Foreman.

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 matching job exists and returns it.

Example
var (
	t      *testing.T
	client *foreman.Client
)
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 matching job 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 and runs the job synchronously on client.

Example
var (
	t      *testing.T
	client *foreman.Client
)
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.

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))
	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     time.Duration
	RetryPolicy foreman.RetryPolicy
	Clock       foreman.Clock
}

WorkOpts configures WorkJob.

type WorkResult

type WorkResult struct {
	State       foreman.State
	ScheduledAt time.Time
	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, without a database.

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