redis-mutex 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.2.2
@@ -17,21 +17,31 @@ class Redis
17
17
  end
18
18
 
19
19
  def method_added(target)
20
- return if target.to_s =~ /^auto_mutex_methods.*$/
20
+ return if target.to_s =~ /^auto_mutex_methods/
21
21
  return unless self.auto_mutex_methods[target]
22
22
  without_method = "#{target}_without_auto_mutex"
23
23
  with_method = "#{target}_with_auto_mutex"
24
+ after_method = "#{target}_after_failure"
24
25
  return if method_defined?(without_method)
26
+ options = self.auto_mutex_methods[target]
27
+
28
+ if options[:after_failure].is_a?(Proc)
29
+ define_method(after_method, &options[:after_failure])
30
+ end
25
31
 
26
32
  define_method(with_method) do |*args|
27
33
  key = self.class.name << '#' << target.to_s
28
- options = self.class.auto_mutex_methods[target]
34
+ response = nil
29
35
 
30
36
  success = Redis::Mutex.lock(key, options) do
31
- send(without_method, *args)
37
+ response = send(without_method, *args)
38
+ end
39
+
40
+ if !success and respond_to?(after_method)
41
+ response = send(after_method, *args)
32
42
  end
33
43
 
34
- options[:after_failure].call if !success and options[:after_failure]
44
+ response
35
45
  end
36
46
 
37
47
  alias_method without_method, target
data/redis-mutex.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "redis-mutex"
8
- s.version = "1.2.1"
8
+ s.version = "1.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kenn Ejima"]
@@ -78,25 +78,19 @@ describe Redis::Mutex do
78
78
 
79
79
  class C
80
80
  include Redis::Mutex::Macro
81
- auto_mutex :run_singularly, :block => 0, :after_failure => lambda { @@failure += 1 }
82
- @@success = 0
83
- @@failure = 0
81
+ auto_mutex :run_singularly, :block => 0, :after_failure => lambda {|id| return "failure: #{id}" }
84
82
 
85
- def run_singularly
83
+ def run_singularly(id)
86
84
  sleep 0.1
87
- Thread.exclusive { @@success += 1 }
85
+ return "success: #{id}"
88
86
  end
89
-
90
- def self.success; @@success; end
91
- def self.failure; @@failure; end
92
87
  end
93
88
 
94
- t1 = Thread.new { C.new.run_singularly }
95
- t2 = Thread.new { C.new.run_singularly }
89
+ t1 = Thread.new { C.new.run_singularly(1).should == "success: 1" }
90
+ sleep 0.01 # In most cases t1 wins, but make sure to give it a head start, not exceeding the sleep inside the method
91
+ t2 = Thread.new { C.new.run_singularly(2).should == "failure: 2" }
96
92
  t1.join
97
93
  t2.join
98
- C.success.should == 1
99
- C.failure.should == 1
100
94
  end
101
95
  end
102
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-mutex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis-classy
16
- requirement: &2166010020 !ruby/object:Gem::Requirement
16
+ requirement: &2162029080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2166010020
24
+ version_requirements: *2162029080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2166009440 !ruby/object:Gem::Requirement
27
+ requirement: &2161974220 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2166009440
35
+ version_requirements: *2161974220
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2166008600 !ruby/object:Gem::Requirement
38
+ requirement: &2161971780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2166008600
46
+ version_requirements: *2161971780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2166006520 !ruby/object:Gem::Requirement
49
+ requirement: &2161970380 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2166006520
57
+ version_requirements: *2161970380
58
58
  description: Distrubuted mutex using Redis
59
59
  email: kenn.ejima@gmail.com
60
60
  executables: []
@@ -92,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  segments:
94
94
  - 0
95
- hash: -2916954195788433702
95
+ hash: -2314477826109442509
96
96
  required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements: