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

Use Thread.pass instead of Kernel.sleep to trigger race condition

This commit is contained in:
Guilherme Mansur 2019-05-21 14:45:29 -04:00 committed by Kasper Timm Hansen
parent 2bd2d853da
commit 6c6d852854
No known key found for this signature in database
GPG key ID: 191153215EDA53D8
2 changed files with 12 additions and 21 deletions

View file

@ -53,8 +53,7 @@ class Module
EOS
end
default_val = (block_given? && default.nil?) ? yield : default
Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
Thread.current["attr_" + name + "_#{sym}"] = default unless default.nil?
end
end
alias :thread_cattr_reader :thread_mattr_reader
@ -97,8 +96,7 @@ class Module
EOS
end
default_val = (block_given? && default.nil?) ? yield : default
Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
public_send("#{sym}=", default) unless default.nil?
end
end
alias :thread_cattr_writer :thread_mattr_writer
@ -142,9 +140,9 @@ class Module
#
# Current.new.user = "DHH" # => NoMethodError
# Current.new.user # => NoMethodError
def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, &blk)
thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default)
def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil)
thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default)
thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor)
end
alias :thread_cattr_accessor :thread_mattr_accessor
end

View file

@ -14,7 +14,7 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase
class SubMyClass < MyClass
end
def setup
setup do
@class = MyClass
@subclass = SubMyClass
@object = @class.new
@ -23,18 +23,11 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase
def test_can_initialize_with_default_value
Thread.new do
@class.thread_mattr_accessor :baz, default: "default_value"
assert_equal "default_value", @class.baz
end.join
end
def test_can_initialize_with_a_block_as_default_value
Thread.new do
@class.thread_mattr_accessor :baz do
"default_value"
end
assert_equal "default_value", @class.baz
end.join
assert_nil @class.baz
end
def test_should_use_mattr_default
@ -82,23 +75,23 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase
threads = []
threads << Thread.new do
@class.foo = "things"
sleep 1
Thread.pass
assert_equal "things", @class.foo
end
threads << Thread.new do
@class.foo = "other things"
sleep 1
Thread.pass
assert_equal "other things", @class.foo
end
threads << Thread.new do
@class.foo = "really other things"
sleep 1
Thread.pass
assert_equal "really other things", @class.foo
end
threads.each { |t| t.join }
threads.each(&:join)
end
def test_should_raise_name_error_if_attribute_name_is_invalid