sdb_lock 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -40,7 +40,9 @@ locked = lock.try_lock("a1") do
40
40
  # do some work
41
41
  end
42
42
 
43
- # if you want to block until gain lock, then
43
+ # if you want to block until gain lock, then use #lock
44
+ # WARN There is no way to wake up others. It is slow with high contention
45
+ # because it does polling internally.
44
46
  lock.lock("a1") do
45
47
  # do some work
46
48
  end
@@ -1,3 +1,3 @@
1
1
  class SdbLock
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/sdb_lock.rb CHANGED
@@ -27,6 +27,9 @@ class SdbLock
27
27
  # Attribute name to be used to save locked time
28
28
  LOCK_TIME = 'lock_time'
29
29
 
30
+ # Max wait secs for #lock
31
+ MAX_WAIT_SECS = 2
32
+
30
33
  # Constructor
31
34
  #
32
35
  # @param [String] domain_name SimpleDB domain name
@@ -69,7 +72,7 @@ class SdbLock
69
72
  while true
70
73
  lock = try_lock(resource_name)
71
74
  break if lock
72
- sleep(wait_secs)
75
+ sleep([wait_secs, MAX_WAIT_SECS].min)
73
76
  wait_secs *= 2
74
77
  end
75
78
 
data/test/lock_test.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'minitest/unit'
2
+ require 'minitest/autorun'
3
+ require "sdb_lock"
4
+
5
+ class LockTest < MiniTest::Unit::TestCase
6
+ def setup
7
+ @lock = SdbLock.new(
8
+ 'lock_test',
9
+ create_domain: true,
10
+ simple_db_endpoint: "sdb.ap-northeast-1.amazonaws.com"
11
+ )
12
+ @lock.unlock("test")
13
+ end
14
+
15
+ # This test will take long time (16 secs in my environment).
16
+ def test_try_lock
17
+ shared_var = 0
18
+ locked_count = 0
19
+
20
+ threads = 10.times.map do
21
+ Thread.new do
22
+ Thread.pass
23
+ locked = @lock.try_lock("test") { shared_var += 1 }
24
+ locked_count += 1 if locked
25
+ end
26
+ end
27
+ threads.each { |thread| thread.join }
28
+ assert_equal(locked_count, shared_var)
29
+ end
30
+
31
+ def test_lock
32
+ shared_var = 0
33
+ threads = 10.times.map do
34
+ Thread.new do
35
+ Thread.pass
36
+ @lock.lock("test") { shared_var += 1 }
37
+ end
38
+ end
39
+ threads.each { |thread| thread.join }
40
+ assert_equal(10, shared_var)
41
+ end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdb_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -58,6 +58,7 @@ files:
58
58
  - lib/sdb_lock.rb
59
59
  - lib/sdb_lock/version.rb
60
60
  - sdb_lock.gemspec
61
+ - test/lock_test.rb
61
62
  homepage: ''
62
63
  licenses: []
63
64
  post_install_message:
@@ -72,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
73
  version: '0'
73
74
  segments:
74
75
  - 0
75
- hash: -3369914292283911745
76
+ hash: 1769990317450640860
76
77
  required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  none: false
78
79
  requirements:
@@ -81,11 +82,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  version: '0'
82
83
  segments:
83
84
  - 0
84
- hash: -3369914292283911745
85
+ hash: 1769990317450640860
85
86
  requirements: []
86
87
  rubyforge_project:
87
88
  rubygems_version: 1.8.24
88
89
  signing_key:
89
90
  specification_version: 3
90
91
  summary: Lock library using Amazon SimpleDB
91
- test_files: []
92
+ test_files:
93
+ - test/lock_test.rb