Skip to content

Commit

Permalink
feat: add tests for rewrapping errors
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-rostovtsev committed Sep 3, 2022
1 parent 2e05efa commit 6be5f20
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gapic-common/test/gapic/grpc/rpc_call/raise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,37 @@ def test_wont_wrap_non_grpc_errors
assert_kind_of Time, deadline_arg
assert_equal 1, call_count
end

##
# Tests that if a layer underlying the RpcCall throws a ::GRPC::Unavailable
# that contains a Signet::AuthorizationError in its text,
# it gets extracted and rewrapped into a G
def test_will_rewrap_signet_errors
signet_error_text = <<-SIGNET
#<Signet::AuthorizationError: Authorization failed. Server message:
# {
# "error": "invalid_grant",
# "error_description": "Bad Request"
# }
SIGNET

unauth_error_text = "#{::GRPC::Core::StatusCodes::UNAUTHENTICATED}:#{signet_error_text}"

api_meth_stub = proc do |*_args|
raise GRPC::Unavailable.new(signet_error_text)
end

rpc_call = Gapic::ServiceStub::RpcCall.new(
api_meth_stub
)

ex = assert_raises Gapic::GRPC::AuthorizationError do
rpc_call.call Object.new
end

assert_equal ::GRPC::Core::StatusCodes::UNAUTHENTICATED, ex.code
assert_equal unauth_error_text, ex.message
refute_nil ex.cause
assert_kind_of GRPC::Unavailable, ex.cause
end
end
47 changes: 47 additions & 0 deletions gapic-common/test/gapic/grpc/rpc_call/retry/raise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ def test_times_out
sleep_mock.verify
end

##
# Tests that if a layer underlying the RpcCall repeatedly throws a retriable exception
# and then throws a DeadlineExceeded, the two exceptions get wrapped in a Gapic::GRPC::DeadlineExceededError
#
def test_deadline_exceeded
call_count = 0
err_message = "foo"
unavailable_err_message = "#{GRPC::Core::StatusCodes::UNAVAILABLE}:#{err_message}"

api_meth_stub = proc do |deadline: nil, **_kwargs|
call_count += 1
raise GRPC::DeadlineExceeded if call_count == 3
raise GRPC::Unavailable.new(err_message)
end

rpc_call = Gapic::ServiceStub::RpcCall.new api_meth_stub

sleep_mock = Minitest::Mock.new
sleep_mock.expect :sleep, nil, [1]
sleep_mock.expect :sleep, nil, [1 * 1.3]
sleep_proc = ->(count) { sleep_mock.sleep count }

options = Gapic::CallOptions.new(
timeout: 300,
retry_policy: { retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE] }
)

Kernel.stub :sleep, sleep_proc do
ex = assert_raises Gapic::GRPC::DeadlineExceededError do
rpc_call.call Object.new, options: options
end

assert_equal 3, call_count

assert_equal ::GRPC::Core::StatusCodes::DEADLINE_EXCEEDED, ex.code

refute_nil ex.cause
assert_kind_of ::GRPC::DeadlineExceeded, ex.cause

refute_nil ex.root_cause
assert_kind_of ::GRPC::Unavailable, ex.root_cause
assert_equal unavailable_err_message, ex.root_cause.message
end

sleep_mock.verify
end

def test_aborts_on_unexpected_exception
call_count = 0

Expand Down

0 comments on commit 6be5f20

Please sign in to comment.