From a11f80d9d7f40aedeba6c0526bd4cec344bd0570 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 7 Oct 2014 13:06:41 +0300 Subject: [PATCH] Add pubsub* commands. --- commands.go | 22 ++++++++++++++++++++++ redis_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/commands.go b/commands.go index a3a35313..01e758a7 100644 --- a/commands.go +++ b/commands.go @@ -1208,3 +1208,25 @@ func (c *Client) DebugObject(key string) *StringCmd { c.Process(cmd) return cmd } + +//------------------------------------------------------------------------------ + +func (c *Client) PubSubChannels(pattern string) *StringSliceCmd { + cmd := NewStringSliceCmd("PUBSUB", "CHANNELS", pattern) + c.Process(cmd) + return cmd +} + +func (c *Client) PubSubNumSub(channels ...string) *StringSliceCmd { + args := []string{"PUBSUB", "NUMSUB"} + args = append(args, channels...) + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) PubSubNumPat() *IntCmd { + cmd := NewIntCmd("PUBSUB", "NUMPAT") + c.Process(cmd) + return cmd +} diff --git a/redis_test.go b/redis_test.go index 877be34e..2407795a 100644 --- a/redis_test.go +++ b/redis_test.go @@ -2562,6 +2562,52 @@ func (t *RedisTest) TestPubSub(c *C) { } } +func (t *RedisTest) TestPubSubChannels(c *C) { + channels, err := t.client.PubSubChannels("mychannel*").Result() + c.Assert(err, IsNil) + c.Assert(channels, HasLen, 0) + c.Assert(channels, Not(IsNil)) + + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.Subscribe("mychannel", "mychannel2"), IsNil) + + channels, err = t.client.PubSubChannels("mychannel*").Result() + c.Assert(err, IsNil) + c.Assert(sortStrings(channels), DeepEquals, []string{"mychannel", "mychannel2"}) +} + +func (t *RedisTest) TestPubSubNumSub(c *C) { + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.Subscribe("mychannel", "mychannel2"), IsNil) + + channels, err := t.client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result() + c.Assert(err, IsNil) + c.Assert( + channels, + DeepEquals, + []string{"mychannel", "1", "mychannel2", "1", "mychannel3", "0"}, + ) +} + +func (t *RedisTest) TestPubSubNumPat(c *C) { + num, err := t.client.PubSubNumPat().Result() + c.Assert(err, IsNil) + c.Assert(num, Equals, int64(0)) + + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.PSubscribe("mychannel*"), IsNil) + + num, err = t.client.PubSubNumPat().Result() + c.Assert(err, IsNil) + c.Assert(num, Equals, int64(1)) +} + //------------------------------------------------------------------------------ func (t *RedisTest) TestPipeline(c *C) {