Skip to content

Commit

Permalink
feat: Add CallOptions#merge and CallOptions equality checking (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma authored Jul 27, 2022
1 parent 41441fc commit 4028cfe
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Install tools
shell: bash
run: |
cd gapic-common && bundle install && gem install --no-document toys
cd gapic-common && gem install --no-document toys bundler && bundle install
- name: Test ${{ matrix.task }}
shell: bash
run: |
Expand Down
32 changes: 32 additions & 0 deletions gapic-common/lib/gapic/call_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,50 @@ def initialize timeout: nil, metadata: nil, retry_policy: nil
# @param retry_policy [Hash] the policy for error retry.
# @param retry_policy [Hash] The policy for error retry. keys must match the arguments for
# {RetryPolicy.new}.
#
def apply_defaults timeout: nil, metadata: nil, retry_policy: nil
@timeout ||= timeout
@metadata = metadata.merge @metadata if metadata
@retry_policy.apply_defaults retry_policy if @retry_policy.respond_to? :apply_defaults
end

##
# Convert to hash form.
#
# @return [Hash]
#
def to_h
{
timeout: timeout,
metadata: metadata,
retry_policy: retry_policy
}
end

##
# Return a new CallOptions with the given modifications. The current object
# is not modified.
#
# @param kwargs [keywords] Updated fields. See {#initialize} for details.
# @return [CallOptions] A new CallOptions object.
#
def merge **kwargs
kwargs = to_h.merge kwargs
CallOptions.new(**kwargs)
end

# @private Equality test
def eql? other
other.is_a?(CallOptions) &&
other.timeout == timeout &&
other.metadata == metadata &&
other.retry_policy == retry_policy
end
alias == eql?

# @private Hash code
def hash
to_h.hash
end
end
end
16 changes: 16 additions & 0 deletions gapic-common/lib/gapic/call_options/retry_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def call error
#
# @param retry_policy [Hash] The policy for error retry. keys must match the arguments for
# {RpcCall::RetryPolicy.new}.
#
def apply_defaults retry_policy
return unless retry_policy.is_a? Hash

Expand All @@ -85,6 +86,21 @@ def apply_defaults retry_policy
self
end

# @private Equality test
def eql? other
other.is_a?(RetryPolicy) &&
other.retry_codes == retry_codes &&
other.initial_delay == initial_delay &&
other.multiplier == multiplier &&
other.max_delay == max_delay
end
alias == eql?

# @private Hash code
def hash
[retry_codes, initial_delay, multiplier, max_delay].hash
end

# @private
# See https://grpc.github.io/grpc/core/md_doc_statuscodes.html for a
# list of error codes.
Expand Down
104 changes: 104 additions & 0 deletions gapic-common/test/gapic/call_options/settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,108 @@ def test_apply_defaults_wont_override_custom_values
assert_equal 6, options.retry_policy.multiplier
assert_equal 5, options.retry_policy.max_delay
end

def test_to_h_empty
options = Gapic::CallOptions.new
expected = {
timeout: nil,
metadata: {},
retry_policy: Gapic::CallOptions::RetryPolicy.new
}
assert_equal expected, options.to_h
end

def test_to_h_with_values
options = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
expected_retry_policy = Gapic::CallOptions::RetryPolicy.new(
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
)
expected = {
timeout: 60,
metadata: { foo: :bar },
retry_policy: expected_retry_policy
}
assert_equal expected, options.to_h
end

def test_merge_no_changes
options = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
merged = options.merge
assert_equal 60, merged.timeout
expected_retry_policy = Gapic::CallOptions::RetryPolicy.new(
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
)
assert_equal expected_retry_policy, merged.retry_policy
assert_equal({ foo: :bar }, merged.metadata)
end

def test_merge_with_changes
options = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
merged = options.merge(
timeout: 30,
retry_policy: nil,
metadata: { bar: :foo }
)
assert_equal 30, merged.timeout
assert_equal Gapic::CallOptions::RetryPolicy.new, merged.retry_policy
assert_equal({ bar: :foo }, merged.metadata)
end

def test_equality
options = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
other = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
assert_equal other, options
assert_equal other.hash, options.hash
end

def test_inequality
options = Gapic::CallOptions.new(
timeout: 60,
metadata: { foo: :bar },
retry_policy: {
retry_codes: [GRPC::Core::StatusCodes::UNAVAILABLE],
initial_delay: 4, multiplier: 5, max_delay: 6
}
)
other = Gapic::CallOptions.new
refute_equal other, options
refute_equal other.hash, options.hash
end
end

0 comments on commit 4028cfe

Please sign in to comment.