Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel Pepper
9a16831147 rubocop 2022-12-09 17:07:19 +01:00
Daniel Pepper
1914d594c4 test adapter timeout 2022-12-09 17:07:19 +01:00
3 changed files with 46 additions and 5 deletions

View File

@ -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.

View File

@ -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
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 =
if block_arity >= 0
stub.block.call(*[env, meta].take(block_arity))
if timeout
::Timeout.timeout(timeout, Faraday::TimeoutError) do
stub.block.call(*params)
end
else
stub.block.call(env, meta)
stub.block.call(*params)
end
# We need to explicitly pass `reason_phrase = nil` here to avoid keyword args conflicts.

View File

@ -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