Skip to content

Commit

Permalink
Small refactoring of tests for subscribe/unsubscribe.
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Apr 3, 2010
1 parent 1783477 commit a434056
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions test/redis_test.rb
Expand Up @@ -826,39 +826,77 @@ def redis.call_command(attrs)
end

context "Publish/Subscribe" do

test "SUBSCRIBE and UNSUBSCRIBE" do
sub = Thread.new {
@r.subscribe('A') do |on|
on.subscribe {|klass, num| @subscribed = true }
on.message {|klass,msg| @r.unsubscribe }
on.unsubscribe {|klass, num| @unsubscribed = true }
thread = Thread.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
}
Redis.new(OPTIONS).publish('A', 'finish!')
sub.join
assert true, "message received; ie, this test didn't hang"
assert @subscribed, 'subscribed'
assert @unsubscribed, 'unsubscribed'
end

Redis.new(OPTIONS).publish("foo", "s1")

thread.join

assert @subscribed
assert_equal 1, @t1
assert @unsubscribed
assert_equal 0, @t2
assert_equal "s1", @message
end

test "SUBSCRIBE within SUBSCRIBE" do
@r.subscribe('A') do |on|
on.subscribe {|klass, num|
@r.subscribe('B') if klass == 'A'
@r.unsubscribe if klass == 'B'
}
@channels = []

thread = Thread.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
assert true, 'we are here, so it worked'

Redis.new(OPTIONS).publish("foo", "s1")

thread.join

assert_equal ["foo", "bar"], @channels
end

test "other commands within a SUBSCRIBE" do
assert_raise(RuntimeError) do
@r.subscribe(:a) do |on|
on.subscribe {|klass, num| @r.set('subscribed', 'true') }
assert_raise RuntimeError do
@r.subscribe("foo") do |on|
on.subscribe do |channel, total|
@r.set("bar", "s2")
end
end
end
end

test "SUBSCRIBE without a block" do
assert_raise RuntimeError do
@r.subscribe("foo")
end
end

test "SUBSCRIBE timeout"

end
Expand Down

0 comments on commit a434056

Please sign in to comment.