mirror of
https://github.com/lostisland/faraday.git
synced 2025-07-06 00:01:37 -04:00
Compare commits
2 Commits
3ee9c81335
...
9a16831147
Author | SHA1 | Date | |
---|---|---|---|
|
9a16831147 | ||
|
1914d594c4 |
@ -78,8 +78,7 @@ module Faraday
|
|||||||
# @param type [Symbol] Describes which timeout setting to get: :read,
|
# @param type [Symbol] Describes which timeout setting to get: :read,
|
||||||
# :write, or :open.
|
# :write, or :open.
|
||||||
# @param options [Hash] Hash containing Symbol keys like :timeout,
|
# @param options [Hash] Hash containing Symbol keys like :timeout,
|
||||||
# :read_timeout, :write_timeout, :open_timeout, or
|
# :read_timeout, :write_timeout, or :open_timeout
|
||||||
# :timeout
|
|
||||||
#
|
#
|
||||||
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
||||||
# has been set.
|
# has been set.
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'timeout'
|
||||||
|
|
||||||
module Faraday
|
module Faraday
|
||||||
class Adapter
|
class Adapter
|
||||||
# @example
|
# @example
|
||||||
@ -277,11 +279,22 @@ module Faraday
|
|||||||
end
|
end
|
||||||
|
|
||||||
block_arity = stub.block.arity
|
block_arity = stub.block.arity
|
||||||
|
params = if block_arity >= 0
|
||||||
|
[env, meta].take(block_arity)
|
||||||
|
else
|
||||||
|
[env, meta]
|
||||||
|
end
|
||||||
|
|
||||||
|
timeout = request_timeout(:open, env[:request])
|
||||||
|
timeout ||= request_timeout(:read, env[:request])
|
||||||
|
|
||||||
status, headers, body =
|
status, headers, body =
|
||||||
if block_arity >= 0
|
if timeout
|
||||||
stub.block.call(*[env, meta].take(block_arity))
|
::Timeout.timeout(timeout, Faraday::TimeoutError) do
|
||||||
|
stub.block.call(*params)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
stub.block.call(env, meta)
|
stub.block.call(*params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# We need to explicitly pass `reason_phrase = nil` here to avoid keyword args conflicts.
|
# We need to explicitly pass `reason_phrase = nil` here to avoid keyword args conflicts.
|
||||||
|
@ -410,4 +410,33 @@ RSpec.describe Faraday::Adapter::Test do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'request timeout' do
|
||||||
|
subject(:request) do
|
||||||
|
connection.get('/sleep') do |req|
|
||||||
|
req.options.timeout = timeout
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
stubs.get('/sleep') do
|
||||||
|
sleep(0.01)
|
||||||
|
[200, {}, '']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when request is within timeout' do
|
||||||
|
let(:timeout) { 1 }
|
||||||
|
|
||||||
|
it { expect(request.status).to eq 200 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when request is too slow' do
|
||||||
|
let(:timeout) { 0.001 }
|
||||||
|
|
||||||
|
it 'raises an exception' do
|
||||||
|
expect { request }.to raise_error(Faraday::TimeoutError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user