forked from evalphobia/wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
114 lines (87 loc) · 3.39 KB
/
helper_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
package wizard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUseMaster(t *testing.T) {
assert := assert.New(t)
var wiz *Wizard
var c *StandardCluster
wiz = NewWizard()
c = wiz.CreateCluster("country_table", "db-master")
c.RegisterSlave("db-slave")
assert.Equal("db-master", wiz.UseMaster("country_table"))
assert.Nil(wiz.UseMaster("city_table"), "Non registered name")
var s *ShardCluster
s = wiz.CreateShardCluster("user_table", 997)
s.RegisterShard(0, 499, NewCluster("shard01-master"))
s.RegisterShard(500, 996, NewCluster("shard02-master"))
assert.Equal("shard01-master", wiz.UseMaster("user_table"))
}
func TestUseMasters(t *testing.T) {
assert := assert.New(t)
var wiz *Wizard
wiz = NewWizard()
wiz.CreateCluster("country_table", "db-master")
assert.Contains(wiz.UseMasters("country_table"), "db-master")
assert.Empty(wiz.UseMasters("city_table"), "Non registered name")
var s *ShardCluster
s = wiz.CreateShardCluster("user_table", 997)
s.RegisterShard(0, 499, NewCluster("shard01-master"))
s.RegisterShard(500, 996, NewCluster("shard02-master"))
assert.Contains(wiz.UseMasters("user_table"), "shard01-master")
assert.Contains(wiz.UseMasters("user_table"), "shard02-master")
assert.Len(wiz.UseMasters("user_table"), 2)
}
func TestUseSlave(t *testing.T) {
assert := assert.New(t)
var wiz *Wizard
var c *StandardCluster
wiz = NewWizard()
c = wiz.CreateCluster("country_table", "db-master")
assert.Equal("db-master", wiz.UseSlave("country_table"), "Slave() return master when no slaves exists")
c.RegisterSlave("db-slave")
assert.Equal("db-slave", wiz.UseSlave("country_table"))
assert.Nil(wiz.UseSlave("city_table"), "Non registered name")
}
func TestUseMasterByKey(t *testing.T) {
assert := assert.New(t)
var wiz *Wizard
var c *StandardCluster
wiz = NewWizard()
c = wiz.CreateCluster("country_table", "db-master")
c.RegisterSlave("db-slave")
assert.Equal("db-master", wiz.UseMasterByKey("country_table", 1))
assert.Nil(wiz.UseMasterByKey("city_table", 1), "Non registered name")
var s *ShardCluster
s = wiz.CreateShardCluster("user_table", 997)
s.RegisterShard(0, 499, NewCluster("shard01-master"))
s.RegisterShard(500, 996, NewCluster("shard02-master"))
assert.Equal("shard01-master", wiz.UseMasterByKey("user_table", 499))
assert.Equal("shard02-master", wiz.UseMasterByKey("user_table", 500))
assert.Equal("shard02-master", wiz.UseMasterByKey("user_table", 996))
assert.Equal("shard01-master", wiz.UseMasterByKey("user_table", 997))
}
// TODO: add test for multiple slaves
func TestUseSlaveByKey(t *testing.T) {
assert := assert.New(t)
var wiz *Wizard
var c *StandardCluster
wiz = NewWizard()
c = wiz.CreateCluster("country_table", "db-master")
c.RegisterSlave("db-slave")
assert.Equal("db-master", wiz.UseMasterByKey("country_table", 1))
assert.Nil(wiz.UseMasterByKey("city_table", 1), "Non registered name")
var s *ShardCluster
s = wiz.CreateShardCluster("user_table", 997)
c1 := NewCluster("shard01-master")
c1.RegisterSlave("shard01-slave")
c2 := NewCluster("shard02-master")
c2.RegisterSlave("shard02-slave")
s.RegisterShard(0, 499, c1)
s.RegisterShard(500, 996, c2)
assert.Equal("shard01-slave", wiz.UseSlaveByKey("user_table", 499))
assert.Equal("shard02-slave", wiz.UseSlaveByKey("user_table", 500))
assert.Equal("shard02-slave", wiz.UseSlaveByKey("user_table", 996))
assert.Equal("shard01-slave", wiz.UseSlaveByKey("user_table", 997))
}