-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_test.go
101 lines (81 loc) · 2.51 KB
/
shard_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
package tsdb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewEntry(t *testing.T) {
e := newEntry([]Value[int]{{100, 200}})
assert.Len(t, e.values, 1)
}
func TestEntry_Add(t *testing.T) {
var e entry[int]
e.add([]Value[int]{{200, 400}})
assert.Len(t, e.values, 1)
}
func TestEntry_RemoveBefore(t *testing.T) {
var e entry[int]
e.add([]Value[int]{{100, 100}, {200, 200}, {300, 300}})
e.removeBefore(200)
assert.Equal(t, []Value[int]{{200, 200}, {300, 300}}, e.values)
}
func TestEntry_ValuesBetween(t *testing.T) {
var e entry[int]
e.add([]Value[int]{{50, 50}, {100, 100}, {200, 200}, {300, 300}, {400, 400}})
values := e.valuesBetween(100, 300)
assert.Equal(t, []Value[int]{{100, 100}, {200, 200}, {300, 300}}, values)
}
func TestPartition_Write(t *testing.T) {
p := newPartition[int]()
// 新 key 写入
p.write("a", []Value[int]{{100, 200}})
expect := make(map[string]*entry[int])
expect["a"] = newEntry([]Value[int]{{100, 200}})
assert.Equal(t, expect, p.store)
// 写入已经存在的 key
p.write("a", []Value[int]{{300, 400}})
expect = make(map[string]*entry[int])
expect["a"] = newEntry([]Value[int]{{100, 200}, {300, 400}})
assert.Equal(t, expect, p.store)
}
func TestPartition_RemoveBefore(t *testing.T) {
p := newPartition[int]()
p.write("a", []Value[int]{{100, 200}})
p.removeBefore(200)
assert.Empty(t, p.store["a"].values)
// 检查 slice 和 map 缩容
p.removeBefore(200)
assert.Empty(t, p.store)
}
func TestPartition_ValuesBetween(t *testing.T) {
p := newPartition[int]()
p.write("a", []Value[int]{{50, 50}, {100, 100}, {200, 200}, {300, 300}, {400, 400}})
values := p.valuesBetween("a", 100, 300)
assert.Equal(t, []Value[int]{{100, 100}, {200, 200}, {300, 300}}, values)
values = p.valuesBetween("b", 400, 600)
assert.Empty(t, values)
}
func TestShard_RemoveBefore(t *testing.T) {
s := newShard[int]()
s.writeMulti(map[string][]Value[int]{"a": {{100, 200}, {300, 400}}, "c": {{300, 400}}})
s.removeBefore(400)
for _, p := range s.partitions {
for _, e := range p.store {
assert.Empty(t, e.values)
}
}
}
func TestShard_GetPartition(t *testing.T) {
s := newShard[int]()
p := s.getPartitions("a")
assert.NotNil(t, p)
}
func TestShard_WriteMulti(t *testing.T) {
s := newShard[int]()
s.writeMulti(map[string][]Value[int]{"a": {{100, 200}, {300, 400}}, "c": {{300, 400}}})
p := s.getPartitions("a")
assert.Len(t, p.store, 1)
assert.Len(t, p.store["a"].values, 2)
p = s.getPartitions("c")
assert.Len(t, p.store, 1)
assert.Len(t, p.store["c"].values, 1)
}