1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reset ActiveSupport::CurrentAttributes even where executor doesn't wrap

Currently there's a problem with ActiveSupport::CurrentAttributes where
they don't reset unless there's a controller or a job executing.

This is because we correctly hook into the controller/job executor to
reset them.

However, we were missing plain tests, so this is that.
This commit is contained in:
Kasper Timm Hansen 2020-03-06 14:06:18 +01:00
parent e93e45a502
commit c962409ba6
No known key found for this signature in database
GPG key ID: 191153215EDA53D8
3 changed files with 29 additions and 8 deletions

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module ActiveSupport::CurrentAttributes::TestHelper # :nodoc:
def before_setup
ActiveSupport::CurrentAttributes.reset_all
super
end
def before_teardown
ActiveSupport::CurrentAttributes.reset_all
super
end
end

View file

@ -22,6 +22,11 @@ module ActiveSupport
app.reloader.before_class_unload { ActiveSupport::CurrentAttributes.clear_all }
app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all }
ActiveSupport.on_load(:active_support_test_case) do
require "active_support/current_attributes/test_helper"
include ActiveSupport::CurrentAttributes::TestHelper
end
end
initializer "active_support.deprecation_behavior" do |app|

View file

@ -1,8 +1,12 @@
# frozen_string_literal: true
require_relative "abstract_unit"
require "active_support/current_attributes/test_helper"
class CurrentAttributesTest < ActiveSupport::TestCase
# Automatically included in Rails apps via railtie but that dodn't run here.
include ActiveSupport::CurrentAttributes::TestHelper
Person = Struct.new(:id, :name, :time_zone)
class Current < ActiveSupport::CurrentAttributes
@ -40,15 +44,13 @@ class CurrentAttributesTest < ActiveSupport::TestCase
attribute :current, :previous
end
setup do
@original_time_zone = Time.zone
Current.reset
Session.reset
end
# Eagerly set-up `instance`s by reference.
[ Current.instance, Session.instance ]
teardown do
Time.zone = @original_time_zone
end
setup { @original_time_zone = Time.zone }
teardown { Time.zone = @original_time_zone }
setup { assert_nil Session.previous, "Expected Session to not have leaked state" }
test "read and write attribute" do
Current.world = "world/1"
@ -78,6 +80,7 @@ class CurrentAttributesTest < ActiveSupport::TestCase
Current.reset
assert_equal "UTC", Time.zone.name
assert_equal 42, Session.previous
assert_nil Session.current
end