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 ¶
- func RequireInserted[T foreman.JobArgs](ctx context.Context, tb testing.TB, client *foreman.Client, expected T) *foreman.JobRow
- func RequireNotInserted[T foreman.JobArgs](ctx context.Context, tb testing.TB, client *foreman.Client, args T)
- func WorkInline[T foreman.JobArgs](ctx context.Context, tb testing.TB, client *foreman.Client, args T, ...) *foreman.JobRow
- type Clock
- type WorkOpts
- type WorkResult
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 ¶
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
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