berater 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,7 @@ describe Berater::ConcurrencyLimiter do
18
18
  describe '#capacity' do
19
19
  def expect_capacity(capacity)
20
20
  limiter = described_class.new(:key, capacity)
21
- expect(limiter.capacity).to eq capacity
21
+ expect(limiter.capacity).to eq capacity.to_i
22
22
  end
23
23
 
24
24
  it { expect_capacity(0) }
@@ -77,14 +77,14 @@ describe Berater::ConcurrencyLimiter do
77
77
  expect(limiter.limit).to be_a Berater::Lock
78
78
  expect(limiter.limit).to be_a Berater::Lock
79
79
 
80
- expect(limiter).to be_incapacitated
80
+ expect(limiter).to be_overloaded
81
81
  end
82
82
 
83
83
  context 'with capacity 0' do
84
84
  let(:limiter) { described_class.new(:key, 0) }
85
85
 
86
86
  it 'always fails' do
87
- expect(limiter).to be_incapacitated
87
+ expect(limiter).to be_overloaded
88
88
  end
89
89
  end
90
90
 
@@ -96,80 +96,80 @@ describe Berater::ConcurrencyLimiter do
96
96
 
97
97
  # since fractional cost is not supported
98
98
  expect(lock.capacity).to be 1
99
- expect(limiter).to be_incapacitated
99
+ expect(limiter).to be_overloaded
100
100
  end
101
101
  end
102
102
 
103
103
  it 'limit resets over time' do
104
104
  2.times { limiter.limit }
105
- expect(limiter).to be_incapacitated
105
+ expect(limiter).to be_overloaded
106
106
 
107
107
  Timecop.freeze(30)
108
108
 
109
109
  2.times { limiter.limit }
110
- expect(limiter).to be_incapacitated
110
+ expect(limiter).to be_overloaded
111
111
  end
112
112
 
113
113
  it 'limit resets with millisecond precision' do
114
114
  2.times { limiter.limit }
115
- expect(limiter).to be_incapacitated
115
+ expect(limiter).to be_overloaded
116
116
 
117
117
  # travel forward to just before first lock times out
118
118
  Timecop.freeze(29.999)
119
- expect(limiter).to be_incapacitated
119
+ expect(limiter).to be_overloaded
120
120
 
121
121
  # traveling one more millisecond will decrement the count
122
122
  Timecop.freeze(0.001)
123
123
  2.times { limiter.limit }
124
- expect(limiter).to be_incapacitated
124
+ expect(limiter).to be_overloaded
125
125
  end
126
126
 
127
127
  it 'accepts a dynamic capacity' do
128
- expect { limiter.limit(capacity: 0) }.to be_incapacitated
128
+ expect { limiter.limit(capacity: 0) }.to be_overloaded
129
129
  5.times { limiter.limit(capacity: 10) }
130
- expect { limiter }.to be_incapacitated
130
+ expect { limiter }.to be_overloaded
131
131
  end
132
132
 
133
133
  context 'with cost parameter' do
134
- it { expect { limiter.limit(cost: 4) }.to be_incapacitated }
134
+ it { expect { limiter.limit(cost: 4) }.to be_overloaded }
135
135
 
136
136
  it 'works within limit' do
137
137
  limiter.limit(cost: 2)
138
- expect(limiter).to be_incapacitated
138
+ expect(limiter).to be_overloaded
139
139
  end
140
140
 
141
141
  it 'releases full cost' do
142
142
  lock = limiter.limit(cost: 2)
143
- expect(limiter).to be_incapacitated
143
+ expect(limiter).to be_overloaded
144
144
 
145
145
  lock.release
146
- expect(limiter).not_to be_incapacitated
146
+ expect(limiter).not_to be_overloaded
147
147
 
148
148
  lock = limiter.limit(cost: 2)
149
- expect(limiter).to be_incapacitated
149
+ expect(limiter).to be_overloaded
150
150
  end
151
151
 
152
152
  it 'respects timeout' do
153
153
  limiter.limit(cost: 2)
154
- expect(limiter).to be_incapacitated
154
+ expect(limiter).to be_overloaded
155
155
 
156
156
  Timecop.freeze(30)
157
- expect(limiter).not_to be_incapacitated
157
+ expect(limiter).not_to be_overloaded
158
158
 
159
159
  limiter.limit(cost: 2)
160
- expect(limiter).to be_incapacitated
160
+ expect(limiter).to be_overloaded
161
161
  end
162
162
 
163
163
  context 'with fractional costs' do
164
164
  it 'rounds up' do
165
165
  limiter.limit(cost: 1.5)
166
- expect(limiter).to be_incapacitated
166
+ expect(limiter).to be_overloaded
167
167
  end
168
168
 
169
169
  it 'accumulates correctly' do
170
170
  limiter.limit(cost: 0.5) # => 1
171
171
  limiter.limit(cost: 0.7) # => 2
172
- expect(limiter).to be_incapacitated
172
+ expect(limiter).to be_overloaded
173
173
  end
174
174
  end
175
175
 
@@ -187,8 +187,8 @@ describe Berater::ConcurrencyLimiter do
187
187
  it 'works as expected' do
188
188
  expect(limiter_one.limit).to be_a Berater::Lock
189
189
 
190
- expect(limiter_one).to be_incapacitated
191
- expect(limiter_two).to be_incapacitated
190
+ expect(limiter_one).to be_overloaded
191
+ expect(limiter_two).to be_overloaded
192
192
  end
193
193
  end
194
194
 
@@ -202,22 +202,22 @@ describe Berater::ConcurrencyLimiter do
202
202
  one_lock = limiter_one.limit
203
203
  expect(one_lock).to be_a Berater::Lock
204
204
 
205
- expect(limiter_one).to be_incapacitated
206
- expect(limiter_two).not_to be_incapacitated
205
+ expect(limiter_one).to be_overloaded
206
+ expect(limiter_two).not_to be_overloaded
207
207
 
208
208
  two_lock = limiter_two.limit
209
209
  expect(two_lock).to be_a Berater::Lock
210
210
 
211
- expect(limiter_one).to be_incapacitated
212
- expect(limiter_two).to be_incapacitated
211
+ expect(limiter_one).to be_overloaded
212
+ expect(limiter_two).to be_overloaded
213
213
 
214
214
  one_lock.release
215
- expect(limiter_one).to be_incapacitated
216
- expect(limiter_two).not_to be_incapacitated
215
+ expect(limiter_one).to be_overloaded
216
+ expect(limiter_two).not_to be_overloaded
217
217
 
218
218
  two_lock.release
219
- expect(limiter_one).not_to be_incapacitated
220
- expect(limiter_two).not_to be_incapacitated
219
+ expect(limiter_one).not_to be_overloaded
220
+ expect(limiter_two).not_to be_overloaded
221
221
  end
222
222
  end
223
223
 
@@ -226,41 +226,41 @@ describe Berater::ConcurrencyLimiter do
226
226
  let(:limiter_two) { described_class.new(:two, 1) }
227
227
 
228
228
  it 'works as expected' do
229
- expect(limiter_one).not_to be_incapacitated
230
- expect(limiter_two).not_to be_incapacitated
229
+ expect(limiter_one).not_to be_overloaded
230
+ expect(limiter_two).not_to be_overloaded
231
231
 
232
232
  one_lock = limiter_one.limit
233
233
  expect(one_lock).to be_a Berater::Lock
234
234
 
235
- expect(limiter_one).to be_incapacitated
236
- expect(limiter_two).not_to be_incapacitated
235
+ expect(limiter_one).to be_overloaded
236
+ expect(limiter_two).not_to be_overloaded
237
237
 
238
238
  two_lock = limiter_two.limit
239
239
  expect(two_lock).to be_a Berater::Lock
240
240
 
241
- expect(limiter_one).to be_incapacitated
242
- expect(limiter_two).to be_incapacitated
241
+ expect(limiter_one).to be_overloaded
242
+ expect(limiter_two).to be_overloaded
243
243
  end
244
244
  end
245
245
  end
246
246
 
247
- describe '#overloaded?' do
248
- let(:limiter) { described_class.new(:key, 1, timeout: 30) }
247
+ describe '#utilization' do
248
+ let(:limiter) { described_class.new(:key, 10, timeout: 30) }
249
249
 
250
250
  it 'works' do
251
- expect(limiter.overloaded?).to be false
252
- lock = limiter.limit
253
- expect(limiter.overloaded?).to be true
254
- lock.release
255
- expect(limiter.overloaded?).to be false
256
- end
251
+ expect(limiter.utilization).to be 0.0
257
252
 
258
- it 'respects timeout' do
259
- expect(limiter.overloaded?).to be false
260
- lock = limiter.limit
261
- expect(limiter.overloaded?).to be true
262
- Timecop.freeze(30)
263
- expect(limiter.overloaded?).to be false
253
+ 2.times { limiter.limit }
254
+ expect(limiter.utilization).to be 0.2
255
+
256
+ Timecop.freeze(15)
257
+
258
+ 8.times { limiter.limit }
259
+ expect(limiter.utilization).to be 1.0
260
+
261
+ Timecop.freeze(15)
262
+
263
+ expect(limiter.utilization).to be 0.8
264
264
  end
265
265
  end
266
266
 
@@ -3,18 +3,6 @@ require 'berater/dsl'
3
3
  describe Berater do
4
4
  using Berater::DSL
5
5
 
6
- it 'instatiates an Unlimiter' do
7
- limiter = Berater.new(:key) { unlimited }
8
- expect(limiter).to be_a Berater::Unlimiter
9
- expect(limiter.key).to be :key
10
- end
11
-
12
- it 'instatiates an Inhibiter' do
13
- limiter = Berater.new(:key) { inhibited }
14
- expect(limiter).to be_a Berater::Inhibitor
15
- expect(limiter.key).to be :key
16
- end
17
-
18
6
  it 'instatiates a RateLimiter' do
19
7
  limiter = Berater.new(:key) { 1.per second }
20
8
  expect(limiter).to be_a Berater::RateLimiter
data/spec/dsl_spec.rb CHANGED
@@ -13,13 +13,13 @@ describe Berater::DSL do
13
13
  end
14
14
 
15
15
  it 'parses' do
16
- check([ 1, :second ]) { 1.per second }
17
- check([ 3, :minute ]) { 3.per minute }
18
- check([ 5, :hour ]) { 5.every hour }
16
+ check([ 1, interval: :second ]) { 1.per second }
17
+ check([ 3, interval: :minute ]) { 3.per minute }
18
+ check([ 5, interval: :hour ]) { 5.every hour }
19
19
  end
20
20
 
21
21
  it 'cleans up afterward' do
22
- check([ 1, :second ]) { 1.per second }
22
+ check([ 1, interval: :second ]) { 1.per second }
23
23
 
24
24
  expect(Integer).not_to respond_to(:per)
25
25
  expect(Integer).not_to respond_to(:every)
@@ -29,7 +29,7 @@ describe Berater::DSL do
29
29
  count = 1
30
30
  interval = :second
31
31
 
32
- check([ count, interval ]) { count.per interval }
32
+ check([ count, interval: interval ]) { count.per interval }
33
33
  end
34
34
  end
35
35
 
@@ -57,16 +57,4 @@ describe Berater::DSL do
57
57
  end
58
58
  end
59
59
 
60
- context 'unlimited mode' do
61
- it 'has keywords' do
62
- check(:unlimited) { unlimited }
63
- end
64
- end
65
-
66
- context 'inhibited mode' do
67
- it 'has keywords' do
68
- check(:inhibited) { inhibited }
69
- end
70
- end
71
-
72
60
  end
data/spec/limiter_spec.rb CHANGED
@@ -8,7 +8,7 @@ describe Berater::Limiter do
8
8
 
9
9
  it do
10
10
  expect { limiter.limit }.to raise_error(NotImplementedError)
11
- expect { limiter.overloaded? }.to raise_error(NotImplementedError)
11
+ expect { limiter.utilization }.to raise_error(NotImplementedError)
12
12
  end
13
13
  end
14
14
 
@@ -1,118 +1,80 @@
1
1
  describe Berater::Matchers::Overloaded do
2
2
 
3
3
  context 'Berater::Unlimiter' do
4
- let(:limiter) { Berater.new(:key, :unlimited) }
4
+ let(:limiter) { Berater::Unlimiter.new }
5
5
 
6
6
  it { expect(limiter).not_to be_overloaded }
7
- it { expect(limiter).not_to be_inhibited }
8
- it { expect(limiter).not_to be_overrated }
9
- it { expect(limiter).not_to be_incapacitated }
10
-
11
7
  it { expect { limiter }.not_to be_overloaded }
12
- it { expect { limiter }.not_to be_inhibited }
13
- it { expect { limiter }.not_to be_overrated }
14
- it { expect { limiter }.not_to be_incapacitated }
15
-
16
8
  it { expect { limiter.limit }.not_to be_overloaded }
17
- it { expect { limiter.limit }.not_to be_inhibited }
18
- it { expect { limiter.limit }.not_to be_overrated }
19
- it { expect { limiter.limit }.not_to be_incapacitated }
20
9
  end
21
10
 
22
11
  context 'Berater::Inhibitor' do
23
- let(:limiter) { Berater.new(:key, :inhibited) }
12
+ let(:limiter) { Berater::Inhibitor.new }
24
13
 
25
14
  it { expect(limiter).to be_overloaded }
26
- it { expect(limiter).to be_inhibited }
27
-
28
15
  it { expect { limiter }.to be_overloaded }
29
- it { expect { limiter }.to be_inhibited }
30
-
31
16
  it { expect { limiter.limit }.to be_overloaded }
32
- it { expect { limiter.limit }.to be_inhibited }
33
17
  end
34
18
 
35
19
  context 'Berater::RateLimiter' do
36
- let(:limiter) { Berater.new(:key, 1, :second) }
20
+ let(:limiter) { Berater::RateLimiter.new(:key, 1, :second) }
37
21
 
38
22
  it { expect(limiter).not_to be_overloaded }
39
- it { expect(limiter).not_to be_inhibited }
40
- it { expect(limiter).not_to be_overrated }
41
- it { expect(limiter).not_to be_incapacitated }
42
-
43
23
  it { expect { limiter }.not_to be_overloaded }
44
- it { expect { limiter }.not_to be_inhibited }
45
- it { expect { limiter }.not_to be_overrated }
46
- it { expect { limiter }.not_to be_incapacitated }
47
-
48
24
  it { expect { limiter.limit }.not_to be_overloaded }
49
- it { expect { limiter.limit }.not_to be_inhibited }
50
- it { expect { limiter.limit }.not_to be_overrated }
51
- it { expect { limiter.limit }.not_to be_incapacitated }
52
25
 
53
26
  context 'once limit is used up' do
54
27
  before { limiter.limit }
55
28
 
56
- it 'should be_overrated' do
57
- expect(limiter).to be_overrated
29
+ it 'should be_overloaded' do
30
+ expect(limiter).to be_overloaded
58
31
  end
59
32
 
60
- it 'should be_overrated' do
61
- expect { limiter }.to be_overrated
33
+ it 'should be_overloaded' do
34
+ expect { limiter }.to be_overloaded
62
35
  end
63
36
 
64
- it 'should be_overrated' do
65
- expect { limiter.limit }.to be_overrated
37
+ it 'should be_overloaded' do
38
+ expect { limiter.limit }.to be_overloaded
66
39
  end
67
40
  end
68
41
  end
69
42
 
70
43
  context 'Berater::ConcurrencyLimiter' do
71
- let(:limiter) { Berater.new(:key, 1) }
44
+ let(:limiter) { Berater::ConcurrencyLimiter.new(:key, 1) }
72
45
 
73
46
  it { expect(limiter).not_to be_overloaded }
74
- it { expect(limiter).not_to be_inhibited }
75
- it { expect(limiter).not_to be_overrated }
76
- it { expect(limiter).not_to be_incapacitated }
77
-
78
47
  it { expect { limiter }.not_to be_overloaded }
79
- it { expect { limiter }.not_to be_inhibited }
80
- it { expect { limiter }.not_to be_overrated }
81
- it { expect { limiter }.not_to be_incapacitated }
82
-
83
48
  it { expect { limiter.limit }.not_to be_overloaded }
84
- it { expect { limiter.limit }.not_to be_inhibited }
85
- it { expect { limiter.limit }.not_to be_overrated }
86
- it { expect { limiter.limit }.not_to be_incapacitated }
87
49
 
88
50
  context 'when lock is released' do
89
- it 'should be_incapacitated' do
51
+ it 'should be_overloaded' do
90
52
  3.times do
91
- expect(limiter).not_to be_incapacitated
53
+ expect(limiter).not_to be_overloaded
92
54
  end
93
55
  end
94
56
 
95
- it 'should be_incapacitated' do
57
+ it 'should be_overloaded' do
96
58
  3.times do
97
- expect { limiter }.not_to be_incapacitated
59
+ expect { limiter }.not_to be_overloaded
98
60
  end
99
61
  end
100
62
 
101
- it 'should be_incapacitated' do
63
+ it 'should be_overloaded' do
102
64
  3.times do
103
- expect { limiter.limit {} }.not_to be_incapacitated
65
+ expect { limiter.limit {} }.not_to be_overloaded
104
66
  end
105
67
  end
106
68
  end
107
69
 
108
70
  context 'when lock is *not* released' do
109
- it 'should be_incapacitated' do
110
- expect { limiter.limit }.not_to be_incapacitated
111
- expect { limiter.limit }.to be_incapacitated
71
+ it 'should be_overloaded' do
72
+ expect { limiter.limit }.not_to be_overloaded
73
+ expect { limiter.limit }.to be_overloaded
112
74
  end
113
75
 
114
- it 'should be_incapacitated' do
115
- expect { 3.times { limiter.limit } }.to be_incapacitated
76
+ it 'should be_overloaded' do
77
+ expect { 3.times { limiter.limit } }.to be_overloaded
116
78
  end
117
79
  end
118
80
  end
@@ -156,31 +118,5 @@ describe Berater::Matchers::Overloaded do
156
118
  expect { raise Berater::Overloaded }.not_to be_overloaded
157
119
  }.to fail_including("did not expect #{Berater::Overloaded} to be raised")
158
120
  end
159
-
160
- it 'supports different verbs' do
161
- expect {
162
- expect { unlimiter }.to be_overrated
163
- }.to fail_including('expected to be overrated')
164
-
165
- expect {
166
- expect { unlimiter }.to be_incapacitated
167
- }.to fail_including('expected to be incapacitated')
168
- end
169
-
170
- it 'supports different exceptions' do
171
- expect {
172
- expect { 123 }.to be_overrated
173
- }.to fail_including(
174
- "expected #{Berater::RateLimiter::Overrated} to be raised"
175
- )
176
-
177
- expect {
178
- expect {
179
- raise Berater::ConcurrencyLimiter::Incapacitated
180
- }.not_to be_incapacitated
181
- }.to fail_including(
182
- "did not expect #{Berater::ConcurrencyLimiter::Incapacitated} to be raised"
183
- )
184
- end
185
121
  end
186
122
  end