...

Text file src/cmd/go/testdata/script/test_fuzz_deadline.txt

Documentation: cmd/go/testdata/script

     1[!fuzz] skip
     2[short] skip
     3env GOCACHE=$WORK/cache
     4
     5# Warm up the build cache with GOMAXPROCS unrestricted.
     6go test -c -o $devnull
     7
     8# For the fuzzing phase, we reduce GOMAXPROCS to avoid consuming too many
     9# resources during the test. Ideally this would just free up resources to run
    10# other parallel tests more quickly, but unfortunately it is actually necessary
    11# in some 32-bit environments to prevent the fuzzing engine from running out of
    12# address space (see https://go.dev/issue/65434).
    13env GOMAXPROCS=2
    14
    15# The fuzz function should be able to detect whether -timeout
    16# was set with T.Deadline. Note there is no F.Deadline, and
    17# there is no timeout while fuzzing, even if -fuzztime is set.
    18go test -run=FuzzDeadline -wantdeadline=true # -timeout defaults to 10m
    19go test -run=FuzzDeadline -timeout=0 -wantdeadline=false
    20! go test -run=FuzzDeadline -timeout=1s -wantdeadline=false
    21go test -run=FuzzDeadline -timeout=1s -wantdeadline=true
    22go test -fuzz=FuzzDeadline -timeout=0 -fuzztime=1s -wantdeadline=false
    23go test -fuzz=FuzzDeadline -timeout=0 -fuzztime=100x -wantdeadline=false
    24
    25-- go.mod --
    26module fuzz
    27
    28go 1.16
    29-- fuzz_deadline_test.go --
    30package fuzz_test
    31
    32import (
    33	"flag"
    34	"testing"
    35)
    36
    37var wantDeadline = flag.Bool("wantdeadline", false, "whether the test should have a deadline")
    38
    39func FuzzDeadline(f *testing.F) {
    40	f.Add("run once")
    41	f.Fuzz(func (t *testing.T, _ string) {
    42		if _, hasDeadline := t.Deadline(); hasDeadline != *wantDeadline {
    43			t.Fatalf("function got %v; want %v", hasDeadline, *wantDeadline)
    44		}
    45	})
    46}

View as plain text