Skip to content

Commit

Permalink
Should fallback to degraded mode when any SystemCallError raised
Browse files Browse the repository at this point in the history
The `::SystemCallError` includes
- ::Errno::EINVAL,
- ::Errno::ECONNRESET,
- ::Errno::ECONNREFUSED,
- ::Errno::ETIMEDOUT and more

It's often caused by network problem or hardware error
  • Loading branch information
IChou committed Sep 16, 2022
1 parent b0c1114 commit b998b44
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ source 'https://rubygems.org'
gemspec

gem 'logger'
gem 'openssl'
gem 'openssl', '~> 2.2'
gem 'rest-client'

group :test do
gem 'rake', '~> 12.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', require: false
gem 'rubocop', '~> 1.25', require: false
gem 'rubocop-rspec', require: false
gem 'webmock'
gem 'timecop'
Expand Down
99 changes: 51 additions & 48 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,77 +1,80 @@
PATH
remote: .
specs:
geetest_ruby_sdk (4.0.2)
geetest_ruby_sdk (4.0.3)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.0)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
crack (0.4.5)
rexml
diff-lcs (1.5.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
hashdiff (0.3.7)
hashdiff (1.0.1)
http-accept (1.7.0)
http-cookie (1.0.3)
http-cookie (1.0.5)
domain_name (~> 0.5)
logger (1.4.2)
mime-types (3.3.1)
ipaddr (1.2.4)
logger (1.5.1)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
mime-types-data (3.2022.0105)
netrc (0.11.0)
openssl (2.2.0)
parallel (1.19.1)
parser (2.7.1.2)
ast (~> 2.4.0)
public_suffix (4.0.6)
rainbow (3.0.0)
openssl (2.2.2)
ipaddr
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
public_suffix (4.0.7)
rainbow (3.1.1)
rake (12.3.3)
regexp_parser (2.5.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rubocop (0.84.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.1)
rubocop (1.28.2)
parallel (~> 1.10)
parser (>= 2.7.0.1)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 0.0.3)
rubocop-ast (>= 1.17.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.0.3)
parser (>= 2.7.0.1)
rubocop-rspec (1.39.0)
rubocop (>= 0.68.1)
ruby-progressbar (1.10.1)
safe_yaml (1.0.4)
timecop (0.9.4)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.17.0)
parser (>= 3.1.1.0)
rubocop-rspec (2.10.0)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
timecop (0.9.5)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.7.0)
webmock (3.1.0)
addressable (>= 2.3.6)
unf_ext (0.0.8.2)
unicode-display_width (2.3.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
Expand All @@ -80,14 +83,14 @@ DEPENDENCIES
bundler (~> 2.0)
geetest_ruby_sdk!
logger
openssl
openssl (~> 2.2)
rake (~> 12.0)
rest-client
rspec (~> 3.0)
rubocop
rubocop (~> 1.25)
rubocop-rspec
timecop
webmock

BUNDLED WITH
2.3.8
2.3.13
8 changes: 3 additions & 5 deletions geetest_ruby_sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ require_relative 'lib/geetest_ruby_sdk/version'
Gem::Specification.new do |spec|
spec.name = 'geetest_ruby_sdk'
spec.version = GeetestRubySdk::VERSION
spec.authors = ['Yuehao Hua']
spec.email = ['[email protected]']
spec.authors = ['Yuehao Hua', 'Yii Chou']
spec.email = ['[email protected], zhouyi@jinshuju,net']

spec.summary = 'Ruby version of Geetest SDK'
spec.homepage = 'https://github.com/jinshuju/geetest-ruby-sdk'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')

spec.metadata['allowed_push_host'] = 'https://github.com/jinshuju/geetest-ruby-sdk'
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/jinshuju/geetest-ruby-sdk'
Expand Down
5 changes: 3 additions & 2 deletions lib/geetest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ def channels

def exceptions_to_degraded_mode
@exceptions_to_degraded_mode ||= [
::SystemCallError, # includes ::Errno::EINVAL, ::Errno::ECONNRESET, ::Errno::ECONNREFUSED, ::Errno::ETIMEDOUT, and more
JSON::ParserError,
RestClient::InternalServerError,
RestClient::NotFound,
RestClient::RequestTimeout,
JSON::ParserError
RestClient::RequestTimeout
]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/geetest_ruby_sdk/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GeetestRubySdk
VERSION = '4.0.2'
VERSION = '4.0.3'
end
19 changes: 10 additions & 9 deletions spec/geetest/v3/register_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
let(:response_headers) { { 'Content-Type' => 'application/octet-stream; charset=binary' } }

describe 'failed request' do
it 'returns success 0 when status is 500' do
it 'returns challenge and set success to 0 when response status is 500' do
stub_request(:get, 'http://api.geetest.com/register.php').with(query: payload).to_return(
status: [500, 'Internal Server Error'], headers: response_headers
)
expect(geetest_register.register[:challenge].size).to eq 32
expect(geetest_register.register[:success]).to eq 0
end

it 'returns challenge when status is 500' do
it 'returns challenge and set success to 0 when response body not json' do
stub_request(:get, 'http://api.geetest.com/register.php').with(query: payload).to_return(
status: [500, 'Internal Server Error'], headers: response_headers
status: [200, 'Ok'], headers: response_headers, body: 'invalid response'
)
expect(geetest_register.register[:challenge].size).to eq 32
expect(geetest_register.register[:success]).to eq 0
end

it 'returns challenge and set success to 0 when response body not json' do
stub_request(:get, 'http://api.geetest.com/register.php').with(query: payload).to_return(
status: [200, 'Ok'], headers: response_headers, body: 'invalid response'
)
it 'returns challenge and set success to 0 when a SystemCallError raised' do
stub_request(:get, 'http://api.geetest.com/register.php').with(query: payload).and_raise ::Errno::ECONNRESET
expect(geetest_register.register[:challenge].size).to eq 32
expect(geetest_register.register[:success]).to eq 0
end

it 'raises an register failed error when status is 403' do
it 'raises an register failed error when response status is 403' do
stub_request(:get, 'http://api.geetest.com/register.php').with(query: payload).to_return(
status: [403, 'Forbidden'], headers: response_headers
status: [403, 'Forbidden'], headers: response_headers
)
expect { geetest_register.register }.to raise_error RestClient::Forbidden
end
Expand Down
5 changes: 5 additions & 0 deletions spec/geetest/v4/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
expect(geetest_account.validate?(geetest_data)).to be true
end

it 'return true if a SystemCallError raised' do
stub_request(:post, validate_url).with(body:request_payload).and_raise ::Errno::ECONNRESET
expect(geetest_account.validate?(geetest_data)).to be true
end

it 'returns false if request geetest api fail and exception not in degraded_mode' do
stub_request(:post, validate_url).with(body: request_payload).to_return(
body: 'Forbidden', status: 403
Expand Down

0 comments on commit b998b44

Please sign in to comment.