forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 59
/
control_ccm_test.go
179 lines (151 loc) · 3.9 KB
/
control_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
167
168
169
170
171
172
173
174
175
176
177
178
179
//go:build ccm
// +build ccm
package gocql
/* TODO: Fix this test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/gocql/gocql/internal/ccm"
)
type TestHostFilter struct {
mu sync.Mutex
allowedHosts map[string]ccm.Host
}
func (f *TestHostFilter) Accept(h *HostInfo) bool {
f.mu.Lock()
defer f.mu.Unlock()
_, ok := f.allowedHosts[h.ConnectAddress().String()]
return ok
}
func (f *TestHostFilter) SetAllowedHosts(hosts map[string]ccm.Host) {
f.mu.Lock()
defer f.mu.Unlock()
f.allowedHosts = hosts
}
func TestControlConn_ReconnectRefreshesRing(t *testing.T) {
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
allCcmHosts, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
if len(allCcmHosts) < 2 {
t.Skip("this test requires at least 2 nodes")
}
allAllowedHosts := map[string]ccm.Host{}
var firstNode *ccm.Host
for _, node := range allCcmHosts {
if firstNode == nil {
firstNode = &node
}
allAllowedHosts[node.Addr] = node
}
allowedHosts := map[string]ccm.Host{
firstNode.Addr: *firstNode,
}
testFilter := &TestHostFilter{allowedHosts: allowedHosts}
session := createSession(t, func(config *ClusterConfig) {
config.Hosts = []string{firstNode.Addr}
config.Events.DisableTopologyEvents = true
config.Events.DisableNodeStatusEvents = true
config.HostFilter = testFilter
})
defer session.Close()
if session.control == nil || session.control.conn.Load() == nil {
t.Fatal("control conn is nil")
}
controlConnection := session.control.getConn()
ccHost := controlConnection.host
var ccHostName string
for _, node := range allCcmHosts {
if node.Addr == ccHost.ConnectAddress().String() {
ccHostName = node.Name
break
}
}
if ccHostName == "" {
t.Fatal("could not find name of control host")
}
if err := ccm.NodeDown(ccHostName); err != nil {
t.Fatal()
}
defer func() {
ccmStatus, err := ccm.Status()
if err != nil {
t.Logf("could not bring nodes back up after test: %v", err)
return
}
for _, node := range ccmStatus {
if node.State == ccm.NodeStateDown {
err = ccm.NodeUp(node.Name)
if err != nil {
t.Logf("could not bring node %v back up after test: %v", node.Name, err)
}
}
}
}()
assertNodeDown := func() error {
hosts := session.hostSource.getHostsMap()
if len(hosts) != 1 {
return fmt.Errorf("expected 1 host in ring but there were %v", len(hosts))
}
for _, host := range hosts {
if host.IsUp() {
return fmt.Errorf("expected host to be DOWN but %v isn't", host.String())
}
}
session.pool.mu.RLock()
poolsLen := len(session.pool.hostConnPools)
session.pool.mu.RUnlock()
if poolsLen != 0 {
return fmt.Errorf("expected 0 connection pool but there were %v", poolsLen)
}
return nil
}
maxAttempts := 5
delayPerAttempt := 1 * time.Second
assertErr := assertNodeDown()
for i := 0; i < maxAttempts && assertErr != nil; i++ {
time.Sleep(delayPerAttempt)
assertErr = assertNodeDown()
}
if assertErr != nil {
t.Fatal(err)
}
testFilter.SetAllowedHosts(allAllowedHosts)
if err = ccm.NodeUp(ccHostName); err != nil {
t.Fatal(err)
}
assertNodeUp := func() error {
hosts := session.hostSource.getHostsMap()
if len(hosts) != len(allCcmHosts) {
return fmt.Errorf("expected %v hosts in ring but there were %v", len(allCcmHosts), len(hosts))
}
for _, host := range hosts {
if !host.IsUp() {
return fmt.Errorf("expected all hosts to be UP but %v isn't", host.String())
}
}
session.pool.mu.RLock()
poolsLen := len(session.pool.hostConnPools)
session.pool.mu.RUnlock()
if poolsLen != len(allCcmHosts) {
return fmt.Errorf("expected %v connection pool but there were %v", len(allCcmHosts), poolsLen)
}
return nil
}
maxAttempts = 30
delayPerAttempt = 1 * time.Second
assertErr = assertNodeUp()
for i := 0; i < maxAttempts && assertErr != nil; i++ {
time.Sleep(delayPerAttempt)
assertErr = assertNodeUp()
}
if assertErr != nil {
t.Fatal(err)
}
}
*/