testdeadline

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 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 testdeadline bounds a test's subprocess and device waits by the test binary's own deadline instead of a fixed timeout.

A fixed timeout of a few seconds around a compile or an exec fails on a loaded host, and fails as "signal: killed" rather than as a timeout. Context ends shortly before go test -timeout would panic, so a slow wait gets all the time the run allows and a hung one is still reported by the test. Command runs a subprocess under that context and stops waiting for its children's output soon after the context is done. Err makes an error caused by that deadline say it timed out.

ctx := testdeadline.Context(t)
out, err := testdeadline.Command(ctx, nvcc, "-o", bin, src).CombinedOutput()
if err != nil {
	t.Fatalf("nvcc: %v\n%s", testdeadline.Err(ctx, err), out)
}

Index

Examples

Constants

View Source
const MaxMargin = 10 * time.Second

MaxMargin is the most time Context leaves between its deadline and the test binary's, for the test to stop its subprocess and report.

View Source
const MinMargin = WaitDelay + time.Second

MinMargin is the least time Context leaves between its deadline and the test binary's: a Command's WaitDelay and a second to report. With less than MinMargin left, the context returned is already done, so a test's later subprocesses fail at once instead of outliving the binary.

View Source
const WaitDelay = time.Second

WaitDelay is the WaitDelay of a command made by Command: how long its Wait waits, once ctx is done and the process killed, for the process's children to close its output.

Variables

This section is empty.

Functions

func Command

func Command(ctx context.Context, name string, arg ...string) *exec.Cmd

Command is like exec.CommandContext but sets the command's WaitDelay to WaitDelay. Killing a process does not kill its children, and a child that inherited the process's output keeps the command's Wait from returning: nvcc's cicc and ptxas do this, as do a compiler driver's linker and a shell's commands. Without WaitDelay, a compile killed at the deadline of ctx is still waited for until its children finish.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/tmc/lib/nvidia/internal/testdeadline"
)

func main() {
	ctx, cancel := context.WithDeadlineCause(context.Background(), time.Now(), errors.New("timed out before the go test deadline"))
	defer cancel()
	_, err := testdeadline.Command(ctx, "sh", "-c", "sleep 30; echo done").CombinedOutput()
	fmt.Println(testdeadline.Err(ctx, err))
}
Output:
timed out before the go test deadline: context deadline exceeded

func Context

func Context(t T) context.Context

Context returns a context derived from t.Context that is canceled when the test ends and that times out before t's deadline: a tenth of the time remaining before it, but no more than MaxMargin and no less than MinMargin. If the test binary has no deadline (go test -timeout 0), the context does not time out.

func Err

func Err(ctx context.Context, err error) error

Err returns err unchanged unless ctx has timed out, in which case it returns an error that says so and wraps both context.DeadlineExceeded and err. A subprocess killed by its context fails with only the signal that killed it; Err is how its test says it timed out.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"os/exec"
	"time"

	"github.com/tmc/lib/nvidia/internal/testdeadline"
)

func main() {
	ctx, cancel := context.WithDeadlineCause(context.Background(), time.Now(), errors.New("timed out before the go test deadline"))
	defer cancel()
	err := exec.CommandContext(ctx, "sleep", "1").Run()
	fmt.Println(testdeadline.Err(ctx, err))
}
Output:
timed out before the go test deadline: context deadline exceeded

Types

type T

type T interface {
	Helper()
	Context() context.Context
	Deadline() (time.Time, bool)
	Cleanup(func())
}

A T is the part of *testing.T that Context uses.

Jump to

Keyboard shortcuts

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