aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/module
diff options
context:
space:
mode:
authorNick <nick@nicksieger.com>2008-04-19 10:07:55 -0500
committerNick Sieger <nick@nicksieger.com>2008-08-29 14:12:08 -0500
commit3eb68248e0c0495c4533792260c9262fcd2840af (patch)
tree01b4d30fd7e7a6a380928d3640f6a6d4a525118d /activesupport/test/core_ext/module
parent5879b15f23f080badedbbab7835eda8c2c748a52 (diff)
downloadrails-3eb68248e0c0495c4533792260c9262fcd2840af.tar.gz
rails-3eb68248e0c0495c4533792260c9262fcd2840af.tar.bz2
rails-3eb68248e0c0495c4533792260c9262fcd2840af.zip
Adds Module#synchronize for easier method-level synchronization.
Diffstat (limited to 'activesupport/test/core_ext/module')
-rw-r--r--activesupport/test/core_ext/module/synchronization_test.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/module/synchronization_test.rb b/activesupport/test/core_ext/module/synchronization_test.rb
new file mode 100644
index 0000000000..78be6b725f
--- /dev/null
+++ b/activesupport/test/core_ext/module/synchronization_test.rb
@@ -0,0 +1,57 @@
+require 'abstract_unit'
+
+class SynchronizationTest < Test::Unit::TestCase
+ def setup
+ @target = Class.new
+ @target.cattr_accessor :mutex, :instance_writer => false
+ @target.mutex = Mutex.new
+ @instance = @target.new
+ end
+
+ def test_synchronize_aliases_method_chain_with_synchronize
+ @target.module_eval do
+ attr_accessor :value
+ synchronize :value, :with => :mutex
+ end
+ assert @instance.respond_to?(:value_with_synchronization)
+ assert @instance.respond_to?(:value_without_synchronization)
+ end
+
+ def test_synchronize_does_not_change_behavior
+ @target.module_eval do
+ attr_accessor :value
+ synchronize :value, :with => :mutex
+ end
+ expected = "some state"
+ @instance.value = expected
+ assert_equal expected, @instance.value
+ end
+
+ def test_synchronize_with_no_mutex_raises_an_argument_error
+ assert_raises(ArgumentError) do
+ @target.synchronize :to_s
+ end
+ end
+
+ def test_double_synchronize_raises_an_argument_error
+ @target.synchronize :to_s, :with => :mutex
+ assert_raises(ArgumentError) do
+ @target.synchronize :to_s, :with => :mutex
+ end
+ end
+
+ def test_mutex_is_entered_during_method_call
+ dummy = Object.new
+ def dummy.synchronize
+ @sync_count ||= 0
+ @sync_count += 1
+ yield
+ end
+ def dummy.sync_count; @sync_count; end
+ @target.mutex = dummy
+ @target.synchronize :to_s, :with => :mutex
+ @instance.to_s
+ @instance.to_s
+ assert_equal 2, dummy.sync_count
+ end
+end \ No newline at end of file