Skip to content

Commit 7daf563

Browse files
committed
Add unit tests for pkg/sharding/consistenthash
1 parent 608b03d commit 7daf563

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package consistenthash_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestConsistentHash(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Consistent Hash Suite")
29+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package consistenthash_test
18+
19+
import (
20+
"strings"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
25+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/consistenthash"
26+
)
27+
28+
var _ = Describe("Ring", func() {
29+
Describe("#New", func() {
30+
It("should initialize a new Ring", func() {
31+
ring := New(nil, 0, "foo")
32+
Expect(ring).NotTo(BeNil())
33+
Expect(ring.IsEmpty()).To(BeFalse())
34+
})
35+
})
36+
37+
Describe("#IsEmpty", func() {
38+
It("should true if there are no nodes", func() {
39+
ring := New(nil, 0)
40+
Expect(ring.IsEmpty()).To(BeTrue())
41+
ring.AddNodes("foo")
42+
Expect(ring.IsEmpty()).To(BeFalse())
43+
})
44+
})
45+
46+
Describe("#Hash", func() {
47+
It("should use the configured hash function", func() {
48+
ring := New(func(data string) uint64 {
49+
if strings.HasPrefix(data, "foo") {
50+
// map all foo* nodes and keys to 1
51+
return 1
52+
}
53+
return 2
54+
}, 1, "foo", "bar")
55+
56+
Expect(ring.Hash("foo")).To(Equal("foo"))
57+
Expect(ring.Hash("bar")).To(Equal("bar"))
58+
Expect(ring.Hash("baz")).To(Equal("bar"))
59+
})
60+
61+
It("should use the default hash function", func() {
62+
ring := New(nil, 0, "foo", "bar")
63+
64+
Expect(ring.Hash("1")).NotTo(Equal(ring.Hash("10")))
65+
})
66+
67+
It("should return the empty string if there are no nodes", func() {
68+
ring := New(nil, 0)
69+
70+
Expect(ring.Hash("foo")).To(BeEmpty())
71+
})
72+
73+
It("should return the first node when walking the whole ring", func() {
74+
ring := New(func(data string) uint64 {
75+
if strings.HasPrefix(data, "foo") {
76+
// map all foo* nodes and keys to 1
77+
return 1
78+
}
79+
if strings.HasPrefix(data, "bar") {
80+
// map all bar* nodes and keys to 1
81+
return 2
82+
}
83+
return 3
84+
}, 1, "foo", "bar")
85+
86+
Expect(ring.Hash("foo")).To(Equal("foo"))
87+
Expect(ring.Hash("bar")).To(Equal("bar"))
88+
Expect(ring.Hash("baz")).To(Equal("foo"))
89+
})
90+
})
91+
})

0 commit comments

Comments
 (0)