Skip to content

Commit c8f71d5

Browse files
committed
Improve concurrent unsubscribe_from_channel test
1 parent 2fe6254 commit c8f71d5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

actioncable/test/channel/stream_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,11 @@ class StreamTest < ActionCable::TestCase
282282
end
283283

284284
test "concurrent unsubscribe_from_channel and stream_from do not raise RuntimeError" do
285-
ENV["UNSUBSCRIBE_SLEEP_TIME"] = "0.0001" # Set a delay to increase the chance of concurrent execution
285+
threads = []
286286
run_in_eventmachine do
287287
connection = TestConnection.new
288+
connection.pubsub.unsubscribe_latency = 0.1
289+
288290
channel = ChatChannel.new connection, "{id: 1}", id: 1
289291
channel.subscribe_to_channel
290292

@@ -309,6 +311,7 @@ class StreamTest < ActionCable::TestCase
309311
ensure
310312
barrier.wait
311313
end
314+
threads << thread1
312315

313316
# Thread 2: calls stream_from during unsubscribe_from_channel iteration
314317
thread2 = Thread.new do
@@ -323,6 +326,7 @@ class StreamTest < ActionCable::TestCase
323326
ensure
324327
barrier.wait
325328
end
329+
threads << thread2
326330

327331
thread1.join
328332
thread2.join
@@ -331,7 +335,7 @@ class StreamTest < ActionCable::TestCase
331335
assert_nil exception_caught, "Concurrent unsubscribe_from_channel and stream_from should not raise RuntimeError: #{exception_caught}"
332336
end
333337
ensure
334-
ENV.delete("UNSUBSCRIBE_SLEEP_TIME")
338+
threads.each(&:kill)
335339
end
336340

337341
private

actioncable/test/stubs/test_adapter.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# frozen_string_literal: true
22

33
class SuccessAdapter < ActionCable::SubscriptionAdapter::Base
4+
attr_accessor :unsubscribe_latency
5+
6+
def initialize(...)
7+
super
8+
@unsubscribe_latency = nil
9+
end
10+
411
def broadcast(channel, payload)
512
end
613

@@ -10,7 +17,7 @@ def subscribe(channel, callback, success_callback = nil)
1017
end
1118

1219
def unsubscribe(channel, callback)
13-
sleep ENV["UNSUBSCRIBE_SLEEP_TIME"].to_f if ENV["UNSUBSCRIBE_SLEEP_TIME"]
20+
sleep @unsubscribe_latency if @unsubscribe_latency
1421
subscriber_map[channel].delete(callback)
1522
subscriber_map.delete(channel) if subscriber_map[channel].empty?
1623
@@unsubscribe_called = { channel: channel, callback: callback }

0 commit comments

Comments
 (0)