Skip to content

Commit

Permalink
Fix races in publish_subscribe_test.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Nov 17, 2023
1 parent c430c84 commit 2a65de5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions test/redis/publish_subscribe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_psubscribe_connection_usable_after_raise
end

def test_subscribe_within_subscribe
@channels = []
@channels = Queue.new

thread = new_thread do |r|
r.subscribe(channel_name) do |on|
Expand All @@ -192,7 +192,8 @@ def test_subscribe_within_subscribe

thread.join

assert_equal [channel_name, "bar"], @channels
assert_equal [channel_name, "bar"], [@channels.pop, @channels.pop]
assert_empty @channels
end

def test_other_commands_within_a_subscribe
Expand Down Expand Up @@ -270,8 +271,7 @@ def test_psubscribe_with_timeout
def test_unsubscribe_from_another_thread
@unsubscribed = @subscribed = false
@subscribed_redis = nil
@messages = []
@messages_count = 0
@messages = Queue.new
thread = new_thread do |r|
@subscribed_redis = r
r.subscribe(channel_name) do |on|
Expand All @@ -281,7 +281,6 @@ def test_unsubscribe_from_another_thread

on.message do |channel, message|
@messages << [channel, message]
@messages_count += 1
end

on.unsubscribe do |_channel, _total|
Expand All @@ -293,16 +292,16 @@ def test_unsubscribe_from_another_thread
Thread.pass until @subscribed

redis.publish(channel_name, "test")
Thread.pass until @messages_count == 1
assert_equal [channel_name, "test"], @messages.last
assert_equal [channel_name, "test"], @messages.pop
assert_empty @messages

@subscribed_redis.unsubscribe # this shouldn't block
refute_nil thread.join(2)
assert_equal true, @unsubscribed
end

def test_subscribe_from_another_thread
@events = []
@events = Queue.new
@subscribed_redis = nil
thread = new_thread do |r|
r.subscribe(channel_name) do |on|
Expand Down Expand Up @@ -339,7 +338,8 @@ def test_subscribe_from_another_thread
["unsubscribed", channel_name],
["unsubscribed", "#{channel_name}:2"]
]
assert_equal expected, @events
assert_equal(expected, expected.map { @events.pop })
assert_empty @events
end

private
Expand Down

0 comments on commit 2a65de5

Please sign in to comment.