Skip to content

Commit

Permalink
Add support for BZMPOP (#2456)
Browse files Browse the repository at this point in the history
* Add support for BZMPOP

* Add BZMPOP command comment
  • Loading branch information
yikuaibro authored Feb 23, 2023
1 parent 621c02c commit 6045d2e
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 17 deletions.
21 changes: 21 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
159 changes: 142 additions & 17 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}}))

Expand All @@ -3790,45 +3790,170 @@ 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()
Expect(err).NotTo(HaveOccurred())
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() {
Expand Down

0 comments on commit 6045d2e

Please sign in to comment.