Skip to content

Commit 9ffb47e

Browse files
committed
fix(tests): some tests relied on client internal field access
timeout tests now use reflect and unsafe to access clients internal options
1 parent d986ff7 commit 9ffb47e

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

acl_commands_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ var _ = Describe("ACL", func() {
2525
err := client.Do(ctx, "acl", "setuser", "test", ">test", "on", "allkeys", "+get").Err()
2626
Expect(err).NotTo(HaveOccurred())
2727

28-
clientAcl := redis.NewClient(redisOptions())
29-
clientAcl.Options().Username = "test"
30-
clientAcl.Options().Password = "test"
31-
clientAcl.Options().DB = 0
28+
opt := redisOptions()
29+
opt.Username = "test"
30+
opt.Password = "test"
31+
opt.DB = 0
32+
clientAcl := redis.NewClient(opt)
3233
_ = clientAcl.Set(ctx, "mystring", "foo", 0).Err()
3334
_ = clientAcl.HSet(ctx, "myhash", "foo", "bar").Err()
3435
_ = clientAcl.SAdd(ctx, "myset", "foo", "bar").Err()

osscluster_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"errors"
77
"fmt"
88
"net"
9+
"reflect"
910
"slices"
1011
"strconv"
1112
"strings"
1213
"sync"
1314
"time"
15+
"unsafe"
1416

1517
. "github.com/bsm/ginkgo/v2"
1618
. "github.com/bsm/gomega"
@@ -256,6 +258,10 @@ func slotEqual(s1, s2 redis.ClusterSlot) bool {
256258
return true
257259
}
258260

261+
func getField(field reflect.Value) any {
262+
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
263+
}
264+
259265
// ------------------------------------------------------------------------------
260266

261267
var _ = Describe("ClusterClient", func() {
@@ -1631,11 +1637,12 @@ var _ = Describe("ClusterClient timeout", func() {
16311637
BeforeEach(func() {
16321638
opt := redisClusterOptions()
16331639
client = cluster.newClusterClient(ctx, opt)
1640+
opt = getField(reflect.ValueOf(client).Elem().FieldByName("opt")).(*redis.ClusterOptions)
16341641

16351642
err := client.ForEachShard(ctx, func(ctx context.Context, client *redis.Client) error {
16361643
err := client.ClientPause(ctx, pause).Err()
16371644

1638-
opt := client.Options()
1645+
opt := getField(reflect.ValueOf(client).Elem().FieldByName("opt")).(*redis.Options)
16391646
opt.ReadTimeout = time.Nanosecond
16401647
opt.WriteTimeout = time.Nanosecond
16411648

@@ -1653,7 +1660,7 @@ var _ = Describe("ClusterClient timeout", func() {
16531660
_ = client.ForEachShard(ctx, func(ctx context.Context, client *redis.Client) error {
16541661
defer GinkgoRecover()
16551662

1656-
opt := client.Options()
1663+
opt := getField(reflect.ValueOf(client).Elem().FieldByName("opt")).(*redis.Options)
16571664
opt.ReadTimeout = time.Second
16581665
opt.WriteTimeout = time.Second
16591666

@@ -3188,6 +3195,7 @@ var _ = Describe("ClusterClient FailingTimeoutSeconds", func() {
31883195
It("should use default failing timeout of 15 seconds", func() {
31893196
opt := redisClusterOptions()
31903197
client = cluster.newClusterClient(ctx, opt)
3198+
opt = client.Options()
31913199

31923200
// Default should be 15 seconds
31933201
Expect(opt.FailingTimeoutSeconds).To(Equal(15))
@@ -3197,6 +3205,7 @@ var _ = Describe("ClusterClient FailingTimeoutSeconds", func() {
31973205
opt := redisClusterOptions()
31983206
opt.FailingTimeoutSeconds = 30
31993207
client = cluster.newClusterClient(ctx, opt)
3208+
opt = client.Options()
32003209

32013210
// Should use custom value
32023211
Expect(opt.FailingTimeoutSeconds).To(Equal(30))
@@ -3241,6 +3250,7 @@ var _ = Describe("ClusterClient FailingTimeoutSeconds", func() {
32413250
opt := redisClusterOptions()
32423251
opt.FailingTimeoutSeconds = 0 // Should use default
32433252
client = cluster.newClusterClient(ctx, opt)
3253+
opt = client.Options()
32443254

32453255
// After initialization, should be set to default
32463256
Expect(opt.FailingTimeoutSeconds).To(Equal(15))

0 commit comments

Comments
 (0)