1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package view
17
18 import (
19 "reflect"
20 "testing"
21 "time"
22
23 "github.com/google/go-cmp/cmp"
24 "github.com/google/go-cmp/cmp/cmpopts"
25 "go.opencensus.io/metric/metricdata"
26 )
27
28 func TestDataClone(t *testing.T) {
29 agg := &Aggregation{
30 Buckets: []float64{1, 2, 3, 4},
31 }
32 dist := newDistributionData(agg, time.Time{})
33 dist.Count = 7
34 dist.Max = 11
35 dist.Min = 1
36 dist.CountPerBucket = []int64{0, 2, 3, 2}
37 dist.Mean = 4
38 dist.SumOfSquaredDev = 1.2
39
40 tests := []struct {
41 name string
42 src AggregationData
43 }{
44 {
45 name: "count data",
46 src: &CountData{Value: 5},
47 },
48 {
49 name: "distribution data",
50 src: dist,
51 },
52 {
53 name: "sum data",
54 src: &SumData{Value: 65.7},
55 },
56 }
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
59 got := tt.src.clone()
60 if !reflect.DeepEqual(got, tt.src) {
61 t.Errorf("AggregationData.clone() = %v, want %v", got, tt.src)
62 }
63
64 if got == tt.src {
65 t.Errorf("AggregationData.clone() returned the same pointer")
66 }
67 })
68 }
69 }
70
71 func TestDistributionData_addSample(t *testing.T) {
72 agg := &Aggregation{
73 Buckets: []float64{1, 2},
74 }
75 dd := newDistributionData(agg, time.Time{})
76 attachments1 := map[string]interface{}{"key1": "value1"}
77 t1 := time.Now()
78 dd.addSample(0.5, attachments1, t1)
79
80 e1 := &metricdata.Exemplar{Value: 0.5, Timestamp: t1, Attachments: attachments1}
81 want := &DistributionData{
82 Count: 1,
83 CountPerBucket: []int64{1, 0, 0},
84 ExemplarsPerBucket: []*metricdata.Exemplar{e1, nil, nil},
85 Max: 0.5,
86 Min: 0.5,
87 Mean: 0.5,
88 SumOfSquaredDev: 0,
89 }
90 if diff := cmpDD(dd, want); diff != "" {
91 t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
92 }
93
94 attachments2 := map[string]interface{}{"key2": "value2"}
95 t2 := t1.Add(time.Microsecond)
96 dd.addSample(0.7, attachments2, t2)
97
98
99 e2 := &metricdata.Exemplar{Value: 0.7, Timestamp: t2, Attachments: attachments2}
100 want = &DistributionData{
101 Count: 2,
102 CountPerBucket: []int64{2, 0, 0},
103 ExemplarsPerBucket: []*metricdata.Exemplar{e2, nil, nil},
104 Max: 0.7,
105 Min: 0.5,
106 Mean: 0.6,
107 SumOfSquaredDev: 0,
108 }
109 if diff := cmpDD(dd, want); diff != "" {
110 t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
111 }
112
113 attachments3 := map[string]interface{}{"key3": "value3"}
114 t3 := t2.Add(time.Microsecond)
115 dd.addSample(1.2, attachments3, t3)
116
117
118 e3 := &metricdata.Exemplar{Value: 1.2, Timestamp: t3, Attachments: attachments3}
119 want = &DistributionData{
120 Count: 3,
121 CountPerBucket: []int64{2, 1, 0},
122 ExemplarsPerBucket: []*metricdata.Exemplar{e2, e3, nil},
123 Max: 1.2,
124 Min: 0.5,
125 Mean: 0.7999999999999999,
126 SumOfSquaredDev: 0,
127 }
128 if diff := cmpDD(dd, want); diff != "" {
129 t.Fatalf("Unexpected DistributionData -got +want: %s", diff)
130 }
131 }
132
133 func cmpDD(got, want *DistributionData) string {
134 return cmp.Diff(got, want, cmpopts.IgnoreFields(DistributionData{}, "SumOfSquaredDev"), cmpopts.IgnoreUnexported(DistributionData{}))
135 }
136
View as plain text