Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/twirp/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def make_http_request(conn, service_full_name, rpc_method, content_type, req_opt
r.headers[k] = v
end
end

if req_opts && req_opts[:timeout] && req_opts[:timeout].is_a?(Integer) && req_opts[:timeout] > 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a choice to ignore non positive Integer values because Faraday doesn't actually check this and fails rather hard with a NoMethod error if you don't pass in a number. If ignoring invalid values doesn't sound good, we could instead raise a ArgumentError here and make the request fail.

r.options.timeout = req_opts[:timeout]
end
end
end

Expand Down
76 changes: 76 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ def test_proto_send_headers
assert_equal "red", resp.data.color
end

def test_proto_set_request_timeout_with_valid_value
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_equal 5, req.request.timeout
[200, protoheader, proto(Example::Hat, inches: 99, color: "red")]
})
resp = c.make_hat({}, timeout: 5)
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color
end

def test_proto_set_request_timeout_with_invalid_value
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_nil req.request.timeout
[200, protoheader, proto(Example::Hat, inches: 99, color: "red")]
})
resp = c.make_hat({}, timeout: -99)
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color

resp = c.make_hat({}, timeout: "not an integer")
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color
end

def test_proto_exceeds_request_timeout
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_equal 1, req.request.timeout
sleep(2)
[200, protoheader, proto(Example::Hat, inches: 99, color: "red")]
})
assert_raises Faraday::TimeoutError do
resp = c.make_hat({}, timeout: 1)
end
end

def test_proto_serialized_request_body_attrs
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
size = Example::Size.decode(req.body) # body is valid protobuf
Expand Down Expand Up @@ -249,6 +287,44 @@ def test_json_send_headers
assert_equal "red", resp.data.color
end

def test_proto_set_request_timeout_with_valid_value
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_equal 5, req.request.timeout
[200, jsonheader, '{"inches": 99, "color": "red"}']
}, content_type: "application/json")
resp = c.make_hat({}, timeout: 5)
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color
end

def test_proto_set_request_timeout_with_invalid_value
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_nil req.request.timeout
[200, jsonheader, '{"inches": 99, "color": "red"}']
}, content_type: "application/json")
resp = c.make_hat({}, timeout: -99)
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color

resp = c.make_hat({}, timeout: "not an integer")
assert_nil resp.error
assert_equal 99, resp.data.inches
assert_equal "red", resp.data.color
end

def test_proto_exceeds_request_timeout
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_equal 1, req.request.timeout
sleep(2)
[200, jsonheader, '{"inches": 99, "color": "red"}']
}, content_type: "application/json")
assert_raises Faraday::TimeoutError do
resp = c.make_hat({}, timeout: 1)
end
end

def test_json_serialized_request_body_attrs
c = Example::HaberdasherClient.new(conn_stub("/example.Haberdasher/MakeHat") {|req|
assert_equal "application/json", req.request_headers['Content-Type']
Expand Down