latch 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/VERSION +1 -1
- data/latch.gemspec +2 -2
- data/lib/latch.rb +14 -2
- data/spec/latch_spec.rb +33 -2
- metadata +3 -3
data/README
CHANGED
@@ -1 +1 @@
|
|
1
|
-
This is a
|
1
|
+
This is a simple countdown latch for Ruby. It's implemented under the covers with a Condition Variable.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/latch.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{latch}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kyle Maxwell"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-05-01}
|
13
13
|
s.description = %q{This is a really simple countdown latch for Ruby.}
|
14
14
|
s.email = %q{kyle@kylemaxwell.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/latch.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require "thread"
|
2
1
|
require "timeout"
|
3
|
-
|
4
2
|
class Latch
|
5
3
|
class Timeout < ::Timeout::Error; end
|
4
|
+
|
6
5
|
module Mixin
|
6
|
+
# Syntactic sugar!
|
7
7
|
def latch(count = 1, timeout = nil, &block)
|
8
8
|
l = Latch.new(count)
|
9
9
|
block.call(l)
|
@@ -27,8 +27,20 @@ class Latch
|
|
27
27
|
@cv.broadcast if @count <= 0
|
28
28
|
end
|
29
29
|
|
30
|
+
def try(&block)
|
31
|
+
block.call
|
32
|
+
rescue => e
|
33
|
+
fail(e)
|
34
|
+
end
|
35
|
+
|
36
|
+
def fail(exception)
|
37
|
+
@exception = exception
|
38
|
+
@cv.broadcast
|
39
|
+
end
|
40
|
+
|
30
41
|
def await(timeout = nil)
|
31
42
|
@cv.wait(@mutex, timeout)
|
43
|
+
raise @exception if @exception
|
32
44
|
raise Latch::Timeout if @count > 0
|
33
45
|
end
|
34
46
|
end
|
data/spec/latch_spec.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
EPSILON = 0.1
|
4
|
+
ITEMS = 1
|
5
|
+
|
6
|
+
class CustomError < StandardError; end
|
4
7
|
|
5
8
|
describe "Latch" do
|
6
9
|
it "should wait" do
|
7
10
|
start = Time.now
|
8
|
-
l = Latch.new(
|
11
|
+
l = Latch.new(ITEMS)
|
9
12
|
Thread.new do
|
10
13
|
sleep EPSILON
|
11
14
|
l.decr
|
@@ -28,7 +31,7 @@ describe "Latch" do
|
|
28
31
|
it "should timeout" do
|
29
32
|
start = Time.now
|
30
33
|
proc {
|
31
|
-
Latch::Mixin::latch(
|
34
|
+
Latch::Mixin::latch(ITEMS, EPSILON) do |l|
|
32
35
|
Thread.new do
|
33
36
|
sleep 10
|
34
37
|
l.decr
|
@@ -37,4 +40,32 @@ describe "Latch" do
|
|
37
40
|
}.should raise_error(Latch::Timeout)
|
38
41
|
end
|
39
42
|
|
43
|
+
it "should raise the exception in the outer thread" do
|
44
|
+
start = Time.now
|
45
|
+
proc {
|
46
|
+
Latch::Mixin::latch(ITEMS, EPSILON) do |l|
|
47
|
+
Thread.new do
|
48
|
+
begin
|
49
|
+
l.fail(CustomError)
|
50
|
+
rescue => e
|
51
|
+
puts e.message
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
}.should raise_error(CustomError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should have sugar for catching exceptions" do
|
59
|
+
start = Time.now
|
60
|
+
proc {
|
61
|
+
Latch::Mixin::latch(ITEMS, EPSILON) do |l|
|
62
|
+
Thread.new do
|
63
|
+
l.try {
|
64
|
+
raise CustomError
|
65
|
+
}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
}.should raise_error(CustomError)
|
69
|
+
end
|
70
|
+
|
40
71
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: latch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kyle Maxwell
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-01 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
hash: -
|
96
|
+
hash: -4234111217352214708
|
97
97
|
segments:
|
98
98
|
- 0
|
99
99
|
version: "0"
|