forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_test.go
189 lines (171 loc) · 5.32 KB
/
token_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
187
188
189
// Copyright (c) 2015 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"math/big"
"strconv"
"testing"
)
func TestMurmur3H1(t *testing.T) {
assertMurmur3H1(t, []byte{}, 0x000000000000000)
assertMurmur3H1(t, []byte{0}, 0x4610abe56eff5cb5)
assertMurmur3H1(t, []byte{0, 1}, 0x7cb3f5c58dab264c)
assertMurmur3H1(t, []byte{0, 1, 2}, 0xb872a12fef53e6be)
assertMurmur3H1(t, []byte{0, 1, 2, 3}, 0xe1c594ae0ddfaf10)
assertMurmur3H1(t, []byte("hello"), 0xcbd8a7b341bd9b02)
assertMurmur3H1(t, []byte("hello, world"), 0x342fac623a5ebc8e)
assertMurmur3H1(t, []byte("19 Jan 2038 at 3:14:07 AM"), 0xb89e5988b737affc)
assertMurmur3H1(t, []byte("The quick brown fox jumps over the lazy dog."), 0xcd99481f9ee902c9)
}
func assertMurmur3H1(t *testing.T, data []byte, expected uint64) {
actual := murmur3H1(data)
if actual != expected {
t.Errorf("Expected h1 = %x for data = %x, but was %x", expected, data, actual)
}
}
func BenchmarkMurmur3H1(b *testing.B) {
var h1 uint64
var data [1024]byte
for i := 0; i < 1024; i++ {
data[i] = byte(i)
}
for i := 0; i < b.N; i++ {
b.ResetTimer()
h1 = murmur3H1(data[:])
_ = murmur3Token(int64(h1))
}
}
func TestMurmur3Partitioner(t *testing.T) {
token := murmur3Partitioner{}.ParseString("-1053604476080545076")
if "-1053604476080545076" != token.String() {
t.Errorf("Expected '-1053604476080545076' but was '%s'", token)
}
// at least verify that the partitioner
// doesn't return nil
pk, _ := marshalInt(nil, 1)
token = murmur3Partitioner{}.Hash(pk)
if token == nil {
t.Fatal("token was nil")
}
}
func TestMurmur3Token(t *testing.T) {
if murmur3Token(42).Less(murmur3Token(42)) {
t.Errorf("Expected Less to return false, but was true")
}
if !murmur3Token(-42).Less(murmur3Token(42)) {
t.Errorf("Expected Less to return true, but was false")
}
if murmur3Token(42).Less(murmur3Token(-42)) {
t.Errorf("Expected Less to return false, but was true")
}
}
func TestOrderPreservingPartitioner(t *testing.T) {
// at least verify that the partitioner
// doesn't return nil
pk, _ := marshalInt(nil, 1)
token := orderPreservingPartitioner{}.Hash(pk)
if token == nil {
t.Fatal("token was nil")
}
}
func TestOrderPreservingToken(t *testing.T) {
if orderPreservingToken([]byte{0, 0, 4, 2}).Less(orderPreservingToken([]byte{0, 0, 4, 2})) {
t.Errorf("Expected Less to return false, but was true")
}
if !orderPreservingToken([]byte{0, 0, 3}).Less(orderPreservingToken([]byte{0, 0, 4, 2})) {
t.Errorf("Expected Less to return true, but was false")
}
if orderPreservingToken([]byte{0, 0, 4, 2}).Less(orderPreservingToken([]byte{0, 0, 3})) {
t.Errorf("Expected Less to return false, but was true")
}
}
func TestRandomPartitioner(t *testing.T) {
// at least verify that the partitioner
// doesn't return nil
pk, _ := marshalInt(nil, 1)
token := randomPartitioner{}.Hash(pk)
if token == nil {
t.Fatal("token was nil")
}
}
func TestRandomToken(t *testing.T) {
if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(42))) {
t.Errorf("Expected Less to return false, but was true")
}
if !((*randomToken)(big.NewInt(41))).Less((*randomToken)(big.NewInt(42))) {
t.Errorf("Expected Less to return true, but was false")
}
if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(41))) {
t.Errorf("Expected Less to return false, but was true")
}
}
type intToken int
func (i intToken) String() string {
return strconv.Itoa(int(i))
}
func (i intToken) Less(token token) bool {
return i < token.(intToken)
}
func TestIntTokenRing(t *testing.T) {
// test based on example at the start of this page of documentation:
// http://www.datastax.com/docs/0.8/cluster_architecture/partitioning
host0 := &HostInfo{}
host25 := &HostInfo{}
host50 := &HostInfo{}
host75 := &HostInfo{}
tokenRing := &tokenRing{
partitioner: nil,
tokens: []token{
intToken(0),
intToken(25),
intToken(50),
intToken(75),
},
hosts: []*HostInfo{
host0,
host25,
host50,
host75,
},
}
if tokenRing.GetHostForToken(intToken(0)) != host0 {
t.Error("Expected host 0 for token 0")
}
if tokenRing.GetHostForToken(intToken(1)) != host25 {
t.Error("Expected host 25 for token 1")
}
if tokenRing.GetHostForToken(intToken(24)) != host25 {
t.Error("Expected host 25 for token 24")
}
if tokenRing.GetHostForToken(intToken(25)) != host25 {
t.Error("Expected host 25 for token 25")
}
if tokenRing.GetHostForToken(intToken(26)) != host50 {
t.Error("Expected host 50 for token 26")
}
if tokenRing.GetHostForToken(intToken(49)) != host50 {
t.Error("Expected host 50 for token 49")
}
if tokenRing.GetHostForToken(intToken(50)) != host50 {
t.Error("Expected host 50 for token 50")
}
if tokenRing.GetHostForToken(intToken(51)) != host75 {
t.Error("Expected host 75 for token 51")
}
if tokenRing.GetHostForToken(intToken(74)) != host75 {
t.Error("Expected host 75 for token 74")
}
if tokenRing.GetHostForToken(intToken(75)) != host75 {
t.Error("Expected host 75 for token 75")
}
if tokenRing.GetHostForToken(intToken(76)) != host0 {
t.Error("Expected host 0 for token 76")
}
if tokenRing.GetHostForToken(intToken(99)) != host0 {
t.Error("Expected host 0 for token 99")
}
if tokenRing.GetHostForToken(intToken(100)) != host0 {
t.Error("Expected host 0 for token 100")
}
}