Skip to content

Commit 2b3f8ab

Browse files
committed
Merge pull request #457 from allomov/add-pubsub
Add pubsub function
2 parents 3c0668c + 81e54de commit 2b3f8ab

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/redis.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,14 @@ def punsubscribe(*channels)
20322032
end
20332033
end
20342034

2035+
# Inspect the state of the Pub/Sub subsystem.
2036+
# Possible subcommands: channels, numsub, numpat.
2037+
def pubsub(subcommand, *args)
2038+
synchronize do |client|
2039+
client.call([:pubsub, subcommand, args].flatten)
2040+
end
2041+
end
2042+
20352043
# Watch the given keys to determine execution of the MULTI/EXEC block.
20362044
#
20372045
# Using a block is optional, but is necessary for thread-safety.

test/publish_subscribe_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,50 @@ def test_psubscribe_and_punsubscribe
8787
assert_equal "s1", @message
8888
end
8989

90+
def test_pubsub_with_numpat_subcommand
91+
target_version("2.8.0") do
92+
@subscribed = false
93+
wire = Wire.new do
94+
r.psubscribe("f*") do |on|
95+
on.psubscribe { |channel, total| @subscribed = true }
96+
on.pmessage { |pattern, channel, message| r.punsubscribe }
97+
end
98+
end
99+
Wire.pass while !@subscribed
100+
redis = Redis.new(OPTIONS)
101+
numpat_result = redis.pubsub(:numpat)
102+
103+
redis.publish("foo", "s1")
104+
wire.join
105+
106+
assert_equal redis.pubsub(:numpat), 0
107+
assert_equal numpat_result, 1
108+
end
109+
end
110+
111+
112+
def test_pubsub_with_channels_and_numsub_subcommnads
113+
target_version("2.8.0") do
114+
@subscribed = false
115+
wire = Wire.new do
116+
r.subscribe("foo") do |on|
117+
on.subscribe { |channel, total| @subscribed = true }
118+
on.message { |channel, message| r.unsubscribe }
119+
end
120+
end
121+
Wire.pass while !@subscribed
122+
redis = Redis.new(OPTIONS)
123+
channels_result = redis.pubsub(:channels)
124+
numsub_result = redis.pubsub(:numsub, 'foo', 'boo')
125+
126+
redis.publish("foo", "s1")
127+
wire.join
128+
129+
assert_equal channels_result, ['foo']
130+
assert_equal numsub_result, ['foo', '1', 'boo', '0']
131+
end
132+
end
133+
90134
def test_subscribe_connection_usable_after_raise
91135
@subscribed = false
92136

0 commit comments

Comments
 (0)