Debian Codegraph @

trace/reader_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Code generated by "gen.bash" from internal/trace; DO NOT EDIT.
6
7 //go:build go1.23
8
9 package trace_test
10
11 import (
12 "bytes"
13 "flag"
14 "io"
15 "path/filepath"
16 "runtime"
17 "testing"
18 "time"
19
20 "golang.org/x/exp/trace"
21 "golang.org/x/exp/trace/internal/testtrace"
22 )
23
24 var (
25 logEvents = flag.Bool("log-events", false, "whether to log high-level events; significantly slows down tests")
26 dumpTraces = flag.Bool("dump-traces", false, "dump traces even on success")
27 )
28
29 func TestReaderGolden(t *testing.T) {
30 matches, err := filepath.Glob("./testdata/tests/*.test")
31 if err != nil {
32 t.Fatalf("failed to glob for tests: %v", err)
33 }
34 for _, testPath := range matches {
35 testName, err := filepath.Rel("./testdata", testPath)
36 if err != nil {
37 t.Fatalf("failed to relativize testdata path: %v", err)
38 }
39 t.Run(testName, func(t *testing.T) {
40 tr, ver, exp, err := testtrace.ParseFile(testPath)
41 if err != nil {
42 t.Fatalf("failed to parse test file at %s: %v", testPath, err)
43 }
44 v := testtrace.NewValidator()
45 v.GoVersion = ver
46 testReader(t, tr, v, exp)
47 })
48 }
49 }
50
51 func FuzzReader(f *testing.F) {
52 // Currently disabled because the parser doesn't do much validation and most
53 // getters can be made to panic. Turn this on once the parser is meant to
54 // reject invalid traces.
55 const testGetters = false
56
57 f.Fuzz(func(t *testing.T, b []byte) {
58 r, err := trace.NewReader(bytes.NewReader(b))
59 if err != nil {
60 return
61 }
62 for {
63 ev, err := r.ReadEvent()
64 if err != nil {
65 break
66 }
67
68 if !testGetters {
69 continue
70 }
71 // Make sure getters don't do anything that panics
72 switch ev.Kind() {
73 case trace.EventLabel:
74 ev.Label()
75 case trace.EventLog:
76 ev.Log()
77 case trace.EventMetric:
78 ev.Metric()
79 case trace.EventRangeActive, trace.EventRangeBegin:
80 ev.Range()
81 case trace.EventRangeEnd:
82 ev.Range()
83 ev.RangeAttributes()
84 case trace.EventStateTransition:
85 ev.StateTransition()
86 case trace.EventRegionBegin, trace.EventRegionEnd:
87 ev.Region()
88 case trace.EventTaskBegin, trace.EventTaskEnd:
89 ev.Task()
90 case trace.EventSync:
91 case trace.EventStackSample:
92 case trace.EventBad:
93 }
94 }
95 })
96 }
97
98 func testReader(t *testing.T, tr io.Reader, v *testtrace.Validator, exp *testtrace.Expectation) {
99 r, err := trace.NewReader(tr)
100 if err != nil {
101 if err := exp.Check(err); err != nil {
102 t.Error(err)
103 }
104 return
105 }
106 for {
107 ev, err := r.ReadEvent()
108 if err == io.EOF {
109 break
110 }
111 v.GoVersion = r.GoVersion()
112 if runtime.GOOS == "windows" || runtime.GOARCH == "wasm" {
113 v.SkipClockSnapshotChecks()
114 }
115 if err != nil {
116 if err := exp.Check(err); err != nil {
117 t.Error(err)
118 }
119 return
120 }
121 if *logEvents {
122 t.Log(ev.String())
123 }
124 if err := v.Event(ev); err != nil {
125 t.Error(err)
126 }
127 }
128 if err := exp.Check(nil); err != nil {
129 t.Error(err)
130 }
131 }
132
133 func TestTraceGenSync(t *testing.T) {
134 type sync struct {
135 Time trace.Time
136 ClockSnapshot *trace.ClockSnapshot
137 }
138 runTest := func(testName string, wantSyncs []sync) {
139 t.Run(testName, func(t *testing.T) {
140 testPath := "testdata/tests/" + testName
141 r, _, _, err := testtrace.ParseFile(testPath)
142 if err != nil {
143 t.Fatalf("malformed test %s: bad trace file: %v", testPath, err)
144 }
145 tr, err := trace.NewReader(r)
146 if err != nil {
147 t.Fatalf("malformed test %s: bad trace file: %v", testPath, err)
148 }
149 var syncEvents []trace.Event
150 for {
151 ev, err := tr.ReadEvent()
152 if err == io.EOF {
153 break
154 }
155 if err != nil {
156 t.Fatalf("malformed test %s: bad trace file: %v", testPath, err)
157 }
158 if ev.Kind() == trace.EventSync {
159 syncEvents = append(syncEvents, ev)
160 }
161 }
162
163 if got, want := len(syncEvents), len(wantSyncs); got != want {
164 t.Errorf("got %d sync events, want %d", got, want)
165 }
166
167 for i, want := range wantSyncs {
168 got := syncEvents[i]
169 gotSync := syncEvents[i].Sync()
170 if got.Time() != want.Time {
171 t.Errorf("sync=%d got time %d, want %d", i+1, got.Time(), want.Time)
172 }
173 if gotSync.ClockSnapshot == nil && want.ClockSnapshot == nil {
174 continue
175 }
176 if gotSync.ClockSnapshot.Trace != want.ClockSnapshot.Trace {
177 t.Errorf("sync=%d got trace time %d, want %d", i+1, gotSync.ClockSnapshot.Trace, want.ClockSnapshot.Trace)
178 }
179 if !gotSync.ClockSnapshot.Wall.Equal(want.ClockSnapshot.Wall) {
180 t.Errorf("sync=%d got wall time %s, want %s", i+1, gotSync.ClockSnapshot.Wall, want.ClockSnapshot.Wall)
181 }
182 if gotSync.ClockSnapshot.Mono != want.ClockSnapshot.Mono {
183 t.Errorf("sync=%d got mono time %d, want %d", i+1, gotSync.ClockSnapshot.Mono, want.ClockSnapshot.Mono)
184 }
185 }
186 })
187 }
188
189 runTest("go123-sync.test", []sync{
190 {10, nil},
191 {40, nil},
192 // The EvFrequency batch for generation 3 is emitted at trace.Time(80),
193 // but 60 is the minTs of the generation, see b30 in the go generator.
194 {60, nil},
195 {63, nil},
196 })
197
198 runTest("go125-sync.test", []sync{
199 {9, &trace.ClockSnapshot{Trace: 10, Mono: 99, Wall: time.Date(2025, 2, 28, 15, 4, 9, 123, time.UTC)}},
200 {38, &trace.ClockSnapshot{Trace: 40, Mono: 199, Wall: time.Date(2025, 2, 28, 15, 4, 10, 123, time.UTC)}},
201 {58, &trace.ClockSnapshot{Trace: 60, Mono: 299, Wall: time.Date(2025, 2, 28, 15, 4, 11, 123, time.UTC)}},
202 {83, nil},
203 })
204 }