Skip to content

Commit e083cde

Browse files
committed
Add test for DisableRoutingPolicies option
1 parent 8d40889 commit e083cde

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

osscluster_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,89 @@ var _ = Describe("Command Tips tests", func() {
24612461
}
24622462
})
24632463

2464+
It("should test DisableRoutingPolicies option disables routing policies", func() {
2465+
SkipBeforeRedisVersion(7.9, "The tips are included from Redis 8")
2466+
2467+
// Test 1: With routing policies enabled (default), MGET should work across slots
2468+
testData := map[string]string{
2469+
"disable_routing_key_1111": "value1",
2470+
"disable_routing_key_2222": "value2",
2471+
"disable_routing_key_3333": "value3",
2472+
}
2473+
2474+
// Set keys
2475+
for key, value := range testData {
2476+
result := client.Set(ctx, key, value, 0)
2477+
Expect(result.Err()).NotTo(HaveOccurred())
2478+
}
2479+
2480+
// Verify keys are on different shards
2481+
type masterNode struct {
2482+
client *redis.Client
2483+
addr string
2484+
}
2485+
var masterNodes []masterNode
2486+
var mu sync.Mutex
2487+
2488+
err := client.ForEachMaster(ctx, func(ctx context.Context, master *redis.Client) error {
2489+
addr := master.Options().Addr
2490+
mu.Lock()
2491+
masterNodes = append(masterNodes, masterNode{client: master, addr: addr})
2492+
mu.Unlock()
2493+
return nil
2494+
})
2495+
Expect(err).NotTo(HaveOccurred())
2496+
2497+
keyLocations := make(map[string]string)
2498+
for key, value := range testData {
2499+
for _, node := range masterNodes {
2500+
getResult := node.client.Get(ctx, key)
2501+
if getResult.Err() == nil && getResult.Val() == value {
2502+
keyLocations[key] = node.addr
2503+
break
2504+
}
2505+
}
2506+
}
2507+
2508+
shardsUsed := make(map[string]bool)
2509+
for _, shardAddr := range keyLocations {
2510+
shardsUsed[shardAddr] = true
2511+
}
2512+
Expect(len(shardsUsed)).To(BeNumerically(">", 1))
2513+
2514+
keys := make([]string, 0, len(testData))
2515+
expectedValues := make([]interface{}, 0, len(testData))
2516+
for key, value := range testData {
2517+
keys = append(keys, key)
2518+
expectedValues = append(expectedValues, value)
2519+
}
2520+
2521+
// With routing policies enabled, MGET should work
2522+
mgetResult := client.MGet(ctx, keys...)
2523+
Expect(mgetResult.Err()).NotTo(HaveOccurred())
2524+
2525+
actualValues := mgetResult.Val()
2526+
Expect(len(actualValues)).To(Equal(len(expectedValues)))
2527+
for i, value := range actualValues {
2528+
if value != nil {
2529+
Expect(value).To(Equal(expectedValues[i]))
2530+
} else {
2531+
Expect(value).To(BeNil())
2532+
}
2533+
}
2534+
2535+
// Test 2: With routing policies disabled, MGET should fail with CROSSSLOT error
2536+
opt := redisClusterOptions()
2537+
opt.DisableRoutingPolicies = true
2538+
clientWithoutPolicies := cluster.newClusterClient(ctx, opt)
2539+
defer clientWithoutPolicies.Close()
2540+
2541+
// Try MGET with routing policies disabled - should fail with CROSSSLOT error
2542+
mgetResultDisabled := clientWithoutPolicies.MGet(ctx, keys...)
2543+
Expect(mgetResultDisabled.Err()).To(HaveOccurred())
2544+
Expect(mgetResultDisabled.Err().Error()).To(ContainSubstring("CROSSSLOT"))
2545+
})
2546+
24642547
It("should route keyless commands to arbitrary shards using round robin", func() {
24652548
SkipBeforeRedisVersion(7.9, "The tips are included from Redis 8")
24662549

0 commit comments

Comments
 (0)