This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
/
aggregation_data_test.go
135 lines (123 loc) · 3.73 KB
/
aggregation_data_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package view
import (
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"go.opencensus.io/metric/metricdata"
)
func TestDataClone(t *testing.T) {
agg := &Aggregation{
Buckets: []float64{1, 2, 3, 4},
}
dist := newDistributionData(agg, time.Time{})
dist.Count = 7
dist.Max = 11
dist.Min = 1
dist.CountPerBucket = []int64{0, 2, 3, 2}
dist.Mean = 4
dist.SumOfSquaredDev = 1.2
tests := []struct {
name string
src AggregationData
}{
{
name: "count data",
src: &CountData{Value: 5},
},
{
name: "distribution data",
src: dist,
},
{
name: "sum data",
src: &SumData{Value: 65.7},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.src.clone()
if !reflect.DeepEqual(got, tt.src) {
t.Errorf("AggregationData.clone() = %v, want %v", got, tt.src)
}
// TODO(jbd): Make sure that data is deep copied.
if got == tt.src {
t.Errorf("AggregationData.clone() returned the same pointer")
}
})
}
}
func TestDistributionData_addSample(t *testing.T) {
agg := &Aggregation{
Buckets: []float64{1, 2},
}
dd := newDistributionData(agg, time.Time{})
attachments1 := map[string]interface{}{"key1": "value1"}
t1 := time.Now()
dd.addSample(0.5, attachments1, t1)
e1 := &metricdata.Exemplar{Value: 0.5, Timestamp: t1, Attachments: attachments1}
want := &DistributionData{
Count: 1,
CountPerBucket: []int64{1, 0, 0},
ExemplarsPerBucket: []*metricdata.Exemplar{e1, nil, nil},
Max: 0.5,
Min: 0.5,
Mean: 0.5,
SumOfSquaredDev: 0,
}
if diff := cmpDD(dd, want); diff != "" {
t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
}
attachments2 := map[string]interface{}{"key2": "value2"}
t2 := t1.Add(time.Microsecond)
dd.addSample(0.7, attachments2, t2)
// Previous exemplar should be overwritten.
e2 := &metricdata.Exemplar{Value: 0.7, Timestamp: t2, Attachments: attachments2}
want = &DistributionData{
Count: 2,
CountPerBucket: []int64{2, 0, 0},
ExemplarsPerBucket: []*metricdata.Exemplar{e2, nil, nil},
Max: 0.7,
Min: 0.5,
Mean: 0.6,
SumOfSquaredDev: 0,
}
if diff := cmpDD(dd, want); diff != "" {
t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
}
attachments3 := map[string]interface{}{"key3": "value3"}
t3 := t2.Add(time.Microsecond)
dd.addSample(1.2, attachments3, t3)
// e3 is at another bucket. e2 should still be there.
e3 := &metricdata.Exemplar{Value: 1.2, Timestamp: t3, Attachments: attachments3}
want = &DistributionData{
Count: 3,
CountPerBucket: []int64{2, 1, 0},
ExemplarsPerBucket: []*metricdata.Exemplar{e2, e3, nil},
Max: 1.2,
Min: 0.5,
Mean: 0.7999999999999999,
SumOfSquaredDev: 0,
}
if diff := cmpDD(dd, want); diff != "" {
t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
}
}
func cmpDD(got, want *DistributionData) string {
return cmp.Diff(got, want, cmpopts.IgnoreFields(DistributionData{}, "SumOfSquaredDev"), cmpopts.IgnoreUnexported(DistributionData{}))
}