-
Notifications
You must be signed in to change notification settings - Fork 84
/
memory_partition_test.go
235 lines (227 loc) · 5.68 KB
/
memory_partition_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package tstorage
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_memoryPartition_InsertRows(t *testing.T) {
tests := []struct {
name string
memoryPartition *memoryPartition
rows []Row
wantErr bool
wantDataPoints []*DataPoint
wantOutOfOrderRows []Row
}{
{
name: "insert in-order rows",
memoryPartition: newMemoryPartition(nil, 0, "").(*memoryPartition),
rows: []Row{
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 1, Value: 0.1}},
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 2, Value: 0.1}},
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 3, Value: 0.1}},
},
wantDataPoints: []*DataPoint{
{Timestamp: 1, Value: 0.1},
{Timestamp: 2, Value: 0.1},
{Timestamp: 3, Value: 0.1},
},
wantOutOfOrderRows: []Row{},
},
{
name: "insert out-of-order rows",
memoryPartition: func() *memoryPartition {
m := newMemoryPartition(nil, 0, "").(*memoryPartition)
m.insertRows([]Row{
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 2, Value: 0.1}},
})
return m
}(),
rows: []Row{
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 1, Value: 0.1}},
},
wantDataPoints: []*DataPoint{
{Timestamp: 2, Value: 0.1},
},
wantOutOfOrderRows: []Row{
{Metric: "metric1", DataPoint: DataPoint{Timestamp: 1, Value: 0.1}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOutOfOrder, err := tt.memoryPartition.insertRows(tt.rows)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.wantOutOfOrderRows, gotOutOfOrder)
got, _ := tt.memoryPartition.selectDataPoints("metric1", nil, 0, 4)
assert.Equal(t, tt.wantDataPoints, got)
})
}
}
func Test_memoryPartition_SelectDataPoints(t *testing.T) {
tests := []struct {
name string
metric string
labels []Label
start int64
end int64
memoryPartition *memoryPartition
want []*DataPoint
}{
{
name: "given non-exist metric name",
metric: "unknown",
start: 1,
end: 2,
memoryPartition: newMemoryPartition(nil, 0, "").(*memoryPartition),
want: []*DataPoint{},
},
{
name: "select some points",
metric: "metric1",
start: 2,
end: 5,
memoryPartition: func() *memoryPartition {
m := newMemoryPartition(nil, 0, "").(*memoryPartition)
m.insertRows([]Row{
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 1, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 2, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 3, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 4, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 5, Value: 0.1},
},
})
return m
}(),
want: []*DataPoint{
{Timestamp: 2, Value: 0.1},
{Timestamp: 3, Value: 0.1},
{Timestamp: 4, Value: 0.1},
},
},
{
name: "select all points",
metric: "metric1",
start: 1,
end: 4,
memoryPartition: func() *memoryPartition {
m := newMemoryPartition(nil, 0, "").(*memoryPartition)
m.insertRows([]Row{
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 1, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 2, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 3, Value: 0.1},
},
})
return m
}(),
want: []*DataPoint{
{Timestamp: 1, Value: 0.1},
{Timestamp: 2, Value: 0.1},
{Timestamp: 3, Value: 0.1},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := tt.memoryPartition.selectDataPoints(tt.metric, tt.labels, tt.start, tt.end)
assert.Equal(t, tt.want, got)
})
}
}
func Test_memoryMetric_EncodeAllPoints_sorted(t *testing.T) {
mt := memoryMetric{
points: []*DataPoint{
{Timestamp: 1, Value: 0.1},
{Timestamp: 3, Value: 0.1},
},
outOfOrderPoints: []*DataPoint{
{Timestamp: 4, Value: 0.1},
{Timestamp: 2, Value: 0.1},
},
}
allTimestamps := make([]int64, 0, 4)
encoder := fakeEncoder{
encodePointFunc: func(p *DataPoint) error {
allTimestamps = append(allTimestamps, p.Timestamp)
return nil
},
}
err := mt.encodeAllPoints(&encoder)
require.NoError(t, err)
assert.Equal(t, []int64{1, 2, 3, 4}, allTimestamps)
}
func Test_memoryMetric_EncodeAllPoints_error(t *testing.T) {
mt := memoryMetric{
points: []*DataPoint{{Timestamp: 1, Value: 0.1}},
}
encoder := fakeEncoder{
encodePointFunc: func(p *DataPoint) error {
return fmt.Errorf("some error")
},
}
err := mt.encodeAllPoints(&encoder)
assert.Error(t, err)
}
func Test_toUnix(t *testing.T) {
tests := []struct {
name string
t time.Time
precision TimestampPrecision
want int64
}{
{
name: "to nanosecond",
t: time.Unix(1600000000, 0),
precision: Nanoseconds,
want: 1600000000000000000,
},
{
name: "to microsecond",
t: time.Unix(1600000000, 0),
precision: Microseconds,
want: 1600000000000000,
},
{
name: "to millisecond",
t: time.Unix(1600000000, 0),
precision: Milliseconds,
want: 1600000000000,
},
{
name: "to second",
t: time.Unix(1600000000, 0),
precision: Seconds,
want: 1600000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toUnix(tt.t, tt.precision)
assert.Equal(t, tt.want, got)
})
}
}