Add support for BZMPOP (#2456)

* Add support for BZMPOP

* Add BZMPOP command comment
This commit is contained in:
YuGan 2023-02-23 19:25:41 +08:00 committed by GitHub
parent 621c02c583
commit 6045d2e456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 163 additions and 17 deletions

View File

@ -299,6 +299,7 @@ type Cmdable interface {
BZPopMax(ctx context.Context, timeout time.Duration, keys ...string) *ZWithKeyCmd
BZPopMin(ctx context.Context, timeout time.Duration, keys ...string) *ZWithKeyCmd
BZMPop(ctx context.Context, timeout time.Duration, order string, count int64, keys ...string) *ZSliceWithKeyCmd
ZAdd(ctx context.Context, key string, members ...Z) *IntCmd
ZAddNX(ctx context.Context, key string, members ...Z) *IntCmd
@ -2328,6 +2329,26 @@ func (c cmdable) BZPopMin(ctx context.Context, timeout time.Duration, keys ...st
return cmd
}
// BZMPop is the blocking variant of ZMPOP.
// When any of the sorted sets contains elements, this command behaves exactly like ZMPOP.
// When all sorted sets are empty, Redis will block the connection until another client adds members to one of the keys or until the timeout elapses.
// A timeout of zero can be used to block indefinitely.
// example: client.BZMPop(ctx, 0,"max", 1, "set")
func (c cmdable) BZMPop(ctx context.Context, timeout time.Duration, order string, count int64, keys ...string) *ZSliceWithKeyCmd {
args := make([]interface{}, 3+len(keys), 6+len(keys))
args[0] = "bzmpop"
args[1] = formatSec(ctx, timeout)
args[2] = len(keys)
for i, key := range keys {
args[3+i] = key
}
args = append(args, strings.ToLower(order), "count", count)
cmd := NewZSliceWithKeyCmd(ctx, args...)
cmd.setReadTimeout(timeout)
_ = c(ctx, cmd)
return cmd
}
// ZAddArgs WARN: The GT, LT and NX options are mutually exclusive.
type ZAddArgs struct {
NX bool

View File

@ -3772,7 +3772,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("zset"))
Expect(elems).To(Equal([]redis.Z{{
Score: 1,
Score: 1,
Member: "one",
}}))
@ -3790,22 +3790,21 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("myzset"))
Expect(elems).To(Equal([]redis.Z{{
Score:1,
Member:"one",
Score: 1,
Member: "one",
}}))
key, elems, err = client.ZMPop(ctx, "max", 10, "myzset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("myzset"))
Expect(elems).To(Equal([]redis.Z{{
Score:3,
Member:"three",
},{
Score: 2,
Score: 3,
Member: "three",
}, {
Score: 2,
Member: "two",
}}))
err = client.ZAdd(ctx, "myzset2", redis.Z{Score: 4, Member: "four"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "myzset2", redis.Z{Score: 5, Member: "five"}).Err()
@ -3813,22 +3812,148 @@ var _ = Describe("Commands", func() {
err = client.ZAdd(ctx, "myzset2", redis.Z{Score: 6, Member: "six"}).Err()
Expect(err).NotTo(HaveOccurred())
key, elems, err = client.ZMPop(ctx, "min", 10, "myzset","myzset2").Result()
key, elems, err = client.ZMPop(ctx, "min", 10, "myzset", "myzset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("myzset2"))
Expect(elems).To(Equal([]redis.Z{{
Score:4,
Member:"four",
},{
Score: 5,
Score: 4,
Member: "four",
}, {
Score: 5,
Member: "five",
},{
Score:6,
}, {
Score: 6,
Member: "six",
}}))
})
It("should BZMPop", func() {
err := client.ZAdd(ctx, "zset", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
key, elems, err := client.BZMPop(ctx, 0, "min", 1, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("zset"))
Expect(elems).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}}))
key, elems, err = client.BZMPop(ctx, 0, "max", 1, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("zset"))
Expect(elems).To(Equal([]redis.Z{{
Score: 3,
Member: "three",
}}))
key, elems, err = client.BZMPop(ctx, 0, "min", 10, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("zset"))
Expect(elems).To(Equal([]redis.Z{{
Score: 2,
Member: "two",
}}))
key, elems, err = client.BZMPop(ctx, 0, "max", 10, "zset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("zset2"))
Expect(elems).To(Equal([]redis.Z{{
Score: 3,
Member: "three",
}, {
Score: 2,
Member: "two",
}, {
Score: 1,
Member: "one",
}}))
err = client.ZAdd(ctx, "myzset", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
key, elems, err = client.BZMPop(ctx, 0, "min", 10, "myzset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("myzset"))
Expect(elems).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}}))
err = client.ZAdd(ctx, "myzset2", redis.Z{Score: 4, Member: "four"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "myzset2", redis.Z{Score: 5, Member: "five"}).Err()
Expect(err).NotTo(HaveOccurred())
key, elems, err = client.BZMPop(ctx, 0, "min", 10, "myzset", "myzset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("myzset2"))
Expect(elems).To(Equal([]redis.Z{{
Score: 4,
Member: "four",
}, {
Score: 5,
Member: "five",
}}))
})
It("should BZMPopBlocks", func() {
started := make(chan bool)
done := make(chan bool)
go func() {
defer GinkgoRecover()
started <- true
key, elems, err := client.BZMPop(ctx, 0, "min", 1, "list_list").Result()
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("list_list"))
Expect(elems).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}}))
done <- true
}()
<-started
select {
case <-done:
Fail("BZMPop is not blocked")
case <-time.After(time.Second):
//ok
}
err := client.ZAdd(ctx, "list_list", redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
select {
case <-done:
//ok
case <-time.After(time.Second):
Fail("BZMPop is still blocked")
}
})
It("should BZMPop timeout", func() {
_, val, err := client.BZMPop(ctx, time.Second, "min", 1, "list1").Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(BeNil())
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
stats := client.PoolStats()
Expect(stats.Hits).To(Equal(uint32(2)))
Expect(stats.Misses).To(Equal(uint32(1)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
})
It("should ZMScore", func() {