forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_ccm_test.go
166 lines (132 loc) · 3.37 KB
/
events_ccm_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
// +build ccm
package gocql
import (
"github.com/gocql/gocql/ccm_test"
"testing"
"time"
)
func TestEventDiscovery(t *testing.T) {
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
session := createSession(t)
defer session.Close()
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
session.pool.mu.RLock()
poolHosts := session.pool.hostConnPools // TODO: replace with session.ring
t.Logf("poolhosts=%+v\n", poolHosts)
// check we discovered all the nodes in the ring
for _, host := range status {
if _, ok := poolHosts[host.Addr]; !ok {
t.Errorf("did not discover %q", host.Addr)
}
}
session.pool.mu.RUnlock()
if t.Failed() {
t.FailNow()
}
}
func TestEventNodeDownControl(t *testing.T) {
const targetNode = "node1"
t.Log("marking " + targetNode + " as down")
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
session := createSession(t)
defer session.Close()
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
poolHosts := session.pool.hostConnPools
node := status[targetNode]
t.Logf("poolhosts=%+v\n", poolHosts)
if _, ok := poolHosts[node.Addr]; ok {
session.pool.mu.RUnlock()
t.Fatal("node not removed after remove event")
}
session.pool.mu.RUnlock()
}
func TestEventNodeDown(t *testing.T) {
const targetNode = "node3"
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
session := createSession(t)
defer session.Close()
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
defer session.pool.mu.RUnlock()
poolHosts := session.pool.hostConnPools
node := status[targetNode]
t.Logf("poolhosts=%+v\n", poolHosts)
if _, ok := poolHosts[node.Addr]; ok {
t.Fatal("node not removed after remove event")
}
}
func TestEventNodeUp(t *testing.T) {
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
session := createSession(t)
defer session.Close()
poolHosts := session.pool.hostConnPools
const targetNode = "node2"
session.pool.mu.RLock()
_, ok := poolHosts[status[targetNode].Addr]
session.pool.mu.RUnlock()
if !ok {
session.pool.mu.RLock()
t.Errorf("target pool not in connection pool: addr=%q pools=%v", status[targetNode].Addr, poolHosts)
session.pool.mu.RUnlock()
t.FailNow()
}
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
t.Logf("poolhosts=%+v\n", poolHosts)
node := status[targetNode]
if _, ok := poolHosts[node.Addr]; ok {
session.pool.mu.RUnlock()
t.Fatal("node not removed after remove event")
}
session.pool.mu.RUnlock()
if err := ccm.NodeUp(targetNode); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
t.Logf("poolhosts=%+v\n", poolHosts)
if _, ok := poolHosts[node.Addr]; !ok {
session.pool.mu.RUnlock()
t.Fatal("node not added after node added event")
}
session.pool.mu.RUnlock()
}