@@ -783,7 +783,7 @@ var _ = Describe("Ring Tx timeout", func() {
783
783
})
784
784
})
785
785
786
- var _ = Describe ("Ring GetShards and GetShardByKey " , func () {
786
+ var _ = Describe ("Ring GetShardClients and GetShardClientForKey " , func () {
787
787
var ring * redis.Ring
788
788
789
789
BeforeEach (func () {
@@ -799,8 +799,12 @@ var _ = Describe("Ring GetShards and GetShardByKey", func() {
799
799
Expect (ring .Close ()).NotTo (HaveOccurred ())
800
800
})
801
801
802
- It ("GetShards returns active shard clients" , func () {
803
- shards := ring .GetShards ()
802
+ It ("GetShardClients returns active shard clients" , func () {
803
+ shards := ring .GetShardClients ()
804
+ // Note: This test will pass even if Redis servers are not running,
805
+ // because GetShardClients only returns clients that are marked as "up",
806
+ // and newly created shards start as "up" until the first health check fails.
807
+
804
808
if len (shards ) == 0 {
805
809
// Expected if Redis servers are not running
806
810
Skip ("No active shards found (Redis servers not running)" )
@@ -812,22 +816,24 @@ var _ = Describe("Ring GetShards and GetShardByKey", func() {
812
816
}
813
817
})
814
818
815
- It ("GetShardByKey returns correct shard for keys" , func () {
819
+ It ("GetShardClientForKey returns correct shard for keys" , func () {
816
820
testKeys := []string {"key1" , "key2" , "user:123" , "channel:test" }
817
821
818
822
for _ , key := range testKeys {
819
- client , err := ring .GetShardByKey (key )
823
+ client , err := ring .GetShardClientForKey (key )
820
824
Expect (err ).NotTo (HaveOccurred ())
821
825
Expect (client ).NotTo (BeNil ())
822
826
}
823
827
})
824
828
825
- It ("GetShardByKey is consistent for same key" , func () {
829
+ It ("GetShardClientForKey is consistent for same key" , func () {
826
830
key := "test:consistency"
827
831
832
+ // Call GetShardClientForKey multiple times with the same key
833
+ // Should always return the same shard
828
834
var firstClient * redis.Client
829
835
for i := 0 ; i < 5 ; i ++ {
830
- client , err := ring .GetShardByKey (key )
836
+ client , err := ring .GetShardClientForKey (key )
831
837
Expect (err ).NotTo (HaveOccurred ())
832
838
Expect (client ).NotTo (BeNil ())
833
839
@@ -839,17 +845,19 @@ var _ = Describe("Ring GetShards and GetShardByKey", func() {
839
845
}
840
846
})
841
847
842
- It ("GetShardByKey distributes keys across shards" , func () {
848
+ It ("GetShardClientForKey distributes keys across shards" , func () {
843
849
testKeys := []string {"key1" , "key2" , "key3" , "key4" , "key5" }
844
850
shardMap := make (map [string ]int )
845
851
846
852
for _ , key := range testKeys {
847
- client , err := ring .GetShardByKey (key )
853
+ client , err := ring .GetShardClientForKey (key )
848
854
Expect (err ).NotTo (HaveOccurred ())
849
855
shardMap [client .String ()]++
850
856
}
851
857
858
+ // Should have at least 1 shard (could be all keys go to same shard due to hashing)
852
859
Expect (len (shardMap )).To (BeNumerically (">=" , 1 ))
860
+ // But with multiple keys, we expect some distribution
853
861
Expect (len (shardMap )).To (BeNumerically ("<=" , 2 )) // At most 2 shards (our setup)
854
862
})
855
863
})
0 commit comments