lock_method 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.5.2 / 2012-04-17
2
+
3
+ * Enhancements
4
+
5
+ * Support locking methods that have block arguments
6
+
1
7
  0.5.1 / 2012-04-17
2
8
 
3
9
  * Bug fixes
@@ -36,15 +36,19 @@ module LockMethod
36
36
  attr_reader :obj
37
37
  attr_reader :method_id
38
38
  attr_reader :args
39
+ attr_reader :blk
40
+ attr_reader :original_method_id
39
41
 
40
- def initialize(obj, method_id, options = {})
42
+ def initialize(obj, method_id, options = {}, &blk)
41
43
  @mutex = ::Mutex.new
42
44
  @obj = obj
43
45
  @method_id = method_id
46
+ @blk = blk
44
47
  options = options.symbolize_keys
45
48
  @ttl = options[:ttl]
46
49
  @args = options[:args]
47
50
  @spin = options[:spin]
51
+ @original_method_id = options[:original_method_id]
48
52
  end
49
53
 
50
54
  def spin?
@@ -99,7 +103,7 @@ module LockMethod
99
103
  # nothing
100
104
  end
101
105
 
102
- def call_and_lock(*original_method_id_and_args)
106
+ def call_and_lock
103
107
  while locked? and spin?
104
108
  ::Kernel.sleep 0.5
105
109
  end
@@ -108,7 +112,7 @@ module LockMethod
108
112
  else
109
113
  begin
110
114
  save
111
- obj.send(*original_method_id_and_args)
115
+ obj.send(*([original_method_id]+args), &blk)
112
116
  ensure
113
117
  delete
114
118
  end
@@ -1,3 +1,3 @@
1
1
  module LockMethod
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/lib/lock_method.rb CHANGED
@@ -64,10 +64,10 @@ module LockMethod
64
64
  end
65
65
  original_method_id = "_unlocked_#{method_id}"
66
66
  alias_method original_method_id, method_id
67
- define_method method_id do |*args1|
68
- options = options.merge(:args => args1)
69
- lock = ::LockMethod::Lock.new self, method_id, options
70
- lock.call_and_lock(*([original_method_id]+args1))
67
+ define_method method_id do |*args1, &blk|
68
+ options1 = options.merge(:args => args1, :original_method_id => original_method_id)
69
+ lock = ::LockMethod::Lock.new self, method_id, options1, &blk
70
+ lock.call_and_lock
71
71
  end
72
72
  end
73
73
  end
data/test/helper.rb CHANGED
@@ -81,6 +81,13 @@ module BlogSpin
81
81
  end
82
82
  end
83
83
 
84
- class Test::Unit::TestCase
85
-
84
+ module BlogBlock
85
+ def self.get_latest_entries
86
+ sleep 2
87
+ yield
88
+ "i yielded"
89
+ end
90
+ class << self
91
+ lock_method :get_latest_entries
92
+ end
86
93
  end
data/test/shared_tests.rb CHANGED
@@ -206,4 +206,26 @@ module SharedTests
206
206
 
207
207
  assert_equal 'danke schoen', BlogSpin.get_latest_entries
208
208
  end
209
+
210
+ def test_15_block
211
+ blocker = Thread.new do
212
+ BlogBlock.get_latest_entries { $stderr.write "i'm in the way!" }
213
+ end
214
+
215
+ # give it a bit of time to lock
216
+ sleep 1
217
+
218
+ # the blocker won't have finished
219
+ assert_raises(LockMethod::Locked) do
220
+ BlogBlock.get_latest_entries { $stderr.write "don't print me" }
221
+ end
222
+
223
+ # wait to finish
224
+ blocker.join
225
+
226
+ # now we're sure
227
+ assert_nothing_raised do
228
+ BlogBlock.get_latest_entries { $stderr.write "i'm now allowed" }
229
+ end
230
+ end
209
231
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lock_method
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: