Merge pull request #44 from go-redis/feature/add-pubsub-cmds

Add pubsub* commands.
This commit is contained in:
Vladimir Mihailenco 2014-10-07 14:02:07 +03:00
commit 7b28e6f9d2
2 changed files with 68 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {