wire 0.1.1 → 0.1.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.markdown +2 -0
- data/lib/wire.rb +8 -2
- data/spec/wire_spec.rb +58 -25
- data/wire.gemspec +1 -1
- metadata +2 -2
data/README.markdown
CHANGED
@@ -90,6 +90,8 @@ Ingoing arguments to `new`.
|
|
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
92
|
- **timeout** (Integer) The maximum time to run *one* thread, default is *no limit*.
|
93
|
+
- **retries** (Integer) How many times should we retry? Default is 0.
|
94
|
+
- **delay** (Float) Time between each retry. Default is 0.
|
93
95
|
|
94
96
|
## How do install
|
95
97
|
|
data/lib/wire.rb
CHANGED
@@ -9,7 +9,7 @@ class Wire < Thread
|
|
9
9
|
|
10
10
|
def initialize(args, &block)
|
11
11
|
args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }
|
12
|
-
|
12
|
+
|
13
13
|
if @max.to_i <= 0 or @wait.nil?
|
14
14
|
warn "Both max and wait needs to be passed, where max > 0. Using default values"
|
15
15
|
@max = 10 if @max.to_i <= 0
|
@@ -18,6 +18,9 @@ class Wire < Thread
|
|
18
18
|
|
19
19
|
@block = block
|
20
20
|
@counter = Wire.counter
|
21
|
+
@retries ||= 0
|
22
|
+
@retry = 0
|
23
|
+
@delay ||= 0
|
21
24
|
|
22
25
|
@counter.synchronize do
|
23
26
|
@counter.cond.wait_until { @counter.i < @max }
|
@@ -33,7 +36,10 @@ class Wire < Thread
|
|
33
36
|
|
34
37
|
def runner
|
35
38
|
@timeout ? Timeout::timeout(@timeout) { @block.call(*@vars) } : @block.call(*@vars)
|
36
|
-
rescue => error
|
39
|
+
rescue StandardError => error
|
40
|
+
if (@retry += 1) < @retries
|
41
|
+
sleep @delay; retry
|
42
|
+
end
|
37
43
|
report(error, "An error occurred: #{error.inspect}")
|
38
44
|
ensure
|
39
45
|
@counter.synchronize do
|
data/spec/wire_spec.rb
CHANGED
@@ -19,36 +19,36 @@ describe Wire do
|
|
19
19
|
counter = Counter.new
|
20
20
|
Wire.should_receive(:counter).any_number_of_times.and_return(counter)
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
context "should be able to do run within the time limit" do
|
24
24
|
it "< max threads" do
|
25
25
|
start = time
|
26
26
|
runner(10, {max: 10, wait: 1}) do
|
27
27
|
sleep 0.1
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
(time - start).should < 1.15
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
it "> max threads" do
|
34
34
|
start = time
|
35
35
|
runner(11, {max: 10, wait: 1}) do
|
36
36
|
sleep 0.1
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
(time - start).should > 1.2
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it "should run using one thread" do
|
44
44
|
start = time
|
45
45
|
runner(1, {max: 1, wait: 1}) do
|
46
46
|
sleep 0.1
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
(time - start).should < 0.2
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
it "should run using one thread, using a high max value" do
|
53
53
|
start = time
|
54
54
|
runner(1, {max: 100, wait: 1}) do
|
@@ -57,16 +57,16 @@ describe Wire do
|
|
57
57
|
|
58
58
|
(time - start).should < 0.2
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
it "it should not wait" do
|
62
62
|
start = time
|
63
63
|
runner(11, {max: 10, wait: 0}) do
|
64
64
|
sleep 0.1
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
(time - start).should < 0.25
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
context "error" do
|
71
71
|
it "should use the default values if wrong arguments is being passed" do
|
72
72
|
w = Wire.new(max: 0) {}.join
|
@@ -75,7 +75,7 @@ describe Wire do
|
|
75
75
|
@wait.should == 1
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
it "should use the default values if nothing is being passed" do
|
80
80
|
w = Wire.new({}) {}.join
|
81
81
|
w.instance_eval do
|
@@ -83,7 +83,7 @@ describe Wire do
|
|
83
83
|
@wait.should == 1
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
it "should use the default values if wrong arguments is being passed" do
|
88
88
|
w = Wire.new(wait: 5) {}.join
|
89
89
|
w.instance_eval do
|
@@ -91,7 +91,7 @@ describe Wire do
|
|
91
91
|
@wait.should == 5
|
92
92
|
end
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it "should be possible to raise an error" do
|
96
96
|
lambda do
|
97
97
|
Wire.new(wait: 5, max: 1) do
|
@@ -99,7 +99,7 @@ describe Wire do
|
|
99
99
|
end.join
|
100
100
|
end.should raise_error(StandardError)
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
it "should be silent" do
|
104
104
|
lambda do
|
105
105
|
Wire.new(wait: 5, max: 1, silent: true) do
|
@@ -107,7 +107,7 @@ describe Wire do
|
|
107
107
|
end.join
|
108
108
|
end.should_not raise_error(StandardError)
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
it "should raise timeout error" do
|
112
112
|
lambda do
|
113
113
|
Wire.new(wait: 5, max: 1, timeout: 1) do
|
@@ -115,7 +115,7 @@ describe Wire do
|
|
115
115
|
end.join
|
116
116
|
end.should raise_error(Timeout::Error)
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
it "should not raise timeout error" do
|
120
120
|
lambda do
|
121
121
|
Wire.new(wait: 5, silent: true, max: 1, timeout: 1) do
|
@@ -123,13 +123,46 @@ describe Wire do
|
|
123
123
|
end.join
|
124
124
|
end.should_not raise_error(Timeout::Error)
|
125
125
|
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
134
|
+
|
135
|
+
it "should be able to retry - without raise an error" do
|
136
|
+
error = mock(Object.new)
|
137
|
+
error.should_receive(:new).exactly(10).times.and_return(StandardError)
|
138
|
+
lambda do
|
139
|
+
Wire.new(wait: 5, silent: true, max: 1, timeout: 2, vars: [error], retries: 10) do |e|
|
140
|
+
sleep 0.2
|
141
|
+
raise e.new
|
142
|
+
end.join
|
143
|
+
end.should_not raise_error(Timeout::Error)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should be able to retry" do
|
147
|
+
error = mock(Object.new)
|
148
|
+
error.should_receive(:new).exactly(10).times.and_return(StandardError)
|
149
|
+
lambda do
|
150
|
+
Wire.new(wait: 5, max: 1, timeout: 2, vars: [error], retries: 10) do |e|
|
151
|
+
sleep 0.2
|
152
|
+
raise e.new
|
153
|
+
end.join
|
154
|
+
end.should raise_error(StandardError)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should be able to retry with a delay" do
|
158
|
+
error = mock(Object.new)
|
159
|
+
error.should_receive(:new).exactly(10).times.and_return(StandardError)
|
160
|
+
lambda do
|
161
|
+
Wire.new(retries: 10, delay: 0.2, wait: 5, max: 1, timeout: 1, vars: [error]) do |e|
|
162
|
+
error.new
|
163
|
+
sleep 10
|
164
|
+
end.join
|
165
|
+
end.should raise_error(Timeout::Error)
|
166
|
+
end
|
134
167
|
end
|
135
|
-
end
|
168
|
+
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.2
|
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-
|
13
|
+
date: 2011-04-02 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|