forked from evalphobia/wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard_cluster_test.go
124 lines (95 loc) · 2.49 KB
/
standard_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
package wizard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCluster(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
c = NewCluster("db")
assert.IsType(Node{}, *c.master)
assert.Equal("db", c.master.db, "db should be saved on NewCluster()")
}
func TestStandardClusterMaster(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
var node *Node
c = NewCluster("db")
node = c.Master()
assert.Equal("db", node.db)
assert.Equal(c.master, node, "Master() shoud equal to StandardCluster.master")
c.RegisterMaster("db2")
node = c.Master()
assert.Equal("db2", node.db)
}
func TestStandardClusterMasters(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
var nodes []*Node
c = NewCluster("db")
nodes = c.Masters()
assert.Equal(c.master, nodes[0])
assert.Len(nodes, 1)
}
func TestStandardClusterSlave(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
var node *Node
c = NewCluster("master")
node = c.Slave()
assert.IsType(Node{}, *node)
assert.Equal("master", node.db)
assert.Equal(c.master, node, "Slave() shoud equal to StandardCluster.master when no slaves")
assert.Len(c.slaves, 0)
c.RegisterSlave("slave")
node = c.Slave()
assert.IsType(Node{}, *node)
assert.Equal("slave", node.db)
assert.Equal(c.slaves[0], node, "Slave() shoud equal to node in StandardCluster.slaves")
assert.Len(c.slaves, 1)
for i, max := 0, 100; i < max; i++ {
c.RegisterSlave(i)
}
assert.Len(c.slaves, 101)
node = c.Slave()
db := node.db
for i, max := 0, 10; i < max; i++ {
node = c.Slave()
if node.db != db {
return
}
}
t.Error("Slave() should return different nodes")
}
func TestStandardClusterSelectByKey(t *testing.T) {
assert := assert.New(t)
var c, c2, c3, c4 *StandardCluster
c = NewCluster("db")
c2 = c.SelectByKey(0)
c3 = c.SelectByKey(1)
c4 = c.SelectByKey(9999)
assert.Equal(c, c2)
assert.Equal(c, c3)
assert.Equal(c, c4)
}
func TestStandardClusterRegisterMaster(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
c = NewCluster("db")
c.RegisterMaster("db2")
c.RegisterMaster("db3")
assert.Equal("db3", c.master.db)
c.RegisterMaster("db4")
assert.Equal("db4", c.master.db)
}
func TestStandardClusterRegisterSlave(t *testing.T) {
assert := assert.New(t)
var c *StandardCluster
c = NewCluster("db")
c.RegisterSlave("db2")
c.RegisterSlave("db3")
c.RegisterSlave("db4")
assert.Equal("db2", c.slaves[0].db)
assert.Equal("db3", c.slaves[1].db)
assert.Equal("db4", c.slaves[2].db)
}