berater 0.11.0 → 0.11.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.
- checksums.yaml +4 -4
- data/lib/berater/limiter.rb +21 -0
- data/lib/berater/version.rb +1 -1
- data/spec/concurrency_limiter_spec.rb +2 -1
- data/spec/limiter_spec.rb +38 -0
- data/spec/rate_limiter_spec.rb +3 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b657c7eb3a69e868416ad2d149d6c35ab13989e77a16ece23217deaaa9fafb6
|
4
|
+
data.tar.gz: 30835e7c247da37543ea118067d0b61d16080399ff6b53e86783c53a94b8d560
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '04481aa9c3097930f66eb29c6e19e650309b882cf835c1f36cb3fa58524b957d1194b5acc6a83f9fdfd33b73f888261ff3cc3171d730fd9781d9cf768b2b0768'
|
7
|
+
data.tar.gz: e0cd1a220a418face72685b44d23dff0040dc5f3d94b8c0d82769123a1c387e709702e8f093cc9c0f600d002ea4c848ebd4f0a2e38a4368e743104fbee597f9d
|
data/lib/berater/limiter.rb
CHANGED
@@ -27,10 +27,24 @@ module Berater
|
|
27
27
|
end
|
28
28
|
|
29
29
|
protected def inner_limit(capacity:, cost:)
|
30
|
+
if capacity.is_a?(String)
|
31
|
+
# try casting
|
32
|
+
begin
|
33
|
+
capacity = Float(capacity)
|
34
|
+
rescue ArgumentError; end
|
35
|
+
end
|
36
|
+
|
30
37
|
unless capacity.is_a?(Numeric) && capacity >= 0
|
31
38
|
raise ArgumentError, "invalid capacity: #{capacity}"
|
32
39
|
end
|
33
40
|
|
41
|
+
if cost.is_a?(String)
|
42
|
+
# try casting
|
43
|
+
begin
|
44
|
+
cost = Float(cost)
|
45
|
+
rescue ArgumentError; end
|
46
|
+
end
|
47
|
+
|
34
48
|
unless cost.is_a?(Numeric) && cost >= 0 && cost < Float::INFINITY
|
35
49
|
raise ArgumentError, "invalid cost: #{cost}"
|
36
50
|
end
|
@@ -76,6 +90,13 @@ module Berater
|
|
76
90
|
end
|
77
91
|
|
78
92
|
def capacity=(capacity)
|
93
|
+
if capacity.is_a?(String)
|
94
|
+
# try casting
|
95
|
+
begin
|
96
|
+
capacity = Float(capacity)
|
97
|
+
rescue TypeError, ArgumentError; end
|
98
|
+
end
|
99
|
+
|
79
100
|
unless capacity.is_a?(Numeric)
|
80
101
|
raise ArgumentError, "expected Numeric, found #{capacity.class}"
|
81
102
|
end
|
data/lib/berater/version.rb
CHANGED
@@ -24,6 +24,7 @@ describe Berater::ConcurrencyLimiter do
|
|
24
24
|
it { expect_capacity(0) }
|
25
25
|
it { expect_capacity(1) }
|
26
26
|
it { expect_capacity(1.5) }
|
27
|
+
it { expect_capacity('1.5') }
|
27
28
|
it { expect_capacity(10_000) }
|
28
29
|
|
29
30
|
context 'with erroneous values' do
|
@@ -34,7 +35,7 @@ describe Berater::ConcurrencyLimiter do
|
|
34
35
|
end
|
35
36
|
|
36
37
|
it { expect_bad_capacity(-1) }
|
37
|
-
it { expect_bad_capacity('
|
38
|
+
it { expect_bad_capacity('abc') }
|
38
39
|
it { expect_bad_capacity(:one) }
|
39
40
|
it { expect_bad_capacity(Float::INFINITY) }
|
40
41
|
end
|
data/spec/limiter_spec.rb
CHANGED
@@ -14,6 +14,32 @@ describe Berater::Limiter do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
describe '#capacity=' do
|
18
|
+
subject { Berater::Unlimiter.new(:key, capacity).capacity }
|
19
|
+
|
20
|
+
context 'when capacity is numeric' do
|
21
|
+
let(:capacity) { 3.5 }
|
22
|
+
|
23
|
+
it { is_expected.to be capacity }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when capacity is a stringified numeric' do
|
27
|
+
let(:capacity) { '3.5' }
|
28
|
+
|
29
|
+
it 'casts the value gracefully' do
|
30
|
+
is_expected.to be capacity.to_f
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when capacity is a bogus value' do
|
35
|
+
let(:capacity) { :abc }
|
36
|
+
|
37
|
+
it 'raises' do
|
38
|
+
expect { subject }.to raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
17
43
|
describe '#limit' do
|
18
44
|
subject { Berater::Unlimiter.new }
|
19
45
|
|
@@ -29,6 +55,12 @@ describe Berater::Limiter do
|
|
29
55
|
subject.limit(capacity: 'abc')
|
30
56
|
}.to raise_error(ArgumentError)
|
31
57
|
end
|
58
|
+
|
59
|
+
it 'handles stringified numerics gracefully' do
|
60
|
+
is_expected.to receive(:acquire_lock).with(3.5, anything)
|
61
|
+
|
62
|
+
subject.limit(capacity: '3.5')
|
63
|
+
end
|
32
64
|
end
|
33
65
|
|
34
66
|
context 'with a cost parameter' do
|
@@ -51,6 +83,12 @@ describe Berater::Limiter do
|
|
51
83
|
subject.limit(cost: Float::INFINITY)
|
52
84
|
}.to raise_error(ArgumentError)
|
53
85
|
end
|
86
|
+
|
87
|
+
it 'handles stringified numerics gracefully' do
|
88
|
+
is_expected.to receive(:acquire_lock).with(anything, 2.5)
|
89
|
+
|
90
|
+
subject.limit(cost: '2.5')
|
91
|
+
end
|
54
92
|
end
|
55
93
|
|
56
94
|
context 'when Berater.redis is nil' do
|
data/spec/rate_limiter_spec.rb
CHANGED
@@ -19,12 +19,13 @@ describe Berater::RateLimiter do
|
|
19
19
|
describe '#capacity' do
|
20
20
|
def expect_capacity(capacity)
|
21
21
|
limiter = described_class.new(:key, capacity, :second)
|
22
|
-
expect(limiter.capacity).to eq capacity
|
22
|
+
expect(limiter.capacity).to eq capacity.to_f
|
23
23
|
end
|
24
24
|
|
25
25
|
it { expect_capacity(0) }
|
26
26
|
it { expect_capacity(1) }
|
27
27
|
it { expect_capacity(1.5) }
|
28
|
+
it { expect_capacity('1.5') }
|
28
29
|
it { expect_capacity(100) }
|
29
30
|
|
30
31
|
context 'with erroneous values' do
|
@@ -35,7 +36,7 @@ describe Berater::RateLimiter do
|
|
35
36
|
end
|
36
37
|
|
37
38
|
it { expect_bad_capacity(-1) }
|
38
|
-
it { expect_bad_capacity('
|
39
|
+
it { expect_bad_capacity('abc') }
|
39
40
|
it { expect_bad_capacity(:one) }
|
40
41
|
it { expect_bad_capacity(Float::INFINITY) }
|
41
42
|
end
|