forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scylla_test.go
186 lines (158 loc) · 3.63 KB
/
scylla_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
package gocql
import (
"math"
"runtime"
"sync"
"testing"
)
func TestScyllaConnPickerPickNilToken(t *testing.T) {
t.Parallel()
s := scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
}
t.Run("no conns", func(t *testing.T) {
s.conns = []*Conn{{}}
if s.Pick(token(nil)) != s.conns[0] {
t.Fatal("expected connection")
}
})
t.Run("one shard", func(t *testing.T) {
s.conns = []*Conn{{}}
if s.Pick(token(nil)) != s.conns[0] {
t.Fatal("expected connection")
}
})
t.Run("multiple shards", func(t *testing.T) {
s.conns = []*Conn{nil, {}}
if s.Pick(token(nil)) != s.conns[1] {
t.Fatal("expected connection")
}
if s.Pick(token(nil)) != s.conns[1] {
t.Fatal("expected connection")
}
})
t.Run("multiple shards no conns", func(t *testing.T) {
s.conns = []*Conn{nil, nil}
if s.Pick(token(nil)) != nil {
t.Fatal("expected nil")
}
if s.Pick(token(nil)) != nil {
t.Fatal("expected nil")
}
})
}
func hammerConnPicker(t *testing.T, wg *sync.WaitGroup, s *scyllaConnPicker, loops int) {
t.Helper()
for i := 0; i < loops; i++ {
if c := s.Pick(nil); c == nil {
t.Error("unexpected nil")
}
}
wg.Done()
}
func TestScyllaConnPickerHammerPickNilToken(t *testing.T) {
t.Parallel()
s := scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
}
s.conns = make([]*Conn, 100)
for i := range s.conns {
if i%7 == 0 {
continue
}
s.conns[i] = &Conn{}
}
n := runtime.GOMAXPROCS(0)
loops := 10000 / n
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go hammerConnPicker(t, &wg, &s, loops)
}
wg.Wait()
}
func TestScyllaConnPickerRemove(t *testing.T) {
t.Parallel()
s := scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
}
conn := mockConn("0")
s.Put(conn)
s.Put(mockConn("1"))
if s.nrConns != 2 {
t.Error("added 2 connections, expected connection count to be 2")
}
s.Remove(conn)
if s.nrConns != 1 {
t.Errorf("removed 1 connection, expected connection count to be 1 but was %d", s.nrConns)
}
if s.conns[0] != nil {
t.Errorf("Expected %v to be removed from it's position", conn)
}
}
func mockConn(shard string) *Conn {
return &Conn{
supported: map[string][]string{
"SCYLLA_SHARD": {shard},
"SCYLLA_NR_SHARDS": {"4"},
"SCYLLA_SHARDING_IGNORE_MSB": {"12"},
"SCYLLA_PARTITIONER": {"org.apache.cassandra.dht.Murmur3Partitioner"},
"SCYLLA_SHARDING_ALGORITHM": {"biased-token-round-robin"},
},
}
}
func TestScyllaConnPickerShardOf(t *testing.T) {
t.Parallel()
s := scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
}
for _, test := range scyllaShardOfTests {
if shard := s.shardOf(murmur3Token(test.token)); shard != test.shard {
t.Errorf("wrong scylla shard calculated for token %d, expected %d, got %d", test.token, test.shard, shard)
}
}
}
func TestScyllaRandomConnPIcker(t *testing.T) {
t.Parallel()
t.Run("max iterations", func(t *testing.T) {
s := &scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
pos: math.MaxUint64,
conns: []*Conn{nil, mockConn("1")},
}
if s.Pick(token(nil)) == nil {
t.Fatal("expected connection")
}
})
t.Run("async access of max iterations", func(t *testing.T) {
s := &scyllaConnPicker{
nrShards: 4,
msbIgnore: 12,
pos: math.MaxUint64,
conns: []*Conn{nil, mockConn("1")},
}
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go pickLoop(t, s, 3, &wg)
}
wg.Wait()
if s.pos != 8 {
t.Fatalf("expected position to be 8 | actual %d", s.pos)
}
})
}
func pickLoop(t *testing.T, s *scyllaConnPicker, c int, wg *sync.WaitGroup) {
t.Helper()
for i := 0; i < c; i++ {
if s.Pick(token(nil)) == nil {
t.Fatal("expected connection")
}
}
wg.Done()
}