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,
|
||||
# :write, or :open.
|
||||
# @param options [Hash] Hash containing Symbol keys like :timeout,
|
||||
# :read_timeout, :write_timeout, :open_timeout, or
|
||||
# :timeout
|
||||
# :read_timeout, :write_timeout, or :open_timeout
|
||||
#
|
||||
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
||||
# has been set.
|
||||
|
@ -1,5 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'timeout'
|
||||
|
||||
module Faraday
|
||||
class Adapter
|
||||
# @example
|
||||
@ -277,11 +279,22 @@ module Faraday
|
||||
end
|
||||
|
||||
block_arity = stub.block.arity
|
||||
status, headers, body =
|
||||
if block_arity >= 0
|
||||
stub.block.call(*[env, meta].take(block_arity))
|
||||
params = if block_arity >= 0
|
||||
[env, meta].take(block_arity)
|
||||
else
|
||||
stub.block.call(env, meta)
|
||||
[env, meta]
|
||||
end
|
||||
|
||||
timeout = request_timeout(:open, env[:request])
|
||||
timeout ||= request_timeout(:read, env[:request])
|
||||
|
||||
status, headers, body =
|
||||
if timeout
|
||||
::Timeout.timeout(timeout, Faraday::TimeoutError) do
|
||||
stub.block.call(*params)
|
||||
end
|
||||
else
|
||||
stub.block.call(*params)
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user