taskmilltest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package taskmilltest helps test code that uses taskmill.

The helpers work against a real PostgreSQL database, so tests see the same transactions, FIFO groups, deduplication, and retries as production. Each test gets its own schema from NewClient, so tests can run in parallel. Every helper fails the test on errors in its own setup.

A typical test checks two things: that the code under test enqueues the right tasks, with Enqueued, and that a handler does the right work, with Run. An integration test can also process everything enqueued, with Work.

Example

This example tests code that enqueues a task, and the task's handler; see testSignUp above, which TestExampleRuns calls.

package main

import (
	"context"
	"testing"

	"github.com/jackc/pgx/v5/pgxpool"

	"github.com/code19m/taskmill"
	"github.com/code19m/taskmill/taskmilltest"
)

// SendWelcome is the task that signing up enqueues.
type SendWelcome struct {
	UserID int64 `json:"user_id"`
}

func (SendWelcome) Kind() string { return "user.welcome" }

// signUp is the code under test.
func signUp(ctx context.Context, client *taskmill.Client, userID int64) error {
	_, err := client.Enqueue(ctx, SendWelcome{UserID: userID}, nil)
	return err
}

// testSignUp is the body of a test, TestSignUp(t), that checks that signing
// up enqueues a welcome email, and that the email's handler succeeds. pool
// connects to the test database.
func testSignUp(t *testing.T, pool *pgxpool.Pool, sendEmail func(userID int64) error) {
	t.Helper()
	client := taskmilltest.NewClient(t, pool, nil)
	if err := signUp(t.Context(), client, 42); err != nil {
		t.Fatal(err)
	}
	tasks := taskmilltest.Enqueued[SendWelcome](t, client)
	if len(tasks) != 1 || tasks[0].Args.UserID != 42 {
		t.Fatalf("enqueued %+v", tasks)
	}

	handler := taskmill.HandlerFunc[SendWelcome](func(_ context.Context, task *taskmill.Task[SendWelcome]) error {
		return sendEmail(task.Args.UserID)
	})
	info, err := taskmilltest.Run(t, client, handler, SendWelcome{UserID: 42})
	if err != nil || info.State != taskmill.StateCompleted {
		t.Fatalf("got %s, %v", info.State, err)
	}
}

// This example tests code that enqueues a task, and the task's handler; see
// testSignUp above, which TestExampleRuns calls.
func main() {
	_ = testSignUp
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enqueued

func Enqueued[A taskmill.Args](tb testing.TB, client *taskmill.Client) []*taskmill.Task[A]

Enqueued returns every committed task of A's kind, oldest first, with its arguments decoded: unfinished tasks, finished ones, and those in history. Use it to check that the code under test enqueued a task when its transaction committed, and no task when it rolled back.

The returned tasks cannot complete themselves with taskmill.Task.CompleteTx; use Run to run a handler.

func NewClient

func NewClient(tb testing.TB, pool *pgxpool.Pool, cfg *taskmill.ClientConfig) *taskmill.Client

NewClient migrates a new schema with a random name in the database that pool connects to, and returns a client for it. The schema is dropped when the test ends. cfg may be nil; its Schema must be empty.

func Run

func Run[A taskmill.Args](tb testing.TB, client *taskmill.Client, h taskmill.Handler[A], args A) (*taskmill.TaskInfo, error)

Run enqueues a task with args, runs h on it once, the way a worker does, and records the outcome. The attempt holds a real lease, so h may complete the task with taskmill.Task.CompleteTx. Run does not retry: a failed task stays retryable or dead. It returns the task afterwards, and the error h returned, or an error describing its panic. To run a task enqueued with other options, or again after a failure, use taskmill.Worker.RunTask.

func Work

func Work(tb testing.TB, client *taskmill.Client, cfg *taskmill.WorkerConfig)

Work runs a worker with cfg until no task is available or running, then stops it. Tasks that wait for a later run time, such as retries, stay waiting. If cfg.PollInterval is zero, the worker polls every 10 milliseconds. Work fails the test if the worker fails, or if tasks are still available or running after a minute, for example in a queue that the worker does not serve.

Types

This section is empty.

Jump to

Keyboard shortcuts

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