wire 0.1.0 → 0.1.1
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.markdown +4 -2
- data/lib/wire.rb +7 -2
- data/spec/wire_spec.rb +32 -0
- data/wire.gemspec +1 -1
- metadata +3 -3
data/README.markdown
CHANGED
@@ -26,7 +26,7 @@ Time to run: ~ *1.2 seconds*.
|
|
26
26
|
|
27
27
|
This is how it works.
|
28
28
|
|
29
|
-
- 11 threads is created, done at time 0
|
29
|
+
- 11 threads is created, done at time 0.
|
30
30
|
- Running 10 threads, done at time 0.1
|
31
31
|
- Wait 1 second, done at time 1.1
|
32
32
|
- Start the 11th thread, done at time 1.2
|
@@ -88,7 +88,9 @@ Ingoing arguments to `new`.
|
|
88
88
|
- **max** (Integer) The maximum amount of threads to run a the same time. The value 10 will be used if `max` is nil or zero.
|
89
89
|
- **wait** (Integer) The time to wait before starting a new thread.
|
90
90
|
- **vars** (Array) A list of arguments to the block.
|
91
|
-
|
91
|
+
- **silent** (Boolean) The given block will not raise error if set to true. Default is false.
|
92
|
+
- **timeout** (Integer) The maximum time to run *one* thread, default is *no limit*.
|
93
|
+
|
92
94
|
## How do install
|
93
95
|
|
94
96
|
[sudo] gem install wire
|
data/lib/wire.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "thread"
|
2
2
|
require "monitor"
|
3
|
+
require "timeout"
|
3
4
|
|
4
5
|
class Wire < Thread
|
5
6
|
def self.counter
|
@@ -31,9 +32,9 @@ class Wire < Thread
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def runner
|
34
|
-
@block.call(*@vars)
|
35
|
+
@timeout ? Timeout::timeout(@timeout) { @block.call(*@vars) } : @block.call(*@vars)
|
35
36
|
rescue => error
|
36
|
-
|
37
|
+
report(error, "An error occurred: #{error.inspect}")
|
37
38
|
ensure
|
38
39
|
@counter.synchronize do
|
39
40
|
if @max == @counter.i or @counter.last
|
@@ -43,6 +44,10 @@ class Wire < Thread
|
|
43
44
|
@counter.cond.signal
|
44
45
|
end
|
45
46
|
end
|
47
|
+
|
48
|
+
def report(error, message)
|
49
|
+
@silent ? warn(message) : (raise error)
|
50
|
+
end
|
46
51
|
end
|
47
52
|
|
48
53
|
class Counter
|
data/spec/wire_spec.rb
CHANGED
@@ -99,5 +99,37 @@ describe Wire do
|
|
99
99
|
end.join
|
100
100
|
end.should raise_error(StandardError)
|
101
101
|
end
|
102
|
+
|
103
|
+
it "should be silent" do
|
104
|
+
lambda do
|
105
|
+
Wire.new(wait: 5, max: 1, silent: true) do
|
106
|
+
raise StandardError.new
|
107
|
+
end.join
|
108
|
+
end.should_not raise_error(StandardError)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should raise timeout error" do
|
112
|
+
lambda do
|
113
|
+
Wire.new(wait: 5, max: 1, timeout: 1) do
|
114
|
+
sleep 5
|
115
|
+
end.join
|
116
|
+
end.should raise_error(Timeout::Error)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should not raise timeout error" do
|
120
|
+
lambda do
|
121
|
+
Wire.new(wait: 5, silent: true, max: 1, timeout: 1) do
|
122
|
+
sleep 5
|
123
|
+
end.join
|
124
|
+
end.should_not raise_error(Timeout::Error)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should not raise timeout error" do
|
128
|
+
lambda do
|
129
|
+
Wire.new(wait: 5, silent: true, max: 1, timeout: 2) do
|
130
|
+
sleep 1
|
131
|
+
end.join
|
132
|
+
end.should_not raise_error(Timeout::Error)
|
133
|
+
end
|
102
134
|
end
|
103
135
|
end
|
data/wire.gemspec
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: wire
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Linus Oleander
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-29 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
requirements: []
|
68
68
|
|
69
69
|
rubyforge_project: wire
|
70
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.6.2
|
71
71
|
signing_key:
|
72
72
|
specification_version: 3
|
73
73
|
summary: Run a strict amount of threads during a time interval
|