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 ¶
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.
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.
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 ¶
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 ¶
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 ¶
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