Browse files

Module#synchronize: Add testcase to ensure that singleton methods can…

… be wrapped
  • Loading branch information...
1 parent 9dc4f66 commit b185d157fe5c14ecac348558d0c0b42658de7097 @nicksieger nicksieger committed with nicksieger Apr 19, 2008
View
1 activesupport/lib/active_support/core_ext/module/synchronization.rb
@@ -20,7 +20,6 @@ def synchronize(*methods)
methods.each do |method|
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
-
if instance_methods.include?("#{aliased_method}_without_synchronization#{punctuation}")
raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
end
View
20 activesupport/test/core_ext/module/synchronization_test.rb
@@ -40,19 +40,23 @@ def test_double_synchronize_raises_an_argument_error
end
end
- def test_mutex_is_entered_during_method_call
+ def dummy_sync
dummy = Object.new
def dummy.synchronize
@sync_count ||= 0
@sync_count += 1
yield
end
def dummy.sync_count; @sync_count; end
- @target.mutex = dummy
+ dummy
+ end
+
+ def test_mutex_is_entered_during_method_call
+ @target.mutex = dummy_sync
@target.synchronize :to_s, :with => :mutex
@instance.to_s
@instance.to_s
- assert_equal 2, dummy.sync_count
+ assert_equal 2, @target.mutex.sync_count
end
def test_can_synchronize_method_with_punctuation
@@ -68,4 +72,14 @@ def dangerous!
@instance.dangerous!
assert @instance.dangerous?
end
+
+ def test_can_synchronize_singleton_methods
+ @target.mutex = dummy_sync
+ class << @target
+ synchronize :to_s, :with => :mutex
+ end
+ assert @target.respond_to?(:to_s_without_synchronization)
+ assert_nothing_raised { @target.to_s; @target.to_s }
+ assert_equal 2, @target.mutex.sync_count
+ end
end

0 comments on commit b185d15

Please sign in to comment.