forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsistent_hashing_test.go
221 lines (201 loc) · 4.47 KB
/
consistent_hashing_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package loadbalancingexporter
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewHashRing(t *testing.T) {
// prepare
endpoints := []string{"endpoint-1", "endpoint-2"}
// test
ring := newHashRing(endpoints)
// verify
assert.Len(t, ring.items, 2*defaultWeight)
}
func TestEndpointFor(t *testing.T) {
// prepare
endpoints := []string{"endpoint-1", "endpoint-2"}
ring := newHashRing(endpoints)
for _, tt := range []struct {
id []byte
expected string
}{
// check that we are indeed alternating endpoints for different inputs
{[]byte{1, 2, 0, 0}, "endpoint-1"},
{[]byte{128, 128, 0, 0}, "endpoint-2"},
{[]byte("ad-service-7"), "endpoint-1"},
{[]byte("get-recommendations-1"), "endpoint-2"},
} {
t.Run(fmt.Sprintf("Endpoint for id %s", string(tt.id)), func(t *testing.T) {
// test
endpoint := ring.endpointFor(tt.id)
// verify
assert.Equal(t, tt.expected, endpoint)
})
}
}
func TestPositionsFor(t *testing.T) {
// prepare
endpoint := "host1"
// test
positions := positionsFor(endpoint, 10)
// verify
assert.Len(t, positions, 10)
}
func TestBinarySearch(t *testing.T) {
// prepare
items := []ringItem{
{pos: 14},
{pos: 25},
{pos: 33},
{pos: 47},
{pos: 56},
{pos: 121},
{pos: 134},
{pos: 158},
{pos: 240},
{pos: 270},
{pos: 350},
}
ringSize := len(items)
left, right := items[:ringSize/2], items[ringSize/2:]
for _, tt := range []struct {
requested position
expected position
}{
{position(85), position(121)},
{position(14), position(14)},
{position(351), position(14)},
{position(270), position(270)},
{position(271), position(350)},
} {
t.Run(fmt.Sprintf("Angle %d Requested", uint32(tt.requested)), func(t *testing.T) {
// test
found := bsearch(tt.requested, left, right)
// verify
assert.Equal(t, tt.expected, found.pos)
})
}
}
func TestPositionsForEndpoints(t *testing.T) {
for _, tt := range []struct {
name string
endpoints []string
expected []ringItem
}{
{
"Single Endpoint",
[]string{"endpoint-1"},
[]ringItem{
// this was first calculated by running the algorithm and taking its output
{pos: 1401, endpoint: "endpoint-1"},
{pos: 4175, endpoint: "endpoint-1"},
{pos: 14133, endpoint: "endpoint-1"},
{pos: 17836, endpoint: "endpoint-1"},
{pos: 21667, endpoint: "endpoint-1"},
},
},
{
"Duplicate Endpoint",
[]string{"endpoint-1", "endpoint-1"},
[]ringItem{
// we expect to not have duplicate items
{pos: 1401, endpoint: "endpoint-1"},
{pos: 4175, endpoint: "endpoint-1"},
{pos: 14133, endpoint: "endpoint-1"},
{pos: 17836, endpoint: "endpoint-1"},
{pos: 21667, endpoint: "endpoint-1"},
},
},
{
"Multiple Endpoints",
[]string{"endpoint-1", "endpoint-2"},
[]ringItem{
// we expect to have 5 positions for each endpoint
{pos: 1401, endpoint: "endpoint-1"},
{pos: 4175, endpoint: "endpoint-1"},
{pos: 10240, endpoint: "endpoint-2"},
{pos: 14133, endpoint: "endpoint-1"},
{pos: 15002, endpoint: "endpoint-2"},
{pos: 17836, endpoint: "endpoint-1"},
{pos: 21263, endpoint: "endpoint-2"},
{pos: 21667, endpoint: "endpoint-1"},
{pos: 26806, endpoint: "endpoint-2"},
{pos: 27020, endpoint: "endpoint-2"},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
// test
items := positionsForEndpoints(tt.endpoints, 5)
// verify
assert.Equal(t, tt.expected, items)
})
}
}
func TestEqual(t *testing.T) {
original := &hashRing{
[]ringItem{
{pos: position(123), endpoint: "endpoint-1"},
},
}
for _, tt := range []struct {
name string
candidate *hashRing
outcome bool
}{
{
"empty",
&hashRing{[]ringItem{}},
false,
},
{
"null",
nil,
false,
},
{
"equal",
&hashRing{
[]ringItem{
{pos: position(123), endpoint: "endpoint-1"},
},
},
true,
},
{
"different length",
&hashRing{
[]ringItem{
{pos: position(123), endpoint: "endpoint-1"},
{pos: position(124), endpoint: "endpoint-2"},
},
},
false,
},
{
"different position",
&hashRing{
[]ringItem{
{pos: position(124), endpoint: "endpoint-1"},
},
},
false,
},
{
"different endpoint",
&hashRing{
[]ringItem{
{pos: position(123), endpoint: "endpoint-2"},
},
},
false,
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.outcome, original.equal(tt.candidate))
})
}
}