Skip to content

Commit 95cb2a1

Browse files
Ian Cottrellianthehat
Ian Cottrell
authored andcommitted
internal/telemetry: make test event with functions
This will allow us to use the public API to generate the events. This avoids creating the structs by hand. It also helps tests the other API. Change-Id: Ic90cbbaf6fc97c2a3e6a5ff64dccdff0d65ec865 Reviewed-on: https://go-review.googlesource.com/c/tools/+/206885 Run-TryBot: Ian Cottrell <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 023c5ee commit 95cb2a1

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

internal/telemetry/export/ocagent/ocagent_test.go

+51-41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package ocagent
66

77
import (
8+
"context"
89
"encoding/json"
910
"errors"
1011
"reflect"
@@ -17,20 +18,23 @@ import (
1718
func TestConvert_annotation(t *testing.T) {
1819
tests := []struct {
1920
name string
20-
event telemetry.Event
21+
event func(ctx context.Context) telemetry.Event
2122
want string
2223
}{
2324
{
24-
name: "no tags",
25-
want: "null",
25+
name: "no tags",
26+
event: func(ctx context.Context) telemetry.Event { return telemetry.Event{} },
27+
want: "null",
2628
},
2729
{
2830
name: "description no error",
29-
event: telemetry.Event{
30-
Message: "cache miss",
31-
Tags: telemetry.TagList{
32-
tag.Of("db", "godb"),
33-
},
31+
event: func(ctx context.Context) telemetry.Event {
32+
return telemetry.Event{
33+
Message: "cache miss",
34+
Tags: telemetry.TagList{
35+
tag.Of("db", "godb"),
36+
},
37+
}
3438
},
3539
want: `{
3640
"description": {
@@ -50,12 +54,14 @@ func TestConvert_annotation(t *testing.T) {
5054

5155
{
5256
name: "description and error",
53-
event: telemetry.Event{
54-
Message: "cache miss",
55-
Error: errors.New("no network connectivity"),
56-
Tags: telemetry.TagList{
57-
tag.Of("db", "godb"),
58-
},
57+
event: func(ctx context.Context) telemetry.Event {
58+
return telemetry.Event{
59+
Message: "cache miss",
60+
Error: errors.New("no network connectivity"),
61+
Tags: telemetry.TagList{
62+
tag.Of("db", "godb"),
63+
},
64+
}
5965
},
6066
want: `{
6167
"description": {
@@ -79,11 +85,13 @@ func TestConvert_annotation(t *testing.T) {
7985
},
8086
{
8187
name: "no description, but error",
82-
event: telemetry.Event{
83-
Error: errors.New("no network connectivity"),
84-
Tags: telemetry.TagList{
85-
tag.Of("db", "godb"),
86-
},
88+
event: func(ctx context.Context) telemetry.Event {
89+
return telemetry.Event{
90+
Error: errors.New("no network connectivity"),
91+
Tags: telemetry.TagList{
92+
tag.Of("db", "godb"),
93+
},
94+
}
8795
},
8896
want: `{
8997
"description": {
@@ -102,30 +110,32 @@ func TestConvert_annotation(t *testing.T) {
102110
},
103111
{
104112
name: "enumerate all attribute types",
105-
event: telemetry.Event{
106-
Message: "cache miss",
107-
Tags: telemetry.TagList{
108-
tag.Of("db", "godb"),
113+
event: func(ctx context.Context) telemetry.Event {
114+
return telemetry.Event{
115+
Message: "cache miss",
116+
Tags: telemetry.TagList{
117+
tag.Of("db", "godb"),
109118

110-
tag.Of("age", 0.456), // Constant converted into "float64"
111-
tag.Of("ttl", float32(5000)),
112-
tag.Of("expiry_ms", float64(1e3)),
119+
tag.Of("age", 0.456), // Constant converted into "float64"
120+
tag.Of("ttl", float32(5000)),
121+
tag.Of("expiry_ms", float64(1e3)),
113122

114-
tag.Of("retry", false),
115-
tag.Of("stale", true),
123+
tag.Of("retry", false),
124+
tag.Of("stale", true),
116125

117-
tag.Of("max", 0x7fff), // Constant converted into "int"
118-
tag.Of("opcode", int8(0x7e)),
119-
tag.Of("base", int16(1<<9)),
120-
tag.Of("checksum", int32(0x11f7e294)),
121-
tag.Of("mode", int64(0644)),
126+
tag.Of("max", 0x7fff), // Constant converted into "int"
127+
tag.Of("opcode", int8(0x7e)),
128+
tag.Of("base", int16(1<<9)),
129+
tag.Of("checksum", int32(0x11f7e294)),
130+
tag.Of("mode", int64(0644)),
122131

123-
tag.Of("min", uint(1)),
124-
tag.Of("mix", uint8(44)),
125-
tag.Of("port", uint16(55678)),
126-
tag.Of("min_hops", uint32(1<<9)),
127-
tag.Of("max_hops", uint64(0xffffff)),
128-
},
132+
tag.Of("min", uint(1)),
133+
tag.Of("mix", uint8(44)),
134+
tag.Of("port", uint16(55678)),
135+
tag.Of("min_hops", uint32(1<<9)),
136+
tag.Of("max_hops", uint64(0xffffff)),
137+
},
138+
}
129139
},
130140
want: `{
131141
"description": {
@@ -186,10 +196,10 @@ func TestConvert_annotation(t *testing.T) {
186196
}`,
187197
},
188198
}
189-
199+
ctx := context.TODO()
190200
for _, tt := range tests {
191201
t.Run(tt.name, func(t *testing.T) {
192-
got := marshaled(convertAnnotation(tt.event))
202+
got := marshaled(convertAnnotation(tt.event(ctx)))
193203
if !reflect.DeepEqual(got, tt.want) {
194204
t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want)
195205
}

0 commit comments

Comments
 (0)