diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-17 15:52:49 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-17 15:52:49 +0100 |
commit | bb4a9e22b02bf954865309c5df1d9762af471973 (patch) | |
tree | 5ca5324325f2e1b89860618de90c0736bf971e16 /activesupport/test/core_ext | |
parent | 529d05fed1737d30058339318626a32dc37ec463 (diff) | |
parent | 8a40cba66ae08c33ff062125b485fddd01f8264d (diff) | |
download | rails-bb4a9e22b02bf954865309c5df1d9762af471973.tar.gz rails-bb4a9e22b02bf954865309c5df1d9762af471973.tar.bz2 rails-bb4a9e22b02bf954865309c5df1d9762af471973.zip |
Merge pull request #22630 from rails/attribute-accessors-per-thread
Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb b/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb new file mode 100644 index 0000000000..65fadc5c20 --- /dev/null +++ b/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb @@ -0,0 +1,109 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/attribute_accessors_per_thread' + +class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase + def setup + @class = Class.new do + thread_mattr_accessor :foo + thread_mattr_accessor :bar, instance_writer: false + thread_mattr_reader :shaq, instance_reader: false + thread_mattr_accessor :camp, instance_accessor: false + end + + @object = @class.new + end + + def test_should_use_mattr_default + Thread.new do + assert_nil @class.foo + assert_nil @object.foo + end.join + end + + def test_should_set_mattr_value + Thread.new do + @class.foo = :test + assert_equal :test, @class.foo + + @class.foo = :test2 + assert_equal :test2, @class.foo + end.join + end + + def test_should_not_create_instance_writer + Thread.new do + assert_respond_to @class, :foo + assert_respond_to @class, :foo= + assert_respond_to @object, :bar + assert !@object.respond_to?(:bar=) + end.join + end + + def test_should_not_create_instance_reader + Thread.new do + assert_respond_to @class, :shaq + assert !@object.respond_to?(:shaq) + end.join + end + + def test_should_not_create_instance_accessors + Thread.new do + assert_respond_to @class, :camp + assert !@object.respond_to?(:camp) + assert !@object.respond_to?(:camp=) + end.join + end + + def test_values_should_not_bleed_between_threads + threads = [] + threads << Thread.new do + @class.foo = 'things' + sleep 1 + assert_equal 'things', @class.foo + end + + threads << Thread.new do + @class.foo = 'other things' + sleep 1 + assert_equal 'other things', @class.foo + end + + threads << Thread.new do + @class.foo = 'really other things' + sleep 1 + assert_equal 'really other things', @class.foo + end + + threads.each { |t| t.join } + end + + def test_should_raise_name_error_if_attribute_name_is_invalid + exception = assert_raises NameError do + Class.new do + thread_cattr_reader "1nvalid" + end + end + assert_equal "invalid attribute name: 1nvalid", exception.message + + exception = assert_raises NameError do + Class.new do + thread_cattr_writer "1nvalid" + end + end + assert_equal "invalid attribute name: 1nvalid", exception.message + + exception = assert_raises NameError do + Class.new do + thread_mattr_reader "1valid_part" + end + end + assert_equal "invalid attribute name: 1valid_part", exception.message + + exception = assert_raises NameError do + Class.new do + thread_mattr_writer "2valid_part" + end + end + assert_equal "invalid attribute name: 2valid_part", exception.message + end +end |