-
Notifications
You must be signed in to change notification settings - Fork 11
/
shard_cluster_test.go
195 lines (152 loc) · 4.33 KB
/
shard_cluster_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
package wizard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func testCreateCluster(prefix string) *StandardCluster {
c := NewCluster(prefix + "-master")
c.RegisterSlave(prefix + "-slave01")
c.RegisterSlave(prefix + "-slave02")
c.RegisterSlave(prefix + "-slave03")
return c
}
func TestShardClusterMaster(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var node *Node
s = &ShardCluster{slotsize: 1}
node = s.Master()
assert.Nil(node, "Master() should be always nil on ShardCluster")
}
func TestShardClusterMasters(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var err error
s = &ShardCluster{slotsize: 1000}
assert.Len(s.Masters(), 0)
c := testCreateCluster("shard01")
err = s.RegisterShard(0, 500, c)
assert.Nil(err)
assert.Len(s.Masters(), 1)
c = testCreateCluster("shard02")
err = s.RegisterShard(501, 999, c)
assert.Nil(err)
assert.Len(s.Masters(), 2)
}
func TestShardClusterSlave(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var node *Node
s = &ShardCluster{slotsize: 1}
node = s.Slave()
assert.Nil(node, "Slave() should be always nil on ShardCluster")
}
func TestShardClusterSelectByKey(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var c *StandardCluster
var err error
s = &ShardCluster{slotsize: 2}
err = s.RegisterShard(0, 0, testCreateCluster("shard01"))
assert.Nil(err)
err = s.RegisterShard(1, 1, testCreateCluster("shard02"))
assert.Nil(err)
c = s.SelectByKey(0)
assert.Equal("shard01-master", c.Master().DB())
c = s.SelectByKey(1)
assert.Equal("shard02-master", c.Master().DB())
c = s.SelectByKey(2)
assert.Equal("shard01-master", c.Master().DB())
c = s.SelectByKey(3)
assert.Equal("shard02-master", c.Master().DB())
c = s.SelectByKey(4)
assert.Equal("shard01-master", c.Master().DB())
c = s.SelectByKey(5)
assert.Equal("shard02-master", c.Master().DB())
}
func TestShardClusterRegisterShard(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var err error
s = &ShardCluster{slotsize: 10}
err = s.RegisterShard(-1, 0, testCreateCluster("min-error"))
assert.NotNil(err, "Slotsize cannot be under 0")
assert.Len(s.List, 0)
err = s.RegisterShard(0, 10, testCreateCluster("max-error"))
assert.NotNil(err, "Slotsize cannot be greater equal than slotsize")
assert.Len(s.List, 0)
err = s.RegisterShard(5, 6, testCreateCluster("shard01"))
assert.Nil(err)
err = s.RegisterShard(6, 9, testCreateCluster("min-error"))
assert.NotNil(err, "Slot min is already registered")
err = s.RegisterShard(0, 5, testCreateCluster("max-error"))
assert.NotNil(err, "Slot max is already registered")
}
func TestShardClusterCheckOverlapped(t *testing.T) {
assert := assert.New(t)
var s *ShardCluster
var err error
s = &ShardCluster{slotsize: 10}
err = s.RegisterShard(5, 6, testCreateCluster("shard01"))
assert.Nil(err)
err = s.checkOverlapped(6, 9)
assert.NotNil(err, "Slot min is already registered")
err = s.checkOverlapped(0, 5)
assert.NotNil(err, "Slot max is already registered")
err = s.checkOverlapped(0, 4)
assert.Nil(err)
err = s.checkOverlapped(7, 9)
assert.Nil(err)
}
func TestShardSetInRange(t *testing.T) {
assert := assert.New(t)
var ss *ShardSet
ss = &ShardSet{
min: 10,
max: 20,
}
assert.False(ss.InRange(9))
assert.True(ss.InRange(10))
assert.True(ss.InRange(11))
assert.True(ss.InRange(19))
assert.True(ss.InRange(20))
assert.False(ss.InRange(21))
}
func TestShardSetCheckSlotSize(t *testing.T) {
assert := assert.New(t)
var ss *ShardSet
var err error
ss = &ShardSet{
min: 10,
max: 20,
}
err = ss.checkSlotSize(19)
assert.NotNil(err, "max must be greater than slotsize")
err = ss.checkSlotSize(20)
assert.NotNil(err, "max must be greater than slotsize")
err = ss.checkSlotSize(21)
assert.Nil(err)
ss = &ShardSet{
min: -2,
max: 20,
}
err = ss.checkSlotSize(21)
assert.NotNil(err, "min must be greater equal than 0")
}
func TestShardSetIsMaxInSlotSize(t *testing.T) {
assert := assert.New(t)
var ss *ShardSet
ss = &ShardSet{
min: 10,
max: 20,
}
assert.False(ss.isMaxInSlotSize(19))
assert.False(ss.isMaxInSlotSize(20))
assert.True(ss.isMaxInSlotSize(21))
}
func TestShardSetIsMinAboveZero(t *testing.T) {
assert := assert.New(t)
assert.False(ShardSet{min: -1}.isMinAboveZero())
assert.True(ShardSet{min: 0}.isMinAboveZero())
assert.True(ShardSet{min: 1}.isMinAboveZero())
}