From 8fadbef84a3f4e7573f8b38e5023fd469470a8a4 Mon Sep 17 00:00:00 2001 From: milad Date: Thu, 6 Mar 2025 18:23:14 +0330 Subject: [PATCH] add readOnly on failover opts (#3281) * add readOnly on failover opts add failover that if is True it connects to slaves * add test * add test for slave connect * fix test * fix tests * skip Flaky * add more tests on slave * add test * delete file * Update universal_test.go enable previously skipped test. * rm skip from sentinels * rm tmp files * Update universal_test.go don't run sentinel test in RE * Update universal_test.go --------- Co-authored-by: Nedyalko Dyakov --- universal.go | 2 ++ universal_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/universal.go b/universal.go index 21867ec2..3e3367e3 100644 --- a/universal.go +++ b/universal.go @@ -163,6 +163,8 @@ func (o *UniversalOptions) Failover() *FailoverOptions { TLSConfig: o.TLSConfig, + ReplicaOnly: o.ReadOnly, + DisableIndentity: o.DisableIndentity, IdentitySuffix: o.IdentitySuffix, UnstableResp3: o.UnstableResp3, diff --git a/universal_test.go b/universal_test.go index 2a1fac39..e389fe4f 100644 --- a/universal_test.go +++ b/universal_test.go @@ -60,6 +60,25 @@ var _ = Describe("UniversalClient", func() { Expect(a).ToNot(Panic()) }) + It("should connect to failover servers on slaves when readonly Options is ok", Label("NonRedisEnterprise"), func() { + client = redis.NewUniversalClient(&redis.UniversalOptions{ + MasterName: sentinelName, + Addrs: sentinelAddrs, + ReadOnly: true, + }) + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + + roleCmd := client.Do(ctx, "ROLE") + role, err := roleCmd.Result() + Expect(err).NotTo(HaveOccurred()) + + roleSlice, ok := role.([]interface{}) + Expect(ok).To(BeTrue()) + Expect(roleSlice[0]).To(Equal("slave")) + + err = client.Set(ctx, "somekey", "somevalue", 0).Err() + Expect(err).To(HaveOccurred()) + }) It("should connect to clusters if IsClusterMode is set even if only a single address is provided", Label("NonRedisEnterprise"), func() { client = redis.NewUniversalClient(&redis.UniversalOptions{ Addrs: []string{cluster.addrs()[0]}, @@ -77,3 +96,4 @@ var _ = Describe("UniversalClient", func() { Expect(client.ClusterSlots(ctx).Val()).To(HaveLen(3)) }) }) +