Documentation
¶
Overview ¶
Package foremantest provides helpers for testing code that uses Foreman.
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 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 ¶
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
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 ¶
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
Click to show internal directories.
Click to hide internal directories.