File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -301,9 +301,8 @@ def process(name)
301
301
error = e
302
302
end
303
303
ensure
304
- # Ensure we clean up any thread locals we copied so that the thread can reused.
305
304
ActiveSupport ::IsolatedExecutionState . clear
306
- locals . each { | k , _ | t2 [ k ] = nil }
305
+ clean_up_thread_locals ( locals , t2 )
307
306
308
307
@_response . commit!
309
308
end
@@ -377,6 +376,11 @@ def new_controller_thread # :nodoc:
377
376
end
378
377
end
379
378
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
+
380
384
def self . live_thread_pool_executor
381
385
@live_thread_pool_executor ||= Concurrent ::CachedThreadPool . new ( name : "action_controller.live" )
382
386
end
Original file line number Diff line number Diff line change @@ -29,6 +29,14 @@ def new_controller_thread # :nodoc:
29
29
yield
30
30
end
31
31
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
+
32
40
# Avoid a deadlock from the queue filling up
33
41
Buffer . queue_size = nil
34
42
end
Original file line number Diff line number Diff line change @@ -360,6 +360,10 @@ def setup
360
360
def @controller . new_controller_thread ( &block )
361
361
original_new_controller_thread ( &block )
362
362
end
363
+
364
+ def @controller . clean_up_thread_locals ( *args )
365
+ original_clean_up_thread_locals ( *args )
366
+ end
363
367
end
364
368
365
369
def test_set_cookie
@@ -658,6 +662,30 @@ def test_response_buffer_do_not_respond_to_to_ary
658
662
end
659
663
end
660
664
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
+
661
689
class BufferTest < ActionController ::TestCase
662
690
def test_nil_callback
663
691
buf = ActionController ::Live ::Buffer . new nil
You can’t perform that action at this time.
0 commit comments