Debian Codegraph @

trace/internal/tracev2/events_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 tracev2_test
10
11 import (
12 "golang.org/x/exp/trace/internal/tracev2"
13 "iter"
14 "regexp"
15 "slices"
16 "strings"
17 "testing"
18 )
19
20 var argNameRegexp = regexp.MustCompile(`((?P<name>[A-Za-z]+)_)?(?P<type>[A-Za-z]+)`)
21
22 func TestSpecs(t *testing.T) {
23 if tracev2.NumEvents <= 0 {
24 t.Fatalf("no trace events?")
25 }
26 if tracev2.MaxExperimentalEvent < tracev2.MaxEvent {
27 t.Fatalf("max experimental event (%d) is < max event (%d)", tracev2.MaxExperimentalEvent, tracev2.MaxEvent)
28 }
29 specs := tracev2.Specs()
30 for ev := range allEvents() {
31 spec := &specs[ev]
32 if spec.Name == "" {
33 t.Errorf("expected event %d to be defined in specs", ev)
34 continue
35 }
36 if spec.IsTimedEvent && spec.Args[0] != "dt" {
37 t.Errorf("%s is a timed event, but its first argument is not 'dt'", spec.Name)
38 }
39 if spec.HasData && spec.Name != "String" && spec.Name != "ExperimentalBatch" {
40 t.Errorf("%s has data, but is not a special kind of event (unsupported, but could be)", spec.Name)
41 }
42 if spec.IsStack && spec.Name != "Stack" {
43 t.Errorf("%s listed as being a stack, but is not the Stack event (unsupported)", spec.Name)
44 }
45 if spec.IsTimedEvent && len(spec.Args) > tracev2.MaxTimedEventArgs {
46 t.Errorf("%s has too many timed event args: have %d, want %d at most", spec.Name, len(spec.Args), tracev2.MaxTimedEventArgs)
47 }
48 if ev.Experimental() && spec.Experiment == tracev2.NoExperiment {
49 t.Errorf("experimental event %s must have an experiment", spec.Name)
50 }
51
52 // Check arg types.
53 for _, arg := range spec.Args {
54 matches := argNameRegexp.FindStringSubmatch(arg)
55 if len(matches) == 0 {
56 t.Errorf("malformed argument %s for event %s", arg, spec.Name)
57 }
58 }
59
60 // Check stacks.
61 for _, i := range spec.StackIDs {
62 if !strings.HasSuffix(spec.Args[i], "stack") {
63 t.Errorf("stack argument listed at %d in %s, but argument name %s does not imply stack type", i, spec.Name, spec.Args[i])
64 }
65 }
66 for i, arg := range spec.Args {
67 if !strings.HasSuffix(spec.Args[i], "stack") {
68 continue
69 }
70 if !slices.Contains(spec.StackIDs, i) {
71 t.Errorf("found stack argument %s in %s at index %d not listed in StackIDs", arg, spec.Name, i)
72 }
73 }
74
75 // Check strings.
76 for _, i := range spec.StringIDs {
77 if !strings.HasSuffix(spec.Args[i], "string") {
78 t.Errorf("string argument listed at %d in %s, but argument name %s does not imply string type", i, spec.Name, spec.Args[i])
79 }
80 }
81 for i, arg := range spec.Args {
82 if !strings.HasSuffix(spec.Args[i], "string") {
83 continue
84 }
85 if !slices.Contains(spec.StringIDs, i) {
86 t.Errorf("found string argument %s in %s at index %d not listed in StringIDs", arg, spec.Name, i)
87 }
88 }
89 }
90 }
91
92 func allEvents() iter.Seq[tracev2.EventType] {
93 return func(yield func(tracev2.EventType) bool) {
94 for ev := tracev2.EvNone + 1; ev < tracev2.NumEvents; ev++ {
95 if !yield(ev) {
96 return
97 }
98 }
99 for ev := tracev2.MaxEvent + 1; ev < tracev2.NumExperimentalEvents; ev++ {
100 if !yield(ev) {
101 return
102 }
103 }
104 }
105 }