-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a request timeout to Excon for non-blocking sockets (#835)
* Add a /sloth path that takes 5 milliseconds to return * Allow defining a timeout parameter and default to 5 minutes * Set a deadline in the datum at the start of the request * Add tests for the request timeout * Only timeout nonblocking connections, same as read and write timeouts * Default to no timeout. * Only set the deadline when a timeout is set. * Add tests for no timeout set. * Handle explicit nil timeout, such as in the defaults. * Don't check the deadline unless set in socket data * match error message to ensure the right type of timeout occurred * Use single quotes for timeout message. * Fix indentation * Fix indentation * Remove duplicate /sloth path and add read timeout tests together with timeout. * Use request symbol instead of string. * Make expectations specific to the type of timeout * Update readme with timeout option. * Switch to a hash for mapping timeout keys to reduce the impact to socket use without a deadline. Also, remove extra raise when checking the deadline. * Ruby the use of multi-return in #check_deadline! * Remove unused params and update docs for check_deadline! * Add a millisecond example and mention that timeout may be an int or float. * Switch / to /no-timeout in the timeout.ru server definition * Rename check deadline and update the doc comment. * Add comments to explain the request timeout in select_with_timeout * Put more of a distance between read and request timeout to reduce the likelihood of a race condition * Need to keep the blocking read-timeout small so the test is fast.
- Loading branch information
Showing
6 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require 'spec_helper' | ||
|
||
describe Excon::Connection do | ||
include_context('test server', :webrick, 'timeout.ru', before: :start, after: :stop) | ||
|
||
let(:read_timeout) { 60 } | ||
let(:conn) do | ||
Excon::Connection.new(host: '127.0.0.1', | ||
hostname: '127.0.0.1', | ||
nonblock: nonblock, | ||
port: 9292, | ||
scheme: 'http', | ||
timeout: timeout, | ||
read_timeout: read_timeout) | ||
end | ||
|
||
context "blocking connection" do | ||
let (:nonblock) { false } | ||
|
||
context 'when timeout is not set' do | ||
let(:timeout) { nil } | ||
|
||
it 'does not error' do | ||
expect(conn.request(:path => '/no-timeout').status).to eq(200) | ||
end | ||
end | ||
|
||
context 'when timeout is not triggered' do | ||
let(:timeout) { 1 } | ||
|
||
it 'does not error' do | ||
expect(conn.request(:path => '/no-timeout').status).to eq(200) | ||
end | ||
end | ||
|
||
context 'when timeout is triggered' do | ||
let(:read_timeout) { 0.005 } | ||
let(:timeout) { 0.001 } | ||
|
||
it 'does not raise' do | ||
# raising a read timeout to keep tests fast | ||
expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'read timeout reached') | ||
end | ||
end | ||
end | ||
|
||
context "non-blocking connection" do | ||
let (:nonblock) { true } | ||
|
||
context 'when timeout is not set' do | ||
let(:timeout) { nil } | ||
|
||
it 'does not error' do | ||
expect(conn.request(:path => '/no-timeout').status).to eq(200) | ||
end | ||
end | ||
|
||
context 'when timeout is not triggered' do | ||
let(:timeout) { 1 } | ||
|
||
it 'does not error' do | ||
expect(conn.request(:path => '/no-timeout').status).to eq(200) | ||
end | ||
end | ||
|
||
context 'when timeout is triggered' do | ||
let(:timeout) { 0.001 } | ||
|
||
it 'returns a request Excon::Error::Timeout' do | ||
expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'request timeout reached') | ||
end | ||
end | ||
|
||
context 'when read timeout is triggered' do | ||
let(:read_timeout) { 0.001 } | ||
let(:timeout) { 5 } | ||
|
||
it 'returns a read Excon::Error::Timeout' do | ||
expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'read timeout reached') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters