Skip to content

Commit ded1136

Browse files
authored
Add --timeout and --connection-timeout options (#128)
* Add --timeout and --connection-timeout options * Only set max_retries if Ruby version is >= 2.5) * Fix test (random failure) Since the Minitest run tests concurrently, sometimes the "DispatchWithOneAuthorizedAccountTest" class runs after the "DispatchWithOneUsernameThatHasAuthorizedMultipleAccountsTest" run. In this case, "Twurl::OAuthClient.rcfile" has multiple profiles data in there and hence it fails with its mock checks.
1 parent c872a7d commit ded1136

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

lib/twurl/cli.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def parse_options(args)
8989
file
9090
filefield
9191
base64
92+
timeout
93+
connection_timeout
9294
end
9395
end
9496

@@ -326,6 +328,18 @@ def base64
326328
options.upload['base64'] = base64
327329
end
328330
end
331+
332+
def timeout
333+
on('--timeout [sec]', Integer, 'Number of seconds to wait for the request to be read (default: 60)') do |timeout|
334+
options.timeout = timeout
335+
end
336+
end
337+
338+
def connection_timeout
339+
on('--connection-timeout [sec]', Integer, 'Number of seconds to wait for the connection to open (default: 60)') do |connection_timeout|
340+
options.connection_timeout = connection_timeout
341+
end
342+
end
329343
end
330344
end
331345

lib/twurl/oauth_client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ def to_hash
206206

207207
def configure_http!
208208
consumer.http.set_debug_output(Twurl.options.debug_output_io) if Twurl.options.trace
209+
consumer.http.read_timeout = consumer.http.open_timeout = Twurl.options.timeout || 60
210+
consumer.http.open_timeout = Twurl.options.connection_timeout if Twurl.options.connection_timeout
211+
# Only override if Net::HTTP support max_retries (since Ruby >= 2.5)
212+
consumer.http.max_retries = 0 if consumer.http.respond_to?(:max_retries=)
209213
if Twurl.options.ssl?
210214
consumer.http.use_ssl = true
211215
consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE

test/account_information_controller_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def setup
2222
@options = Twurl::Options.test_exemplar
2323
@client = Twurl::OAuthClient.load_new_client_from_options(options)
2424
mock(Twurl::OAuthClient.rcfile).save.times(1)
25+
Twurl::OAuthClient.rcfile.profiles.clear
2526
Twurl::OAuthClient.rcfile << client
2627
@controller = Twurl::AccountInformationController.new(client, options)
2728
end
@@ -51,10 +52,14 @@ def setup
5152
@controller = Twurl::AccountInformationController.new(other_client, other_client_options)
5253
end
5354

55+
def teardown
56+
Twurl::OAuthClient.rcfile.profiles[default_client.username][other_client.consumer_key].clear
57+
end
58+
5459
def test_authorized_account_is_displayed_and_marked_as_the_default
55-
mock(Twurl::CLI).puts(default_client.username).times(1)
56-
mock(Twurl::CLI).puts(" #{default_client.consumer_key} (default)").times(1)
57-
mock(Twurl::CLI).puts(" #{other_client.consumer_key}").times(1)
60+
mock(Twurl::CLI).puts(default_client.username).times(1).ordered
61+
mock(Twurl::CLI).puts(" #{default_client.consumer_key} (default)").times(1).ordered
62+
mock(Twurl::CLI).puts(" #{other_client.consumer_key}").times(1).ordered
5863

5964
controller.dispatch
6065
end

test/cli_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,19 @@ def test_setting_proxy_updates_to_requested_value
239239
end
240240
end
241241
include ProxyOptionTests
242+
243+
module TimeoutOptionTests
244+
def test_not_specifying_timeout_sets_it_to_nil
245+
options = Twurl::CLI.parse_options([TEST_PATH])
246+
assert_nil options.timeout
247+
assert_nil options.connection_timeout
248+
end
249+
250+
def test_setting_timeout_updates_to_requested_value
251+
options = Twurl::CLI.parse_options([TEST_PATH, '--timeout', '10', '--connection-timeout', '5'])
252+
assert_equal 10, options.timeout
253+
assert_equal 5, options.connection_timeout
254+
end
255+
end
256+
include TimeoutOptionTests
242257
end

0 commit comments

Comments
 (0)