Skip to content

Commit fa9dec9

Browse files
authored
Merge pull request rails#54143 from Shopify/fix-action-controller-live-thread-locals-in-test
Stop clearing thread locals in test environment when controller includes ActionController::Live
2 parents cc0a09e + 5b2ac4a commit fa9dec9

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

actionpack/lib/action_controller/metal/live.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,8 @@ def process(name)
301301
error = e
302302
end
303303
ensure
304-
# Ensure we clean up any thread locals we copied so that the thread can reused.
305304
ActiveSupport::IsolatedExecutionState.clear
306-
locals.each { |k, _| t2[k] = nil }
305+
clean_up_thread_locals(locals, t2)
307306

308307
@_response.commit!
309308
end
@@ -377,6 +376,11 @@ def new_controller_thread # :nodoc:
377376
end
378377
end
379378

379+
# Ensure we clean up any thread locals we copied so that the thread can reused.
380+
def clean_up_thread_locals(locals, thread) # :nodoc:
381+
locals.each { |k, _| thread[k] = nil }
382+
end
383+
380384
def self.live_thread_pool_executor
381385
@live_thread_pool_executor ||= Concurrent::CachedThreadPool.new(name: "action_controller.live")
382386
end

actionpack/lib/action_controller/test_case.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def new_controller_thread # :nodoc:
2929
yield
3030
end
3131

32+
# Because of the above, we need to prevent the clearing of thread locals, since
33+
# no new thread is actually spawned in the test environment.
34+
alias_method :original_clean_up_thread_locals, :clean_up_thread_locals
35+
36+
silence_redefinition_of_method :clean_up_thread_locals
37+
def clean_up_thread_locals(*args) # :nodoc:
38+
end
39+
3240
# Avoid a deadlock from the queue filling up
3341
Buffer.queue_size = nil
3442
end

actionpack/test/controller/live_stream_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ def setup
360360
def @controller.new_controller_thread(&block)
361361
original_new_controller_thread(&block)
362362
end
363+
364+
def @controller.clean_up_thread_locals(*args)
365+
original_clean_up_thread_locals(*args)
366+
end
363367
end
364368

365369
def test_set_cookie
@@ -658,6 +662,30 @@ def test_response_buffer_do_not_respond_to_to_ary
658662
end
659663
end
660664

665+
class LiveControllerThreadTest < ActionController::TestCase
666+
class TestController < ActionController::Base
667+
include ActionController::Live
668+
669+
def greet
670+
response.headers["Content-Type"] = "text/event-stream"
671+
%w{ hello world }.each do |word|
672+
response.stream.write word
673+
end
674+
response.stream.close
675+
end
676+
end
677+
678+
tests TestController
679+
680+
def test_thread_locals_do_not_get_reset_in_test_environment
681+
Thread.current[:setting] = "aaron"
682+
683+
get :greet
684+
685+
assert_equal "aaron", Thread.current[:setting]
686+
end
687+
end
688+
661689
class BufferTest < ActionController::TestCase
662690
def test_nil_callback
663691
buf = ActionController::Live::Buffer.new nil

0 commit comments

Comments
 (0)