|
| 1 | +// Copyright 2019 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 | +package ocagent |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "reflect" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "golang.org/x/tools/internal/lsp/telemetry/log" |
| 14 | + "golang.org/x/tools/internal/lsp/telemetry/ocagent/wire" |
| 15 | + "golang.org/x/tools/internal/lsp/telemetry/tag" |
| 16 | +) |
| 17 | + |
| 18 | +func TestConvert_annotation(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + tagList tag.List |
| 22 | + want *wire.Annotation |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "no tags", |
| 26 | + tagList: nil, |
| 27 | + want: nil, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "description no error", |
| 31 | + tagList: tag.List{ |
| 32 | + tag.Of(log.MessageTag, "cache miss"), |
| 33 | + tag.Of("db", "godb"), |
| 34 | + }, |
| 35 | + want: &wire.Annotation{ |
| 36 | + Description: &wire.TruncatableString{Value: "cache miss"}, |
| 37 | + Attributes: &wire.Attributes{ |
| 38 | + AttributeMap: map[string]wire.Attribute{ |
| 39 | + "db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + |
| 45 | + { |
| 46 | + name: "description and error", |
| 47 | + tagList: tag.List{ |
| 48 | + tag.Of(log.MessageTag, "cache miss"), |
| 49 | + tag.Of("db", "godb"), |
| 50 | + tag.Of(log.ErrorTag, errors.New("no network connectivity")), |
| 51 | + }, |
| 52 | + want: &wire.Annotation{ |
| 53 | + Description: &wire.TruncatableString{Value: "cache miss"}, |
| 54 | + Attributes: &wire.Attributes{ |
| 55 | + AttributeMap: map[string]wire.Attribute{ |
| 56 | + "Error": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "no network connectivity"}}, |
| 57 | + "db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "no description, but error", |
| 64 | + tagList: tag.List{ |
| 65 | + tag.Of("db", "godb"), |
| 66 | + tag.Of(log.ErrorTag, errors.New("no network connectivity")), |
| 67 | + }, |
| 68 | + want: &wire.Annotation{ |
| 69 | + Description: &wire.TruncatableString{Value: "no network connectivity"}, |
| 70 | + Attributes: &wire.Attributes{ |
| 71 | + AttributeMap: map[string]wire.Attribute{ |
| 72 | + "db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "enumerate all attribute types", |
| 79 | + tagList: tag.List{ |
| 80 | + tag.Of(log.MessageTag, "cache miss"), |
| 81 | + tag.Of("db", "godb"), |
| 82 | + |
| 83 | + tag.Of("age", 0.456), // Constant converted into "float64" |
| 84 | + tag.Of("ttl", float32(5000)), |
| 85 | + tag.Of("expiry_ms", float64(1e3)), |
| 86 | + |
| 87 | + tag.Of("retry", false), |
| 88 | + tag.Of("stale", true), |
| 89 | + |
| 90 | + tag.Of("max", 0x7fff), // Constant converted into "int" |
| 91 | + tag.Of("opcode", int8(0x7e)), |
| 92 | + tag.Of("base", int16(1<<9)), |
| 93 | + tag.Of("checksum", int32(0x11f7e294)), |
| 94 | + tag.Of("mode", int64(0644)), |
| 95 | + |
| 96 | + tag.Of("min", uint(1)), |
| 97 | + tag.Of("mix", uint8(44)), |
| 98 | + tag.Of("port", uint16(55678)), |
| 99 | + tag.Of("min_hops", uint32(1<<9)), |
| 100 | + tag.Of("max_hops", uint64(0xffffff)), |
| 101 | + }, |
| 102 | + want: &wire.Annotation{ |
| 103 | + Description: &wire.TruncatableString{Value: "cache miss"}, |
| 104 | + Attributes: &wire.Attributes{ |
| 105 | + AttributeMap: map[string]wire.Attribute{ |
| 106 | + "db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}}, |
| 107 | + |
| 108 | + "age": wire.DoubleAttribute{DoubleValue: 0.456}, |
| 109 | + "ttl": wire.DoubleAttribute{DoubleValue: 5000.0}, |
| 110 | + "expiry_ms": wire.DoubleAttribute{DoubleValue: 1e3}, |
| 111 | + |
| 112 | + "retry": wire.BoolAttribute{BoolValue: false}, |
| 113 | + "stale": wire.BoolAttribute{BoolValue: true}, |
| 114 | + |
| 115 | + "max": wire.IntAttribute{IntValue: 0x7fff}, |
| 116 | + "opcode": wire.IntAttribute{IntValue: 0x7e}, |
| 117 | + "base": wire.IntAttribute{IntValue: 1 << 9}, |
| 118 | + "checksum": wire.IntAttribute{IntValue: 0x11f7e294}, |
| 119 | + "mode": wire.IntAttribute{IntValue: 0644}, |
| 120 | + |
| 121 | + "min": wire.IntAttribute{IntValue: 1}, |
| 122 | + "mix": wire.IntAttribute{IntValue: 44}, |
| 123 | + "port": wire.IntAttribute{IntValue: 55678}, |
| 124 | + "min_hops": wire.IntAttribute{IntValue: 1 << 9}, |
| 125 | + "max_hops": wire.IntAttribute{IntValue: 0xffffff}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tt := range tests { |
| 133 | + t.Run(tt.name, func(t *testing.T) { |
| 134 | + got := convertAnnotation(tt.tagList) |
| 135 | + if !reflect.DeepEqual(got, tt.want) { |
| 136 | + t.Fatalf("Got:\n%s\nWant:\n%s", marshaled(got), marshaled(tt.want)) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func marshaled(v interface{}) string { |
| 143 | + blob, _ := json.MarshalIndent(v, "", " ") |
| 144 | + return string(blob) |
| 145 | +} |
0 commit comments