1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# frozen_string_literal: true
require_relative "helper"
class TestPublishSubscribe < Minitest::Test
include Helper::Client
class TestError < StandardError
end
def test_subscribe_and_unsubscribe
@subscribed = false
@unsubscribed = false
wire = Wire.new do
r.subscribe("foo") do |on|
on.subscribe do |_channel, total|
@subscribed = true
@t1 = total
end
on.message do |_channel, message|
if message == "s1"
r.unsubscribe
@message = message
end
end
on.unsubscribe do |_channel, total|
@unsubscribed = true
@t2 = total
end
end
end
# Wait until the subscription is active before publishing
Wire.pass until @subscribed
Redis.new(OPTIONS).publish("foo", "s1")
wire.join
assert @subscribed
assert_equal 1, @t1
assert @unsubscribed
assert_equal 0, @t2
assert_equal "s1", @message
end
def test_psubscribe_and_punsubscribe
@subscribed = false
@unsubscribed = false
wire = Wire.new do
r.psubscribe("f*") do |on|
on.psubscribe do |_pattern, total|
@subscribed = true
@t1 = total
end
on.pmessage do |_pattern, _channel, message|
if message == "s1"
r.punsubscribe
@message = message
end
end
on.punsubscribe do |_pattern, total|
@unsubscribed = true
@t2 = total
end
end
end
# Wait until the subscription is active before publishing
Wire.pass until @subscribed
Redis.new(OPTIONS).publish("foo", "s1")
wire.join
assert @subscribed
assert_equal 1, @t1
assert @unsubscribed
assert_equal 0, @t2
assert_equal "s1", @message
end
def test_pubsub_with_numpat_subcommand
target_version("2.8.0") do
@subscribed = false
wire = Wire.new do
r.psubscribe("f*") do |on|
on.psubscribe { |_channel, _total| @subscribed = true }
on.pmessage { |_pattern, _channel, _message| r.punsubscribe }
end
end
Wire.pass until @subscribed
redis = Redis.new(OPTIONS)
numpat_result = redis.pubsub(:numpat)
redis.publish("foo", "s1")
wire.join
assert_equal redis.pubsub(:numpat), 0
assert_equal numpat_result, 1
end
end
def test_pubsub_with_channels_and_numsub_subcommnads
target_version("2.8.0") do
@subscribed = false
wire = Wire.new do
r.subscribe("foo") do |on|
on.subscribe { |_channel, _total| @subscribed = true }
on.message { |_channel, _message| r.unsubscribe }
end
end
Wire.pass until @subscribed
redis = Redis.new(OPTIONS)
channels_result = redis.pubsub(:channels)
channels_result.delete('__sentinel__:hello')
numsub_result = redis.pubsub(:numsub, 'foo', 'boo')
redis.publish("foo", "s1")
wire.join
assert_equal channels_result, ['foo']
assert_equal numsub_result, ['foo', 1, 'boo', 0]
end
end
def test_subscribe_connection_usable_after_raise
@subscribed = false
wire = Wire.new do
begin
r.subscribe("foo") do |on|
on.subscribe do |_channel, _total|
@subscribed = true
end
on.message do |_channel, _message|
raise TestError
end
end
rescue TestError
end
end
# Wait until the subscription is active before publishing
Wire.pass until @subscribed
Redis.new(OPTIONS).publish("foo", "s1")
wire.join
assert_equal "PONG", r.ping
end
def test_psubscribe_connection_usable_after_raise
@subscribed = false
wire = Wire.new do
begin
r.psubscribe("f*") do |on|
on.psubscribe do |_pattern, _total|
@subscribed = true
end
on.pmessage do |_pattern, _channel, _message|
raise TestError
end
end
rescue TestError
end
end
# Wait until the subscription is active before publishing
Wire.pass until @subscribed
Redis.new(OPTIONS).publish("foo", "s1")
wire.join
assert_equal "PONG", r.ping
end
def test_subscribe_within_subscribe
@channels = []
wire = Wire.new do
r.subscribe("foo") do |on|
on.subscribe do |channel, _total|
@channels << channel
r.subscribe("bar") if channel == "foo"
r.unsubscribe if channel == "bar"
end
end
end
wire.join
assert_equal ["foo", "bar"], @channels
end
def test_other_commands_within_a_subscribe
assert_raises Redis::CommandError do
r.subscribe("foo") do |on|
on.subscribe do |_channel, _total|
r.set("bar", "s2")
end
end
end
end
def test_subscribe_without_a_block
assert_raises LocalJumpError do
r.subscribe("foo")
end
end
def test_unsubscribe_without_a_subscribe
assert_raises RuntimeError do
r.unsubscribe
end
assert_raises RuntimeError do
r.punsubscribe
end
end
def test_subscribe_past_a_timeout
# For some reason, a thread here doesn't reproduce the issue.
sleep = %(sleep 0.05)
publish = %{ruby -rsocket -e 't=TCPSocket.new("127.0.0.1",#{OPTIONS[:port]});t.write("publish foo bar\\r\\n");t.read(4);t.close'}
cmd = [sleep, publish].join('; ')
IO.popen(cmd, 'r+') do |_pipe|
received = false
r.subscribe 'foo' do |on|
on.message do |_channel, _message|
received = true
r.unsubscribe
end
end
assert received
end
end
def test_subscribe_with_timeout
received = false
assert_raises Redis::TimeoutError do
r.subscribe_with_timeout(LOW_TIMEOUT, "foo") do |on|
on.message do |_channel, _message|
received = true
end
end
end
assert !received
end
def test_psubscribe_with_timeout
received = false
assert_raises Redis::TimeoutError do
r.psubscribe_with_timeout(LOW_TIMEOUT, "f*") do |on|
on.message do |_channel, _message|
received = true
end
end
end
assert !received
end
end
|