berater 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  describe Berater::Unlimiter do
2
- before { Berater.mode = :unlimited }
2
+ it_behaves_like 'a limiter', described_class.new
3
3
 
4
4
  describe '.new' do
5
5
  it 'initializes without any arguments or options' do
@@ -7,44 +7,24 @@ describe Berater::Unlimiter do
7
7
  end
8
8
 
9
9
  it 'initializes with any arguments and options' do
10
- expect(described_class.new(:abc, x: 123)).to be_a described_class
10
+ expect(described_class.new(:abc, :def, x: 123)).to be_a described_class
11
11
  end
12
12
 
13
13
  it 'has default values' do
14
- expect(described_class.new.key).to eq described_class.to_s
14
+ expect(described_class.new.key).to be :unlimiter
15
15
  expect(described_class.new.redis).to be Berater.redis
16
16
  end
17
17
  end
18
18
 
19
- describe '.limit' do
20
- it 'works' do
21
- expect(described_class.limit).to be_nil
22
- end
23
-
24
- it 'yields' do
25
- expect {|b| described_class.limit(&b) }.to yield_control
26
- end
27
-
28
- it 'is never overloaded' do
29
- 10.times do
30
- expect { described_class.limit }.not_to be_overloaded
31
- end
32
- end
33
-
34
- it 'works with any arguments or options' do
35
- expect(described_class.limit(:abc, x: 123)).to be_nil
36
- end
37
- end
38
-
39
19
  describe '#limit' do
40
20
  let(:limiter) { described_class.new }
41
21
 
42
22
  it 'works' do
43
- expect(limiter.limit).to be_nil
23
+ expect {|b| limiter.limit(&b) }.to yield_control
44
24
  end
45
25
 
46
- it 'yields' do
47
- expect {|b| limiter.limit(&b) }.to yield_control
26
+ it 'works without a block' do
27
+ expect(limiter.limit).to be_a Berater::Lock
48
28
  end
49
29
 
50
30
  it 'is never overloaded' do
@@ -52,10 +32,5 @@ describe Berater::Unlimiter do
52
32
  expect { limiter.limit }.not_to be_overloaded
53
33
  end
54
34
  end
55
-
56
- it 'works with any arguments or options' do
57
- expect(limiter.limit(x: 123)).to be_nil
58
- end
59
35
  end
60
-
61
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-04 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -115,18 +115,23 @@ extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
117
  - lib/berater.rb
118
- - lib/berater/base_limiter.rb
119
118
  - lib/berater/concurrency_limiter.rb
119
+ - lib/berater/dsl.rb
120
120
  - lib/berater/inhibitor.rb
121
+ - lib/berater/limiter.rb
122
+ - lib/berater/lock.rb
121
123
  - lib/berater/rate_limiter.rb
124
+ - lib/berater/rspec.rb
125
+ - lib/berater/rspec/matchers.rb
126
+ - lib/berater/test_mode.rb
122
127
  - lib/berater/unlimiter.rb
123
128
  - lib/berater/version.rb
124
129
  - spec/berater_spec.rb
125
130
  - spec/concurrency_limiter_spec.rb
126
- - spec/concurrency_lock_spec.rb
127
131
  - spec/inhibitor_spec.rb
128
- - spec/matcher_spec.rb
132
+ - spec/matchers_spec.rb
129
133
  - spec/rate_limiter_spec.rb
134
+ - spec/test_mode_spec.rb
130
135
  - spec/unlimiter_spec.rb
131
136
  homepage: https://github.com/dpep/berater_rb
132
137
  licenses:
@@ -153,9 +158,9 @@ specification_version: 4
153
158
  summary: Berater
154
159
  test_files:
155
160
  - spec/rate_limiter_spec.rb
156
- - spec/matcher_spec.rb
161
+ - spec/matchers_spec.rb
162
+ - spec/test_mode_spec.rb
157
163
  - spec/concurrency_limiter_spec.rb
158
- - spec/concurrency_lock_spec.rb
159
164
  - spec/berater_spec.rb
160
165
  - spec/inhibitor_spec.rb
161
166
  - spec/unlimiter_spec.rb
@@ -1,34 +0,0 @@
1
- module Berater
2
- class BaseLimiter
3
-
4
- class Overloaded < StandardError; end
5
-
6
- attr_reader :options
7
-
8
- def initialize(**opts)
9
- @options = opts
10
- end
11
-
12
- def key
13
- if options[:key]
14
- "#{self.class}:#{options[:key]}"
15
- else
16
- # default value
17
- self.class.to_s
18
- end
19
- end
20
-
21
- def redis
22
- options[:redis] || Berater.redis
23
- end
24
-
25
- def limit(**opts)
26
- raise NotImplementedError
27
- end
28
-
29
- def self.limit(*args, **opts, &block)
30
- self.new(*args, **opts).limit(&block)
31
- end
32
-
33
- end
34
- end
@@ -1,50 +0,0 @@
1
- describe Berater::ConcurrencyLimiter::Lock do
2
- subject { Berater.limit(1, timeout: 1) }
3
-
4
- before { Berater.mode = :concurrency }
5
-
6
- it { expect(subject.released?).to be false }
7
- it { expect(subject.expired?).to be false }
8
-
9
- context 'after being released' do
10
- before { subject.release }
11
-
12
- it { expect(subject.released?).to be true }
13
- it { expect(subject.expired?).to be false }
14
-
15
- it 'can not be released again' do
16
- expect { subject.release }.to raise_error(RuntimeError, /already/)
17
- end
18
- end
19
-
20
- context 'when enough time passes' do
21
- before { subject; Timecop.freeze(2) }
22
-
23
- it 'expires' do
24
- expect(subject.expired?).to be true
25
- end
26
-
27
- it 'fails to release' do
28
- expect { subject.release }.to raise_error(RuntimeError, /expired/)
29
- end
30
-
31
- it { expect(subject.released?).to be false }
32
- end
33
-
34
- describe '#contention' do
35
- let(:limiter) { Berater.new(:concurrency, 3) }
36
-
37
- it 'tracks contention' do
38
- lock_1 = limiter.limit
39
- expect(lock_1.contention).to eq 1
40
-
41
- lock_2 = limiter.limit
42
- expect(lock_2.contention).to eq 2
43
-
44
- limiter.limit do |lock_3|
45
- expect(lock_3.contention).to eq 3
46
- end
47
- end
48
- end
49
-
50
- end