| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT. |
| 5 |
// To regenerate this file run "make genpdata". |
| 6 |
|
| 7 |
package internal |
| 8 |
|
| 9 |
import ( |
| 10 |
"strconv" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
gootlpmetrics "go.opentelemetry.io/proto/slim/otlp/metrics/v1" |
| 16 |
"google.golang.org/protobuf/proto" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/featuregate" |
| 19 |
"go.opentelemetry.io/collector/pdata/internal/json" |
| 20 |
) |
| 21 |
|
| 22 |
func TestCopyExponentialHistogram(t *testing.T) { |
| 23 |
for name, src := range genTestEncodingValuesExponentialHistogram() { |
| 24 |
for _, pooling := range []bool{true, false} { |
| 25 |
t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) { |
| 26 |
prevPooling := UseProtoPooling.IsEnabled() |
| 27 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling)) |
| 28 |
defer func() { |
| 29 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling)) |
| 30 |
}() |
| 31 |
|
| 32 |
dest := NewExponentialHistogram() |
| 33 |
CopyExponentialHistogram(dest, src) |
| 34 |
assert.Equal(t, src, dest) |
| 35 |
CopyExponentialHistogram(dest, dest) |
| 36 |
assert.Equal(t, src, dest) |
| 37 |
}) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestCopyExponentialHistogramSlice(t *testing.T) { |
| 43 |
src := []ExponentialHistogram{} |
| 44 |
dest := []ExponentialHistogram{} |
| 45 |
// Test CopyTo empty |
| 46 |
dest = CopyExponentialHistogramSlice(dest, src) |
| 47 |
assert.Equal(t, []ExponentialHistogram{}, dest) |
| 48 |
|
| 49 |
// Test CopyTo larger slice |
| 50 |
src = GenTestExponentialHistogramSlice() |
| 51 |
dest = CopyExponentialHistogramSlice(dest, src) |
| 52 |
assert.Equal(t, GenTestExponentialHistogramSlice(), dest) |
| 53 |
|
| 54 |
// Test CopyTo same size slice |
| 55 |
dest = CopyExponentialHistogramSlice(dest, src) |
| 56 |
assert.Equal(t, GenTestExponentialHistogramSlice(), dest) |
| 57 |
|
| 58 |
// Test CopyTo smaller size slice |
| 59 |
dest = CopyExponentialHistogramSlice(dest, []ExponentialHistogram{}) |
| 60 |
assert.Len(t, dest, 0) |
| 61 |
|
| 62 |
// Test CopyTo larger slice with enough capacity |
| 63 |
dest = CopyExponentialHistogramSlice(dest, src) |
| 64 |
assert.Equal(t, GenTestExponentialHistogramSlice(), dest) |
| 65 |
} |
| 66 |
|
| 67 |
func TestCopyExponentialHistogramPtrSlice(t *testing.T) { |
| 68 |
src := []*ExponentialHistogram{} |
| 69 |
dest := []*ExponentialHistogram{} |
| 70 |
// Test CopyTo empty |
| 71 |
dest = CopyExponentialHistogramPtrSlice(dest, src) |
| 72 |
assert.Equal(t, []*ExponentialHistogram{}, dest) |
| 73 |
|
| 74 |
// Test CopyTo larger slice |
| 75 |
src = GenTestExponentialHistogramPtrSlice() |
| 76 |
dest = CopyExponentialHistogramPtrSlice(dest, src) |
| 77 |
assert.Equal(t, GenTestExponentialHistogramPtrSlice(), dest) |
| 78 |
|
| 79 |
// Test CopyTo same size slice |
| 80 |
dest = CopyExponentialHistogramPtrSlice(dest, src) |
| 81 |
assert.Equal(t, GenTestExponentialHistogramPtrSlice(), dest) |
| 82 |
|
| 83 |
// Test CopyTo smaller size slice |
| 84 |
dest = CopyExponentialHistogramPtrSlice(dest, []*ExponentialHistogram{}) |
| 85 |
assert.Len(t, dest, 0) |
| 86 |
|
| 87 |
// Test CopyTo larger slice with enough capacity |
| 88 |
dest = CopyExponentialHistogramPtrSlice(dest, src) |
| 89 |
assert.Equal(t, GenTestExponentialHistogramPtrSlice(), dest) |
| 90 |
} |
| 91 |
|
| 92 |
func TestMarshalAndUnmarshalJSONExponentialHistogramUnknown(t *testing.T) { |
| 93 |
iter := json.BorrowIterator([]byte(`{"unknown": "string"}`)) |
| 94 |
defer json.ReturnIterator(iter) |
| 95 |
dest := NewExponentialHistogram() |
| 96 |
dest.UnmarshalJSON(iter) |
| 97 |
require.NoError(t, iter.Error()) |
| 98 |
assert.Equal(t, NewExponentialHistogram(), dest) |
| 99 |
} |
| 100 |
|
| 101 |
func TestMarshalAndUnmarshalJSONExponentialHistogram(t *testing.T) { |
| 102 |
for name, src := range genTestEncodingValuesExponentialHistogram() { |
| 103 |
for _, pooling := range []bool{true, false} { |
| 104 |
t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) { |
| 105 |
prevPooling := UseProtoPooling.IsEnabled() |
| 106 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling)) |
| 107 |
defer func() { |
| 108 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling)) |
| 109 |
}() |
| 110 |
|
| 111 |
stream := json.BorrowStream(nil) |
| 112 |
defer json.ReturnStream(stream) |
| 113 |
src.MarshalJSON(stream) |
| 114 |
require.NoError(t, stream.Error()) |
| 115 |
|
| 116 |
iter := json.BorrowIterator(stream.Buffer()) |
| 117 |
defer json.ReturnIterator(iter) |
| 118 |
dest := NewExponentialHistogram() |
| 119 |
dest.UnmarshalJSON(iter) |
| 120 |
require.NoError(t, iter.Error()) |
| 121 |
|
| 122 |
assert.Equal(t, src, dest) |
| 123 |
DeleteExponentialHistogram(dest, true) |
| 124 |
}) |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestMarshalAndUnmarshalProtoExponentialHistogramFailing(t *testing.T) { |
| 130 |
for name, buf := range genTestFailingUnmarshalProtoValuesExponentialHistogram() { |
| 131 |
t.Run(name, func(t *testing.T) { |
| 132 |
dest := NewExponentialHistogram() |
| 133 |
require.Error(t, dest.UnmarshalProto(buf)) |
| 134 |
}) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestMarshalAndUnmarshalProtoExponentialHistogramUnknown(t *testing.T) { |
| 139 |
dest := NewExponentialHistogram() |
| 140 |
// message Test { required int64 field = 1313; } encoding { "field": "1234" } |
| 141 |
require.NoError(t, dest.UnmarshalProto([]byte{0x88, 0x52, 0xD2, 0x09})) |
| 142 |
assert.Equal(t, NewExponentialHistogram(), dest) |
| 143 |
} |
| 144 |
|
| 145 |
func TestMarshalAndUnmarshalProtoExponentialHistogram(t *testing.T) { |
| 146 |
for name, src := range genTestEncodingValuesExponentialHistogram() { |
| 147 |
for _, pooling := range []bool{true, false} { |
| 148 |
t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) { |
| 149 |
prevPooling := UseProtoPooling.IsEnabled() |
| 150 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling)) |
| 151 |
defer func() { |
| 152 |
require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling)) |
| 153 |
}() |
| 154 |
|
| 155 |
buf := make([]byte, src.SizeProto()) |
| 156 |
gotSize := src.MarshalProto(buf) |
| 157 |
assert.Equal(t, len(buf), gotSize) |
| 158 |
|
| 159 |
dest := NewExponentialHistogram() |
| 160 |
require.NoError(t, dest.UnmarshalProto(buf)) |
| 161 |
|
| 162 |
assert.Equal(t, src, dest) |
| 163 |
DeleteExponentialHistogram(dest, true) |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestMarshalAndUnmarshalProtoViaProtobufExponentialHistogram(t *testing.T) { |
| 170 |
for name, src := range genTestEncodingValuesExponentialHistogram() { |
| 171 |
t.Run(name, func(t *testing.T) { |
| 172 |
buf := make([]byte, src.SizeProto()) |
| 173 |
gotSize := src.MarshalProto(buf) |
| 174 |
assert.Equal(t, len(buf), gotSize) |
| 175 |
|
| 176 |
goDest := &gootlpmetrics.ExponentialHistogram{} |
| 177 |
require.NoError(t, proto.Unmarshal(buf, goDest)) |
| 178 |
|
| 179 |
goBuf, err := proto.Marshal(goDest) |
| 180 |
require.NoError(t, err) |
| 181 |
|
| 182 |
dest := NewExponentialHistogram() |
| 183 |
require.NoError(t, dest.UnmarshalProto(goBuf)) |
| 184 |
assert.Equal(t, src, dest) |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func genTestFailingUnmarshalProtoValuesExponentialHistogram() map[string][]byte { |
| 190 |
return map[string][]byte{ |
| 191 |
"invalid_field": {0x02}, |
| 192 |
"DataPoints/wrong_wire_type": {0xc}, |
| 193 |
"DataPoints/missing_value": {0xa}, |
| 194 |
"AggregationTemporality/wrong_wire_type": {0x14}, |
| 195 |
"AggregationTemporality/missing_value": {0x10}, |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func genTestEncodingValuesExponentialHistogram() map[string]*ExponentialHistogram { |
| 200 |
return map[string]*ExponentialHistogram{ |
| 201 |
"empty": NewExponentialHistogram(), |
| 202 |
"DataPoints/test": {DataPoints: []*ExponentialHistogramDataPoint{{}, GenTestExponentialHistogramDataPoint()}}, |
| 203 |
"AggregationTemporality/test": {AggregationTemporality: AggregationTemporality(13)}, |
| 204 |
} |
| 205 |
} |