2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2021-11-05 08:09:14 -04:00
|
|
|
require "active_support/current_attributes/test_helper"
|
2017-05-26 14:00:27 -04:00
|
|
|
|
|
|
|
class CurrentAttributesTest < ActiveSupport::TestCase
|
2021-10-28 06:57:49 -04:00
|
|
|
# CurrentAttributes is automatically reset in Rails app via executor hooks set in railtie
|
|
|
|
# But not in Active Support's own test suite.
|
2021-11-05 08:09:14 -04:00
|
|
|
include ActiveSupport::CurrentAttributes::TestHelper
|
2020-03-06 08:06:18 -05:00
|
|
|
|
2019-01-27 05:18:40 -05:00
|
|
|
Person = Struct.new(:id, :name, :time_zone)
|
2017-05-26 14:00:27 -04:00
|
|
|
|
|
|
|
class Current < ActiveSupport::CurrentAttributes
|
|
|
|
attribute :world, :account, :person, :request
|
|
|
|
delegate :time_zone, to: :person
|
|
|
|
|
2019-08-01 03:41:26 -04:00
|
|
|
before_reset { Session.previous = person&.id }
|
2019-01-27 05:18:40 -05:00
|
|
|
|
|
|
|
resets do
|
|
|
|
Time.zone = "UTC"
|
|
|
|
Session.current = nil
|
|
|
|
end
|
2017-05-26 14:00:27 -04:00
|
|
|
|
|
|
|
def account=(account)
|
|
|
|
super
|
2019-08-01 03:41:26 -04:00
|
|
|
self.person = Person.new(1, "#{account}'s person")
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def person=(person)
|
|
|
|
super
|
2019-08-01 03:41:26 -04:00
|
|
|
Time.zone = person&.time_zone
|
|
|
|
Session.current = person&.id
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
2021-02-22 11:42:48 -05:00
|
|
|
def set_world_and_account(world:, account:)
|
|
|
|
self.world = world
|
|
|
|
self.account = account
|
|
|
|
end
|
|
|
|
|
2021-03-02 08:25:48 -05:00
|
|
|
def get_world_and_account(hash)
|
|
|
|
hash[:world] = world
|
|
|
|
hash[:account] = account
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2021-02-22 11:42:48 -05:00
|
|
|
def respond_to_test; end
|
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
def request
|
|
|
|
"#{super} something"
|
|
|
|
end
|
|
|
|
|
|
|
|
def intro
|
|
|
|
"#{person.name}, in #{time_zone}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-27 05:18:40 -05:00
|
|
|
class Session < ActiveSupport::CurrentAttributes
|
|
|
|
attribute :current, :previous
|
|
|
|
end
|
|
|
|
|
2020-03-06 08:06:18 -05:00
|
|
|
# Eagerly set-up `instance`s by reference.
|
|
|
|
[ Current.instance, Session.instance ]
|
2017-05-29 18:58:00 -04:00
|
|
|
|
2020-06-01 19:29:40 -04:00
|
|
|
# Use library specific minitest hook to catch Time.zone before reset is called via TestHelper
|
|
|
|
def before_setup
|
|
|
|
@original_time_zone = Time.zone
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# Use library specific minitest hook to set Time.zone after reset is called via TestHelper
|
|
|
|
def after_teardown
|
|
|
|
super
|
|
|
|
Time.zone = @original_time_zone
|
|
|
|
end
|
2020-03-06 08:06:18 -05:00
|
|
|
|
|
|
|
setup { assert_nil Session.previous, "Expected Session to not have leaked state" }
|
2017-05-26 14:00:27 -04:00
|
|
|
|
|
|
|
test "read and write attribute" do
|
|
|
|
Current.world = "world/1"
|
|
|
|
assert_equal "world/1", Current.world
|
|
|
|
end
|
|
|
|
|
|
|
|
test "read overwritten attribute method" do
|
|
|
|
Current.request = "request/1"
|
|
|
|
assert_equal "request/1 something", Current.request
|
|
|
|
end
|
|
|
|
|
|
|
|
test "set attribute via overwritten method" do
|
|
|
|
Current.account = "account/1"
|
|
|
|
assert_equal "account/1", Current.account
|
2019-08-01 03:41:26 -04:00
|
|
|
assert_equal "account/1's person", Current.person.name
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "set auxiliary class via overwritten method" do
|
2019-01-27 05:18:40 -05:00
|
|
|
Current.person = Person.new(42, "David", "Central Time (US & Canada)")
|
2017-05-26 14:00:27 -04:00
|
|
|
assert_equal "Central Time (US & Canada)", Time.zone.name
|
2019-01-27 05:18:40 -05:00
|
|
|
assert_equal 42, Session.current
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
2019-01-27 05:18:40 -05:00
|
|
|
test "resets auxiliary classes via callback" do
|
|
|
|
Current.person = Person.new(42, "David", "Central Time (US & Canada)")
|
2017-05-26 14:00:27 -04:00
|
|
|
assert_equal "Central Time (US & Canada)", Time.zone.name
|
|
|
|
|
|
|
|
Current.reset
|
|
|
|
assert_equal "UTC", Time.zone.name
|
2020-03-06 08:06:18 -05:00
|
|
|
assert_equal 42, Session.previous
|
2019-01-27 05:18:40 -05:00
|
|
|
assert_nil Session.current
|
|
|
|
end
|
|
|
|
|
|
|
|
test "set auxiliary class based on current attributes via before callback" do
|
|
|
|
Current.person = Person.new(42, "David", "Central Time (US & Canada)")
|
|
|
|
assert_nil Session.previous
|
|
|
|
assert_equal 42, Session.current
|
|
|
|
|
|
|
|
Current.reset
|
|
|
|
assert_equal 42, Session.previous
|
|
|
|
assert_nil Session.current
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "set attribute only via scope" do
|
|
|
|
Current.world = "world/1"
|
|
|
|
|
|
|
|
Current.set(world: "world/2") do
|
|
|
|
assert_equal "world/2", Current.world
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "world/1", Current.world
|
|
|
|
end
|
|
|
|
|
|
|
|
test "set multiple attributes" do
|
|
|
|
Current.world = "world/1"
|
|
|
|
Current.account = "account/1"
|
|
|
|
|
|
|
|
Current.set(world: "world/2", account: "account/2") do
|
|
|
|
assert_equal "world/2", Current.world
|
|
|
|
assert_equal "account/2", Current.account
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "world/1", Current.world
|
|
|
|
assert_equal "account/1", Current.account
|
|
|
|
end
|
|
|
|
|
2021-02-22 11:42:48 -05:00
|
|
|
test "using keyword arguments" do
|
|
|
|
Current.set_world_and_account(world: "world/1", account: "account/1")
|
|
|
|
|
|
|
|
assert_equal "world/1", Current.world
|
|
|
|
assert_equal "account/1", Current.account
|
2021-03-02 08:25:48 -05:00
|
|
|
|
|
|
|
hash = {}
|
|
|
|
assert_same hash, Current.get_world_and_account(hash)
|
|
|
|
assert_equal "world/1", hash[:world]
|
|
|
|
assert_equal "account/1", hash[:account]
|
2021-02-22 11:42:48 -05:00
|
|
|
end
|
|
|
|
|
2020-11-09 15:15:42 -05:00
|
|
|
setup { @testing_teardown = false }
|
|
|
|
teardown { assert_equal 42, Session.current if @testing_teardown }
|
|
|
|
|
|
|
|
test "accessing attributes in teardown" do
|
|
|
|
Session.current = 42
|
|
|
|
@testing_teardown = true
|
|
|
|
end
|
|
|
|
|
2017-05-26 14:00:27 -04:00
|
|
|
test "delegation" do
|
2019-01-27 05:18:40 -05:00
|
|
|
Current.person = Person.new(42, "David", "Central Time (US & Canada)")
|
2017-05-26 14:00:27 -04:00
|
|
|
assert_equal "Central Time (US & Canada)", Current.time_zone
|
|
|
|
assert_equal "Central Time (US & Canada)", Current.instance.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
test "all methods forward to the instance" do
|
2019-01-27 05:18:40 -05:00
|
|
|
Current.person = Person.new(42, "David", "Central Time (US & Canada)")
|
2017-05-26 14:00:27 -04:00
|
|
|
assert_equal "David, in Central Time (US & Canada)", Current.intro
|
|
|
|
assert_equal "David, in Central Time (US & Canada)", Current.instance.intro
|
|
|
|
end
|
2021-02-22 11:42:48 -05:00
|
|
|
|
|
|
|
test "respond_to? for methods that have not been called" do
|
|
|
|
assert_equal true, Current.respond_to?("respond_to_test")
|
|
|
|
end
|
2020-05-22 15:09:26 -04:00
|
|
|
|
|
|
|
test "CurrentAttributes use fiber-local variables" do
|
2021-11-05 05:30:36 -04:00
|
|
|
previous_level = ActiveSupport::IsolatedExecutionState.isolation_level
|
|
|
|
ActiveSupport::IsolatedExecutionState.isolation_level = :fiber
|
|
|
|
|
2020-05-22 15:09:26 -04:00
|
|
|
Session.current = 42
|
|
|
|
enumerator = Enumerator.new do |yielder|
|
|
|
|
yielder.yield Session.current
|
|
|
|
end
|
|
|
|
assert_nil enumerator.next
|
2021-11-05 05:30:36 -04:00
|
|
|
ensure
|
|
|
|
ActiveSupport::IsolatedExecutionState.isolation_level = previous_level
|
2020-05-22 15:09:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "CurrentAttributes can use thread-local variables" do
|
2021-11-05 05:30:36 -04:00
|
|
|
previous_level = ActiveSupport::IsolatedExecutionState.isolation_level
|
|
|
|
ActiveSupport::IsolatedExecutionState.isolation_level = :thread
|
|
|
|
|
2020-05-22 15:09:26 -04:00
|
|
|
Session.current = 42
|
|
|
|
enumerator = Enumerator.new do |yielder|
|
|
|
|
yielder.yield Session.current
|
|
|
|
end
|
|
|
|
assert_equal 42, enumerator.next
|
|
|
|
ensure
|
2021-11-05 05:30:36 -04:00
|
|
|
ActiveSupport::IsolatedExecutionState.isolation_level = previous_level
|
2020-05-22 15:09:26 -04:00
|
|
|
end
|
2017-05-26 14:00:27 -04:00
|
|
|
end
|