...

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

Documentation: cmd/go/testdata/script

     1[!fuzz] skip
     2[short] skip
     3env GOCACHE=$WORK/cache
     4
     5# Test fuzz.Context.
     6go test -vet=off context_fuzz_test.go
     7stdout ^ok
     8! stdout FAIL
     9
    10go test -vet=off -fuzz=Fuzz -fuzztime=1x context_fuzz_test.go
    11stdout ok
    12! stdout FAIL
    13
    14-- context_fuzz_test.go --
    15package context_fuzz
    16
    17import (
    18	"context"
    19	"errors"
    20	"testing"
    21)
    22
    23func Fuzz(f *testing.F) {
    24	ctx := f.Context()
    25	if err := ctx.Err(); err != nil {
    26		f.Fatalf("expected non-canceled context, got %v", err)
    27	}
    28
    29	f.Fuzz(func(t *testing.T, data []byte) {
    30		innerCtx := t.Context()
    31		if err := innerCtx.Err(); err != nil {
    32			t.Fatalf("expected inner test to not inherit canceled context, got %v", err)
    33		}
    34
    35		t.Cleanup(func() {
    36			if !errors.Is(innerCtx.Err(), context.Canceled) {
    37				t.Fatal("expected context of inner test to be canceled after its fuzz function finished")
    38			}
    39		})
    40	})
    41
    42	f.Cleanup(func() {
    43		if !errors.Is(ctx.Err(), context.Canceled) {
    44			f.Fatal("expected context canceled before cleanup")
    45		}
    46	})
    47}

View as plain text