File size: 967 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[!fuzz] skip
[short] skip
env GOCACHE=$WORK/cache

# Test fuzz.Context.
go test -vet=off context_fuzz_test.go
stdout ^ok
! stdout FAIL

go test -vet=off -fuzz=Fuzz -fuzztime=1x context_fuzz_test.go
stdout ok
! stdout FAIL

-- context_fuzz_test.go --
package context_fuzz

import (
	"context"
	"errors"
	"testing"
)

func Fuzz(f *testing.F) {
	ctx := f.Context()
	if err := ctx.Err(); err != nil {
		f.Fatalf("expected non-canceled context, got %v", err)
	}

	f.Fuzz(func(t *testing.T, data []byte) {
		innerCtx := t.Context()
		if err := innerCtx.Err(); err != nil {
			t.Fatalf("expected inner test to not inherit canceled context, got %v", err)
		}

		t.Cleanup(func() {
			if !errors.Is(innerCtx.Err(), context.Canceled) {
				t.Fatal("expected context of inner test to be canceled after its fuzz function finished")
			}
		})
	})

	f.Cleanup(func() {
		if !errors.Is(ctx.Err(), context.Canceled) {
			f.Fatal("expected context canceled before cleanup")
		}
	})
}