lock_method 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +18 -3
- data/lib/lock_method.rb +1 -1
- data/lib/lock_method/lock.rb +9 -1
- data/lib/lock_method/version.rb +1 -1
- data/test/helper.rb +3 -7
- data/test/shared_tests.rb +21 -0
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -6,12 +6,17 @@ It's like <tt>alias_method</tt>, but it's <tt>lock_method</tt>!
|
|
6
6
|
|
7
7
|
require 'lock_method'
|
8
8
|
class Blog
|
9
|
-
|
9
|
+
attr_accessor :url
|
10
|
+
|
10
11
|
def get_latest_entries
|
11
12
|
sleep 5
|
12
13
|
end
|
13
|
-
# [...]
|
14
14
|
lock_method :get_latest_entries
|
15
|
+
|
16
|
+
# used by lock_method to differentiate between instances
|
17
|
+
def hash
|
18
|
+
url.hash
|
19
|
+
end
|
15
20
|
end
|
16
21
|
|
17
22
|
Then you can do
|
@@ -23,12 +28,22 @@ Just in case, you can clear them
|
|
23
28
|
|
24
29
|
my_blog.clear_method_lock :get_latest_entries
|
25
30
|
|
26
|
-
==
|
31
|
+
== Ignores arguments when locking a method
|
27
32
|
|
28
33
|
lock_method ignores arguments when locking. So if you call Foo.bar(:a), calls to Foo.bar(:b) will be locked until the first call finishes.
|
29
34
|
|
30
35
|
Maybe future versions will support this.
|
31
36
|
|
37
|
+
== Defining #hash
|
38
|
+
|
39
|
+
If you want to lock instance methods, you should define <tt>#hash</tt> on those instances.
|
40
|
+
|
41
|
+
You should follow Ruby convention and have <tt>#hash</tt> return a <tt>Fixnum</tt>.
|
42
|
+
|
43
|
+
Ideally, you should try to make a <tt>String</tt> or a <tt>Hash</tt> and call the standard <tt>#hash</tt> on that.
|
44
|
+
|
45
|
+
Note: this is NOT the same thing as <tt>#to_hash</tt>! That returns a <tt>Hash</tt>... but what we want is an integer "hash code."
|
46
|
+
|
32
47
|
== Configuration (and supported cache clients)
|
33
48
|
|
34
49
|
The default is to use filesystem lockfiles, usually in <tt>/tmp/lock_method/*</tt>.
|
data/lib/lock_method.rb
CHANGED
data/lib/lock_method/lock.rb
CHANGED
@@ -85,7 +85,15 @@ module LockMethod
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def cache_key
|
88
|
-
|
88
|
+
if obj.is_a? ::Class
|
89
|
+
[ 'LockMethod', 'Lock', method_signature ].join ','
|
90
|
+
else
|
91
|
+
[ 'LockMethod', 'Lock', method_signature, obj_hash ].join ','
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def obj_hash
|
96
|
+
@obj_hash ||= obj.hash
|
89
97
|
end
|
90
98
|
|
91
99
|
def in_force?
|
data/lib/lock_method/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -25,6 +25,9 @@ class Blog1
|
|
25
25
|
["voo vaa #{name}"]
|
26
26
|
end
|
27
27
|
lock_method :get_latest_entries2, 5 # second
|
28
|
+
def hash
|
29
|
+
name.hash
|
30
|
+
end
|
28
31
|
end
|
29
32
|
|
30
33
|
class Blog2
|
@@ -42,13 +45,6 @@ class Blog2
|
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
|
-
def new_instance_of_my_blog
|
46
|
-
Blog1.new 'my_blog', 'http://my_blog.example.com'
|
47
|
-
end
|
48
|
-
def new_instance_of_another_blog
|
49
|
-
Blog1.new 'another_blog', 'http://another_blog.example.com'
|
50
|
-
end
|
51
|
-
|
52
48
|
class Test::Unit::TestCase
|
53
49
|
|
54
50
|
end
|
data/test/shared_tests.rb
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
def new_instance_of_my_blog
|
2
|
+
Blog1.new 'my_blog', 'http://my_blog.example.com'
|
3
|
+
end
|
4
|
+
def new_instance_of_another_blog
|
5
|
+
Blog1.new 'another_blog', 'http://another_blog.example.com'
|
6
|
+
end
|
7
|
+
|
1
8
|
module SharedTests
|
2
9
|
def test_locked_method_return_value
|
3
10
|
assert_equal ["hello from my_blog"], new_instance_of_my_blog.get_latest_entries
|
@@ -104,6 +111,20 @@ module SharedTests
|
|
104
111
|
end
|
105
112
|
end
|
106
113
|
|
114
|
+
def test_instance_method_lock_is_unique_to_instance
|
115
|
+
pid = Kernel.fork { new_instance_of_my_blog.get_latest_entries }
|
116
|
+
|
117
|
+
# give it a bit of time to lock
|
118
|
+
sleep 1
|
119
|
+
|
120
|
+
# the blocker won't have finished
|
121
|
+
assert_nothing_raised do
|
122
|
+
new_instance_of_another_blog.get_latest_entries
|
123
|
+
end
|
124
|
+
|
125
|
+
Process.wait pid
|
126
|
+
end
|
127
|
+
|
107
128
|
def test_clear_instance_method_lock
|
108
129
|
pid = Kernel.fork { new_instance_of_my_blog.get_latest_entries }
|
109
130
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lock_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02
|
18
|
+
date: 2011-03-02 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|