-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvshard_test.go
153 lines (121 loc) · 3.65 KB
/
vshard_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
package goconsist
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDistribute(t *testing.T) {
t.Run("should generate ring with 4 sections and step 1", func(t *testing.T) {
singleRange := uint32(1)
rangesCount := uint32(4)
expected := []section{
{min: 0, max: 1},
{min: 2, max: 3},
{min: 4, max: 5},
{min: 6, max: 0},
}
res := distribute(singleRange, rangesCount)
assert.Equal(t, expected, res)
})
t.Run("should generate ring with 6 sections and step 15", func(t *testing.T) {
singleRange := uint32(15)
rangesCount := uint32(6)
expected := []section{
{min: 0, max: 15},
{min: 16, max: 31},
{min: 32, max: 47},
{min: 48, max: 63},
{min: 64, max: 79},
{min: 80, max: 0},
}
res := distribute(singleRange, rangesCount)
assert.Equal(t, expected, res)
})
}
func TestVshard_More(t *testing.T) {
shard := section{max: 5}
t.Run("should return true if specified x is more then section max", func(t *testing.T) {
res := shard.more(6)
assert.True(t, res)
})
t.Run("should return false if specified x is less then section max", func(t *testing.T) {
res := shard.more(4)
assert.False(t, res)
})
t.Run("should return false if specified x is equals to section max", func(t *testing.T) {
res := shard.more(5)
assert.False(t, res)
})
}
func TestVshard_Less(t *testing.T) {
shard := section{min: 5}
t.Run("should return true if specified x is less then section min", func(t *testing.T) {
res := shard.less(4)
assert.True(t, res)
})
t.Run("should return false if specified x is more then section min", func(t *testing.T) {
res := shard.less(6)
assert.False(t, res)
})
t.Run("should return false if specified x is equals to section min", func(t *testing.T) {
res := shard.less(5)
assert.False(t, res)
})
}
func TestVshard_Between(t *testing.T) {
shard := section{min: 4, max: 6}
t.Run("should return true if specified x is more then min factor and current section is last", func(t *testing.T) {
shard := section{min: 4, max: 0}
res := shard.between(5)
assert.True(t, res)
})
t.Run("should return true if specified x is between section min and max", func(t *testing.T) {
res := shard.between(5)
assert.True(t, res)
})
t.Run("should return true if specified x is equals to section min", func(t *testing.T) {
res := shard.between(4)
assert.True(t, res)
})
t.Run("should return true if specified x is equals to section max", func(t *testing.T) {
res := shard.between(6)
assert.True(t, res)
})
t.Run("should return false if specified x is outside section min and max range", func(t *testing.T) {
res := shard.between(3)
assert.False(t, res)
})
}
func TestSearch(t *testing.T) {
shards := []section{
{min: 0, max: 15},
{min: 16, max: 31},
{min: 32, max: 47},
{min: 48, max: 63},
{min: 64, max: 79},
{min: 80, max: 0},
}
t.Run("should return section and true if target found", func(t *testing.T) {
expected := section{min: 32, max: 47}
res, ok := search(shards, 34)
assert.True(t, ok)
assert.Equal(t, expected, res)
})
t.Run("should return true if target more then min of last section", func(t *testing.T) {
expected := section{min: 80, max: 0}
res, ok := search(shards, 100)
assert.True(t, ok)
assert.Equal(t, expected, res)
})
t.Run("should return true if target equals to first section", func(t *testing.T) {
expected := section{min: 0, max: 15}
res, ok := search(shards, 0)
assert.True(t, ok)
assert.Equal(t, expected, res)
})
t.Run("should return true if target equals to last section", func(t *testing.T) {
expected := section{min: 80, max: 0}
res, ok := search(shards, 80)
assert.True(t, ok)
assert.Equal(t, expected, res)
})
}