Skip to content
This repository
Browse code

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

… be wrapped
  • Loading branch information...
commit b185d157fe5c14ecac348558d0c0b42658de7097 1 parent 9dc4f66
Nick Sieger nicksieger authored
1  activesupport/lib/active_support/core_ext/module/synchronization.rb
@@ -20,7 +20,6 @@ def synchronize(*methods)
20 20
21 21 methods.each do |method|
22 22 aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
23   -
24 23 if instance_methods.include?("#{aliased_method}_without_synchronization#{punctuation}")
25 24 raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
26 25 end
20 activesupport/test/core_ext/module/synchronization_test.rb
@@ -40,7 +40,7 @@ def test_double_synchronize_raises_an_argument_error
40 40 end
41 41 end
42 42
43   - def test_mutex_is_entered_during_method_call
  43 + def dummy_sync
44 44 dummy = Object.new
45 45 def dummy.synchronize
46 46 @sync_count ||= 0
@@ -48,11 +48,15 @@ def dummy.synchronize
48 48 yield
49 49 end
50 50 def dummy.sync_count; @sync_count; end
51   - @target.mutex = dummy
  51 + dummy
  52 + end
  53 +
  54 + def test_mutex_is_entered_during_method_call
  55 + @target.mutex = dummy_sync
52 56 @target.synchronize :to_s, :with => :mutex
53 57 @instance.to_s
54 58 @instance.to_s
55   - assert_equal 2, dummy.sync_count
  59 + assert_equal 2, @target.mutex.sync_count
56 60 end
57 61
58 62 def test_can_synchronize_method_with_punctuation
@@ -68,4 +72,14 @@ def dangerous!
68 72 @instance.dangerous!
69 73 assert @instance.dangerous?
70 74 end
  75 +
  76 + def test_can_synchronize_singleton_methods
  77 + @target.mutex = dummy_sync
  78 + class << @target
  79 + synchronize :to_s, :with => :mutex
  80 + end
  81 + assert @target.respond_to?(:to_s_without_synchronization)
  82 + assert_nothing_raised { @target.to_s; @target.to_s }
  83 + assert_equal 2, @target.mutex.sync_count
  84 + end
71 85 end

0 comments on commit b185d15

Please sign in to comment.
Something went wrong with that request. Please try again.