-
Notifications
You must be signed in to change notification settings - Fork 964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Net::HTTP#write_timeout
method (Ruby 2.6.0)
#647
Changes from 1 commit
ab729db
f68e125
b36742b
1b35b88
46e84c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ module HTTParty | |
# * :+timeout+: timeout in seconds | ||
# * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set | ||
# * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set | ||
# * :+write_timeout+: http connection write_timeout in seconds, overrides timeout if set (Ruby >= 2.6.0 required) | ||
# * :+debug_output+: see HTTParty::ClassMethods.debug_output. | ||
# * :+cert_store+: contains certificate data. see method 'attach_ssl_certificates' | ||
# * :+pem+: contains pem client certificate data. see method 'attach_ssl_certificates' | ||
|
@@ -116,6 +117,12 @@ def connection | |
if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float)) | ||
http.open_timeout = options[:timeout] | ||
http.read_timeout = options[:timeout] | ||
|
||
if RUBY_VERSION >= "2.6.0" | ||
http.write_timeout = options[:timeout] | ||
else | ||
Kernel.warn("Warning: option :write_timeout requires Ruby version 2.6.0 or later") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a timeout for everything, we should set |
||
end | ||
end | ||
|
||
if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float)) | ||
|
@@ -126,6 +133,14 @@ def connection | |
http.open_timeout = options[:open_timeout] | ||
end | ||
|
||
if options[:write_timeout] && (options[:write_timeout].is_a?(Integer) || options[:write_timeout].is_a?(Float)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lot of duplication here. Would be better to extract it to a private method |
||
if RUBY_VERSION >= "2.6.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lot of duplication as well. Probably a would be better, to create a wrapper method like so (just an idea, not sure about the naming) for_versions_above('2.6', option: :write_timeout) do
http.write_timeout = options[:write_timeout]
end |
||
http.write_timeout = options[:write_timeout] | ||
else | ||
Kernel.warn("Warning: option :write_timeout requires Ruby version 2.6.0 or later") | ||
end | ||
end | ||
|
||
if options[:debug_output] | ||
http.set_debug_output(options[:debug_output]) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,7 @@ | |
) | ||
expect(http).not_to receive(:open_timeout=) | ||
expect(http).not_to receive(:read_timeout=) | ||
expect(http).not_to receive(:write_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
|
||
adapter.connection | ||
|
@@ -150,6 +151,13 @@ | |
subject { super().read_timeout } | ||
it { is_expected.to eq(5) } | ||
end | ||
|
||
if RUBY_VERSION >= '2.6.0' | ||
describe '#write_timeout' do | ||
subject { super().write_timeout } | ||
it { is_expected.to eq(5) } | ||
end | ||
end | ||
end | ||
|
||
context "and timeout is a string" do | ||
|
@@ -164,6 +172,7 @@ | |
) | ||
expect(http).not_to receive(:open_timeout=) | ||
expect(http).not_to receive(:read_timeout=) | ||
expect(http).not_to receive(:write_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
|
||
adapter.connection | ||
|
@@ -191,6 +200,19 @@ | |
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
|
||
it "should not set the write_timeout" do | ||
http = double( | ||
"http", | ||
:null_object => true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, use colon notation for symbol keyword arguments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @TheSmartnik thanks for the comments 👍 I addressed all of them, but this one. I would like to discuss one thing here. Due to the fact that the doubles contain names with special characters (e.g. Besides that, feel free to take a look at the pushed commits. The code looks better now IMO. |
||
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0 | ||
) | ||
expect(http).not_to receive(:write_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
end | ||
|
||
context "when timeout is set and read_timeout is set to 6 seconds" do | ||
|
@@ -201,6 +223,13 @@ | |
it { is_expected.to eq(5) } | ||
end | ||
|
||
if RUBY_VERSION >= '2.6.0' | ||
describe '#write_timeout' do | ||
subject { super().write_timeout } | ||
it { is_expected.to eq(5) } | ||
end | ||
end | ||
|
||
describe '#read_timeout' do | ||
subject { super().read_timeout } | ||
it { is_expected.to eq(6) } | ||
|
@@ -213,10 +242,14 @@ | |
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0, | ||
:open_timeout= => 0 | ||
:open_timeout= => 0, | ||
:write_timeout= => 0, | ||
) | ||
expect(http).to receive(:open_timeout=) | ||
expect(http).to receive(:read_timeout=).twice | ||
if RUBY_VERSION >= '2.6.0' | ||
expect(http).to receive(:write_timeout=) | ||
end | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
|
@@ -242,6 +275,19 @@ | |
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
|
||
it "should not set the write_timeout" do | ||
http = double( | ||
"http", | ||
:null_object => true, | ||
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:open_timeout= => 0 | ||
) | ||
expect(http).not_to receive(:write_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
end | ||
|
||
context "when timeout is set and open_timeout is set to 7 seconds" do | ||
|
@@ -252,6 +298,13 @@ | |
it { is_expected.to eq(7) } | ||
end | ||
|
||
if RUBY_VERSION >= '2.6.0' | ||
describe '#write_timeout' do | ||
subject { super().write_timeout } | ||
it { is_expected.to eq(5) } | ||
end | ||
end | ||
|
||
describe '#read_timeout' do | ||
subject { super().read_timeout } | ||
it { is_expected.to eq(5) } | ||
|
@@ -264,15 +317,88 @@ | |
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0, | ||
:open_timeout= => 0 | ||
:open_timeout= => 0, | ||
:write_timeout= => 0, | ||
) | ||
expect(http).to receive(:open_timeout=).twice | ||
expect(http).to receive(:read_timeout=) | ||
if RUBY_VERSION >= '2.6.0' | ||
expect(http).to receive(:write_timeout=) | ||
end | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
end | ||
|
||
if RUBY_VERSION >= '2.6.0' | ||
context "when timeout is not set and write_timeout is set to 8 seconds" do | ||
let(:options) { {write_timeout: 8} } | ||
|
||
describe '#write_timeout' do | ||
subject { super().write_timeout } | ||
it { is_expected.to eq(8) } | ||
end | ||
|
||
it "should not set the open timeout" do | ||
http = double( | ||
"http", | ||
:null_object => true, | ||
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0, | ||
:open_timeout= => 0, | ||
:write_timeout= => 0, | ||
|
||
) | ||
expect(http).not_to receive(:open_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
|
||
it "should not set the read timeout" do | ||
http = double( | ||
"http", | ||
:null_object => true, | ||
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0, | ||
:open_timeout= => 0, | ||
:write_timeout= => 0, | ||
|
||
) | ||
expect(http).not_to receive(:read_timeout=) | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
end | ||
|
||
context "when timeout is set and write_timeout is set to 8 seconds" do | ||
let(:options) { {timeout: 2, write_timeout: 8} } | ||
|
||
describe '#write_timeout' do | ||
subject { super().write_timeout } | ||
it { is_expected.to eq(8) } | ||
end | ||
|
||
it "should override the timeout option" do | ||
http = double( | ||
"http", | ||
:null_object => true, | ||
:use_ssl= => false, | ||
:use_ssl? => false, | ||
:read_timeout= => 0, | ||
:open_timeout= => 0, | ||
:write_timeout= => 0, | ||
) | ||
expect(http).to receive(:read_timeout=) | ||
expect(http).to receive(:open_timeout=) | ||
expect(http).to receive(:write_timeout=).twice | ||
allow(Net::HTTP).to receive_messages(new: http) | ||
adapter.connection | ||
end | ||
end | ||
end | ||
|
||
context "when debug_output" do | ||
let(:http) { Net::HTTP.new(uri) } | ||
before do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could extract it to method and refactor other timeout methods as well, please?
Something like
validate_timeout_argument(timeout)
orcheck_timeout_argument(timeout)