lhc 12.2.0 → 13.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rubocop.yml +27 -0
  3. data/.github/workflows/test.yml +27 -0
  4. data/.rubocop.yml +3 -0
  5. data/.ruby-version +1 -1
  6. data/Gemfile.activesupport5 +1 -1
  7. data/Gemfile.activesupport6 +1 -1
  8. data/README.md +67 -6
  9. data/Rakefile +3 -3
  10. data/lhc.gemspec +3 -2
  11. data/lib/lhc/error.rb +3 -1
  12. data/lib/lhc/interceptor.rb +4 -0
  13. data/lib/lhc/interceptors/auth.rb +0 -4
  14. data/lib/lhc/interceptors/caching.rb +65 -44
  15. data/lib/lhc/interceptors/monitoring.rb +39 -10
  16. data/lib/lhc/interceptors/throttle.rb +9 -8
  17. data/lib/lhc/railtie.rb +0 -1
  18. data/lib/lhc/request.rb +7 -3
  19. data/lib/lhc/rspec.rb +1 -2
  20. data/lib/lhc/version.rb +1 -1
  21. data/spec/error/to_s_spec.rb +7 -2
  22. data/spec/formats/multipart_spec.rb +1 -1
  23. data/spec/formats/plain_spec.rb +1 -1
  24. data/spec/interceptors/after_response_spec.rb +1 -1
  25. data/spec/interceptors/caching/main_spec.rb +2 -2
  26. data/spec/interceptors/caching/multilevel_cache_spec.rb +139 -0
  27. data/spec/interceptors/caching/options_spec.rb +0 -11
  28. data/spec/interceptors/monitoring/caching_spec.rb +66 -0
  29. data/spec/interceptors/response_competition_spec.rb +2 -2
  30. data/spec/interceptors/return_response_spec.rb +2 -2
  31. data/spec/interceptors/throttle/main_spec.rb +95 -21
  32. data/spec/spec_helper.rb +1 -0
  33. metadata +27 -20
  34. data/Gemfile.activesupport4 +0 -4
  35. data/cider-ci.yml +0 -6
  36. data/cider-ci/bin/bundle +0 -51
  37. data/cider-ci/bin/ruby_install +0 -8
  38. data/cider-ci/bin/ruby_version +0 -25
  39. data/cider-ci/jobs/rspec-activesupport-4.yml +0 -28
  40. data/cider-ci/jobs/rspec-activesupport-5.yml +0 -27
  41. data/cider-ci/jobs/rspec-activesupport-6.yml +0 -28
  42. data/cider-ci/jobs/rubocop.yml +0 -18
  43. data/cider-ci/task_components/bundle.yml +0 -22
  44. data/cider-ci/task_components/rspec.yml +0 -36
  45. data/cider-ci/task_components/rubocop.yml +0 -29
  46. data/cider-ci/task_components/ruby.yml +0 -15
@@ -12,7 +12,7 @@ describe LHC do
12
12
 
13
13
  def before_request
14
14
  if @@cached
15
- return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from local cache'), nil)
15
+ return LHC::Response.new(Typhoeus::Response.new(response_code: 200, return_code: :ok, response_body: 'Im served from local cache'), nil)
16
16
  end
17
17
  end
18
18
  end
@@ -22,7 +22,7 @@ describe LHC do
22
22
 
23
23
  def before_request
24
24
  if request.response.nil?
25
- return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from remote cache'), nil)
25
+ return LHC::Response.new(Typhoeus::Response.new(response_code: 200, return_code: :ok, response_body: 'Im served from remote cache'), nil)
26
26
  end
27
27
  end
28
28
  end
@@ -8,7 +8,7 @@ describe LHC do
8
8
  class CacheInterceptor < LHC::Interceptor
9
9
 
10
10
  def before_request
11
- LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
11
+ LHC::Response.new(Typhoeus::Response.new(response_code: 200, return_code: :ok, response_body: 'Im served from cache'), nil)
12
12
  end
13
13
  end
14
14
  LHC.configure { |c| c.interceptors = [CacheInterceptor] }
@@ -23,7 +23,7 @@ describe LHC do
23
23
  before(:each) do
24
24
  class AnotherInterceptor < LHC::Interceptor
25
25
  def before_request
26
- LHC::Response.new(Typhoeus::Response.new({}), nil)
26
+ LHC::Response.new(Typhoeus::Response.new(response_code: 200, return_code: :ok), nil)
27
27
  end
28
28
  end
29
29
  end
@@ -100,35 +100,95 @@ describe LHC::Throttle do
100
100
  end
101
101
  end
102
102
 
103
- describe 'calculate "remaining" in proc' do
104
- let(:quota_current) { 8100 }
105
- let(:options_remaining) do
106
- ->(response) { (response.headers['limit']).to_i - (response.headers['current']).to_i }
103
+ describe 'configuration values as Procs' do
104
+ describe 'calculate "limit" in proc' do
105
+ let(:options_limit) do
106
+ ->(*) { 10_000 }
107
+ end
108
+
109
+ before(:each) do
110
+ LHC.get('http://local.ch', options)
111
+ end
112
+
113
+ context 'breaks' do
114
+ let(:options_break) { '80%' }
115
+
116
+ it 'hit the breaks if throttling quota is reached' do
117
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
118
+ LHC::Throttle::OutOfQuota,
119
+ 'Reached predefined quota for local.ch'
120
+ )
121
+ end
122
+
123
+ context 'still within quota' do
124
+ let(:options_break) { '90%' }
125
+
126
+ it 'does not hit the breaks' do
127
+ LHC.get('http://local.ch', options)
128
+ end
129
+ end
130
+ end
107
131
  end
108
132
 
109
- before(:each) do
110
- stub_request(:get, 'http://local.ch').to_return(
111
- headers: { 'limit' => quota_limit, 'current' => quota_current, 'reset' => quota_reset }
112
- )
113
- LHC.get('http://local.ch', options)
133
+ describe 'calculate "remaining" in proc' do
134
+ let(:quota_current) { 8100 }
135
+ let(:options_remaining) do
136
+ ->(response) { (response.headers['limit']).to_i - (response.headers['current']).to_i }
137
+ end
138
+
139
+ before(:each) do
140
+ stub_request(:get, 'http://local.ch').to_return(
141
+ headers: { 'limit' => quota_limit, 'current' => quota_current, 'reset' => quota_reset }
142
+ )
143
+ LHC.get('http://local.ch', options)
144
+ end
145
+
146
+ context 'breaks' do
147
+ let(:options_break) { '80%' }
148
+
149
+ it 'hit the breaks if throttling quota is reached' do
150
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
151
+ LHC::Throttle::OutOfQuota,
152
+ 'Reached predefined quota for local.ch'
153
+ )
154
+ end
155
+
156
+ context 'still within quota' do
157
+ let(:options_break) { '90%' }
158
+
159
+ it 'does not hit the breaks' do
160
+ LHC.get('http://local.ch', options)
161
+ end
162
+ end
163
+ end
114
164
  end
115
165
 
116
- context 'breaks' do
117
- let(:options_break) { '80%' }
166
+ describe 'calculate "reset" in proc' do
167
+ let(:options_expires) { ->(*) { Time.zone.now + 1.second } }
118
168
 
119
- it 'hit the breaks if throttling quota is reached' do
120
- expect { LHC.get('http://local.ch', options) }.to raise_error(
121
- LHC::Throttle::OutOfQuota,
122
- 'Reached predefined quota for local.ch'
169
+ before(:each) do
170
+ stub_request(:get, 'http://local.ch').to_return(
171
+ headers: { 'limit' => quota_limit, 'remaining' => quota_remaining }
123
172
  )
173
+ LHC.get('http://local.ch', options)
124
174
  end
125
175
 
126
- context 'still within quota' do
127
- let(:options_break) { '90%' }
176
+ context 'breaks' do
177
+ let(:options_break) { '80%' }
128
178
 
129
- it 'does not hit the breaks' do
130
- LHC.get('http://local.ch', options)
131
- LHC.get('http://local.ch', options)
179
+ it 'hit the breaks if throttling quota is reached' do
180
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
181
+ LHC::Throttle::OutOfQuota,
182
+ 'Reached predefined quota for local.ch'
183
+ )
184
+ end
185
+
186
+ context 'still within quota' do
187
+ let(:options_break) { '90%' }
188
+
189
+ it 'does not hit the breaks' do
190
+ LHC.get('http://local.ch', options)
191
+ end
132
192
  end
133
193
  end
134
194
  end
@@ -154,9 +214,23 @@ describe LHC::Throttle do
154
214
 
155
215
  it 'does not hit the breaks' do
156
216
  LHC.get('http://local.ch', options)
157
- LHC.get('http://local.ch', options)
158
217
  end
159
218
  end
160
219
  end
161
220
  end
221
+
222
+ context 'when value is empty' do
223
+ let(:quota_reset) { nil }
224
+
225
+ before do
226
+ stub_request(:get, 'http://local.ch').to_return(
227
+ headers: { 'limit' => quota_limit, 'remaining' => quota_remaining }
228
+ )
229
+ LHC.get('http://local.ch', options)
230
+ end
231
+
232
+ it 'still runs' do
233
+ LHC.get('http://local.ch', options)
234
+ end
235
+ end
162
236
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'pry'
4
4
  require 'webmock/rspec'
5
5
  require 'lhc'
6
+ require 'lhc/rspec'
6
7
  require 'timecop'
7
8
 
8
9
  Dir[File.join(__dir__, "support/**/*.rb")].each { |f| require f }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 12.2.0
4
+ version: 13.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-24 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '4.2'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: addressable
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,28 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '4.2'
103
+ version: '5.2'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '4.2'
110
+ version: '5.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rspec-rails
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -188,29 +202,18 @@ executables: []
188
202
  extensions: []
189
203
  extra_rdoc_files: []
190
204
  files:
205
+ - ".github/workflows/rubocop.yml"
206
+ - ".github/workflows/test.yml"
191
207
  - ".gitignore"
192
208
  - ".rubocop.localch.yml"
193
209
  - ".rubocop.yml"
194
210
  - ".ruby-version"
195
211
  - Gemfile
196
- - Gemfile.activesupport4
197
212
  - Gemfile.activesupport5
198
213
  - Gemfile.activesupport6
199
214
  - LICENSE
200
215
  - README.md
201
216
  - Rakefile
202
- - cider-ci.yml
203
- - cider-ci/bin/bundle
204
- - cider-ci/bin/ruby_install
205
- - cider-ci/bin/ruby_version
206
- - cider-ci/jobs/rspec-activesupport-4.yml
207
- - cider-ci/jobs/rspec-activesupport-5.yml
208
- - cider-ci/jobs/rspec-activesupport-6.yml
209
- - cider-ci/jobs/rubocop.yml
210
- - cider-ci/task_components/bundle.yml
211
- - cider-ci/task_components/rspec.yml
212
- - cider-ci/task_components/rubocop.yml
213
- - cider-ci/task_components/ruby.yml
214
217
  - friday.yml
215
218
  - lhc.gemspec
216
219
  - lib/core_ext/hash/deep_transform_values.rb
@@ -335,6 +338,7 @@ files:
335
338
  - spec/interceptors/caching/hydra_spec.rb
336
339
  - spec/interceptors/caching/main_spec.rb
337
340
  - spec/interceptors/caching/methods_spec.rb
341
+ - spec/interceptors/caching/multilevel_cache_spec.rb
338
342
  - spec/interceptors/caching/options_spec.rb
339
343
  - spec/interceptors/caching/parameters_spec.rb
340
344
  - spec/interceptors/caching/response_status_spec.rb
@@ -344,6 +348,7 @@ files:
344
348
  - spec/interceptors/define_spec.rb
345
349
  - spec/interceptors/dup_spec.rb
346
350
  - spec/interceptors/logging/main_spec.rb
351
+ - spec/interceptors/monitoring/caching_spec.rb
347
352
  - spec/interceptors/monitoring/main_spec.rb
348
353
  - spec/interceptors/prometheus_spec.rb
349
354
  - spec/interceptors/response_competition_spec.rb
@@ -405,7 +410,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
405
410
  version: '0'
406
411
  requirements:
407
412
  - Ruby >= 2.0.0
408
- rubygems_version: 3.0.3
413
+ rubygems_version: 3.1.4
409
414
  signing_key:
410
415
  specification_version: 4
411
416
  summary: Advanced HTTP Client for Ruby, fueled with interceptors
@@ -488,6 +493,7 @@ test_files:
488
493
  - spec/interceptors/caching/hydra_spec.rb
489
494
  - spec/interceptors/caching/main_spec.rb
490
495
  - spec/interceptors/caching/methods_spec.rb
496
+ - spec/interceptors/caching/multilevel_cache_spec.rb
491
497
  - spec/interceptors/caching/options_spec.rb
492
498
  - spec/interceptors/caching/parameters_spec.rb
493
499
  - spec/interceptors/caching/response_status_spec.rb
@@ -497,6 +503,7 @@ test_files:
497
503
  - spec/interceptors/define_spec.rb
498
504
  - spec/interceptors/dup_spec.rb
499
505
  - spec/interceptors/logging/main_spec.rb
506
+ - spec/interceptors/monitoring/caching_spec.rb
500
507
  - spec/interceptors/monitoring/main_spec.rb
501
508
  - spec/interceptors/prometheus_spec.rb
502
509
  - spec/interceptors/response_competition_spec.rb
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org/'
2
-
3
- gemspec
4
- gem 'activesupport', '~> 4.2.11'
data/cider-ci.yml DELETED
@@ -1,6 +0,0 @@
1
- jobs:
2
- include:
3
- - cider-ci/jobs/rspec-activesupport-4.yml
4
- - cider-ci/jobs/rspec-activesupport-5.yml
5
- - cider-ci/jobs/rspec-activesupport-6.yml
6
- - cider-ci/jobs/rubocop.yml
data/cider-ci/bin/bundle DELETED
@@ -1,51 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -eux
3
-
4
- export PATH=~/.rubies/$RUBY/bin:$PATH
5
- rm -f .bundle/config
6
-
7
- if [ ! -f ~/.rubies/$RUBY/bin/bundle ]; then
8
- gem install bundler
9
- fi
10
-
11
- # install bundler v. 1.17.3 in order to be able to run the tests with
12
- # ACTIVESUPPORT=4 because rails (= 4.2.0) depends on bundler (< 2.0, >= 1.3.0)
13
- gem install bundler:1.17.3
14
-
15
- sed "s/^source 'https:\/\/rubygems\.intra\.local\.ch'*/source 'http\:\/\/52.29.7.59:9292'/g" Gemfile > Gemfile.tmp
16
- mv Gemfile.tmp Gemfile
17
-
18
- DIGEST=$(git ls-tree HEAD --\
19
- cider-ci.yml cider-ci Gemfile.lock \
20
- | openssl dgst -sha1 | cut -d ' ' -f 2)
21
-
22
- if [ ! -z ${ACTIVESUPPORT:-} ]; then
23
- DIGEST=$(echo "$DIGEST $ACTIVESUPPORT")
24
- fi
25
-
26
- DIGEST=$(echo "$DIGEST $PATH" \
27
- | openssl dgst -sha1 | cut -d ' ' -f 2)
28
-
29
- echo "DIGEST"
30
- echo "${DIGEST}"
31
-
32
- CACHE_SIGNATURE_FILE="/tmp/bundle_cache_signature_${DIGEST}"
33
-
34
- if [ ! -f $CACHE_SIGNATURE_FILE ] ; then
35
- if [ ! -z ${ACTIVESUPPORT:-} ]; then
36
- if [ ! -z ${BUNDLER:-} ]; then
37
- echo "BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle $BUNDLER install"
38
- BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle $BUNDLER install
39
- else
40
- echo "BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle install"
41
- BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle install
42
- fi
43
- else
44
- echo "bundle install"
45
- bundle $BUNDLER install
46
- fi
47
- touch $CACHE_SIGNATURE_FILE
48
- fi
49
-
50
- echo "bundle install"
51
- bundle $BUNDLER install
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -eux
3
-
4
- export PATH=~/.rubies/$RUBY/bin:$PATH
5
-
6
- if [ ! -d ~/.rubies/$RUBY ]; then
7
- ruby-install --no-install-deps $RUBY
8
- fi
@@ -1,25 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -eux
3
-
4
- if [ -f ./.ruby-version ]; then
5
- echo ".ruby-version file found"
6
- fi
7
-
8
- if [ ! -f ./.ruby-version ]; then
9
- echo ".ruby-version file not found"
10
- exit 1
11
- fi
12
-
13
- IFS='-' read -ra EXPLODED_RUBY <<< "$RUBY"
14
-
15
- if [ "${#EXPLODED_RUBY[@]}" == "1" ]; then
16
- echo 'No engine/version separator "-" found in .ruby-version file.'
17
- exit 1
18
- fi
19
-
20
- if [ "${#EXPLODED_RUBY[@]}" != "1" ] && [ "${#EXPLODED_RUBY[@]}" != "2" ]; then
21
- echo "Unknown format of .ruby-version file"
22
- exit 1
23
- fi
24
-
25
- echo $RUBY
@@ -1,28 +0,0 @@
1
- rspec-active-support-v4:
2
- name: 'rspec with ActiveSupport v4'
3
-
4
- run_when:
5
- 'some HEAD has been updated':
6
- type: branch
7
- include_match: ^.*$
8
-
9
- context:
10
-
11
- script_defaults:
12
- template_environment_variables: true
13
-
14
- task_defaults:
15
- environment_variables:
16
- ACTIVESUPPORT: '4'
17
- BUNDLER: '_1.17.3_'
18
-
19
- max_trials: 2
20
- dispatch_storm_delay_duration: 1 Seconds
21
- include:
22
- - cider-ci/task_components/ruby.yml
23
- - cider-ci/task_components/bundle.yml
24
- - cider-ci/task_components/rspec.yml
25
-
26
- tasks:
27
- all-rspec:
28
- name: All rspec tests, using ActiveSupport v4