|
| 1 | +package pika_integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/bsm/ginkgo/v2" |
| 9 | + . "github.com/bsm/gomega" |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = Describe("should replication rename", func() { |
| 14 | + Describe("all replication rename test", func() { |
| 15 | + ctx := context.TODO() |
| 16 | + var clientSlave *redis.Client |
| 17 | + var clientMaster *redis.Client |
| 18 | + |
| 19 | + BeforeEach(func() { |
| 20 | + clientMaster = redis.NewClient(PikaOption(MASTERRENAMEADDR)) |
| 21 | + clientSlave = redis.NewClient(PikaOption(SLAVERENAMEADDR)) |
| 22 | + cleanEnv(ctx, clientMaster, clientSlave) |
| 23 | + if GlobalBefore != nil { |
| 24 | + GlobalBefore(ctx, clientMaster) |
| 25 | + GlobalBefore(ctx, clientSlave) |
| 26 | + } |
| 27 | + }) |
| 28 | + AfterEach(func() { |
| 29 | + cleanEnv(ctx, clientMaster, clientSlave) |
| 30 | + Expect(clientSlave.Close()).NotTo(HaveOccurred()) |
| 31 | + Expect(clientMaster.Close()).NotTo(HaveOccurred()) |
| 32 | + log.Println("Replication test case done") |
| 33 | + }) |
| 34 | + |
| 35 | + It("Let The slave become a replica of The master ", func() { |
| 36 | + infoRes := clientSlave.Info(ctx, "replication") |
| 37 | + Expect(infoRes.Err()).NotTo(HaveOccurred()) |
| 38 | + Expect(infoRes.Val()).To(ContainSubstring("role:master")) |
| 39 | + infoRes = clientMaster.Info(ctx, "replication") |
| 40 | + Expect(infoRes.Err()).NotTo(HaveOccurred()) |
| 41 | + Expect(infoRes.Val()).To(ContainSubstring("role:master")) |
| 42 | + Expect(clientSlave.Do(ctx, "slaveof", LOCALHOST, SLAVERENAMEPORT).Err()).To(MatchError("ERR The master ip:port and the slave ip:port are the same")) |
| 43 | + |
| 44 | + var count = 0 |
| 45 | + for { |
| 46 | + res := trySlave(ctx, clientSlave, LOCALHOST, MASTERRENAMEPORT) |
| 47 | + if res { |
| 48 | + break |
| 49 | + } else if count > 4 { |
| 50 | + break |
| 51 | + } else { |
| 52 | + cleanEnv(ctx, clientMaster, clientSlave) |
| 53 | + count++ |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + infoRes = clientSlave.Info(ctx, "replication") |
| 58 | + Expect(infoRes.Err()).NotTo(HaveOccurred()) |
| 59 | + Expect(infoRes.Val()).To(ContainSubstring("master_link_status:up")) |
| 60 | + |
| 61 | + infoRes = clientMaster.Info(ctx, "replication") |
| 62 | + Expect(infoRes.Err()).NotTo(HaveOccurred()) |
| 63 | + Expect(infoRes.Val()).To(ContainSubstring("connected_slaves:1")) |
| 64 | + |
| 65 | + slaveWrite := clientSlave.Set(ctx, "foo", "bar", 0) |
| 66 | + Expect(slaveWrite.Err()).To(MatchError("ERR Server in read-only")) |
| 67 | + log.Println("Replication rename test 1 start") |
| 68 | + set := clientMaster.Set(ctx, "x", "y", 0) |
| 69 | + Expect(set.Err()).NotTo(HaveOccurred()) |
| 70 | + Expect(set.Val()).To(Equal("OK")) |
| 71 | + set1 := clientMaster.Set(ctx, "a", "b", 0) |
| 72 | + Expect(set1.Err()).NotTo(HaveOccurred()) |
| 73 | + Expect(set1.Val()).To(Equal("OK")) |
| 74 | + r1 := clientMaster.Do(ctx, "flushdb") |
| 75 | + Expect(r1.Val()).NotTo(Equal("OK")) |
| 76 | + time.Sleep(3 * time.Second) |
| 77 | + Expect(clientMaster.Do(ctx, "360flushdb").Err()).NotTo(HaveOccurred()) |
| 78 | + Eventually(func() error { |
| 79 | + return clientMaster.Get(ctx, "x").Err() |
| 80 | + }, "60s", "100ms").Should(Equal(redis.Nil)) |
| 81 | + Eventually(func() error { |
| 82 | + return clientSlave.Get(ctx, "x").Err() |
| 83 | + }, "60s", "100ms").Should(Equal(redis.Nil)) |
| 84 | + log.Println("Replication rename test 1 success") |
| 85 | + |
| 86 | + }) |
| 87 | + }) |
| 88 | +}) |
0 commit comments