From c7c6d5df16d88dd093d03700a81cc9e5ada5c8cc Mon Sep 17 00:00:00 2001 From: Paul Scarrone <159591+ninjapanzer@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:01:33 -0500 Subject: [PATCH] fix(datastore): apply the timeout option to every RPC The timeout given to Google::Cloud::Datastore.new was set only on the client-wide config. The generated client applies each RPC's own default (60 s) first, and Gapic::CallOptions#apply_defaults only fills an empty value, so the option had no effect. Copy the timeout to each RPC config as well, as google-cloud-pubsub does since #14405. --- .../lib/google/cloud/datastore/service.rb | 14 +++- .../google/cloud/datastore/service_test.rb | 80 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 google-cloud-datastore/test/google/cloud/datastore/service_test.rb diff --git a/google-cloud-datastore/lib/google/cloud/datastore/service.rb b/google-cloud-datastore/lib/google/cloud/datastore/service.rb index c91037d01358..ca2106d8ed05 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/service.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/service.rb @@ -45,7 +45,7 @@ def service return mocked_service if mocked_service @service ||= V1::Datastore::Client.new do |config| config.credentials = credentials if credentials - config.timeout = timeout if timeout + override_client_config_timeouts config if timeout config.endpoint = host if host config.lib_name = "gccl" config.lib_version = Google::Cloud::Datastore::VERSION @@ -168,6 +168,18 @@ def inspect protected + # Set the timeout in the client config. + # Override the default timeout in each individual RPC config as well, since when they are non-nil, these + # defaults have precedence over the top-level config.timeout. See Gapic::CallOptions#apply_defaults. + def override_client_config_timeouts config + config.timeout = timeout + rpc_names = config.rpcs.methods - Object.methods + rpc_names.each do |rpc_name| + rpc = config.rpcs.send rpc_name + rpc.timeout = timeout if rpc.respond_to? :timeout= + end + end + def generate_read_options consistency, transaction, read_time if consistency == :eventual return Google::Cloud::Datastore::V1::ReadOptions.new( diff --git a/google-cloud-datastore/test/google/cloud/datastore/service_test.rb b/google-cloud-datastore/test/google/cloud/datastore/service_test.rb new file mode 100644 index 000000000000..ffca97bc6b25 --- /dev/null +++ b/google-cloud-datastore/test/google/cloud/datastore/service_test.rb @@ -0,0 +1,80 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "helper" + +describe Google::Cloud::Datastore::Service do + let(:project) { "test" } + let(:database) { "" } + let(:credentials) { :this_channel_is_insecure } + let(:host) { "localhost:8081" } + let(:timeout) { 123.4 } + let(:default_config) { Google::Cloud::Datastore::V1::Datastore::Client.configure } + + it "keeps the default timeouts when no timeout is given" do + service = Google::Cloud::Datastore::Service.new project, credentials, database, host: host + config = service.service.configure + + _(config.timeout).must_be :nil? + assert_config_rpcs_timeouts default_config.rpcs, config.rpcs + end + + it "applies the timeout to the client config and to every RPC config" do + service = Google::Cloud::Datastore::Service.new project, credentials, database, host: host, timeout: timeout + config = service.service.configure + + _(config.timeout).must_equal timeout + assert_config_rpcs_timeouts default_config.rpcs, config.rpcs, timeout: timeout + end + + it "sends requests with the configured timeout" do + service = Google::Cloud::Datastore::Service.new project, credentials, database, host: host, timeout: timeout + sent_timeouts = {} + stub = service.service.instance_variable_get :@datastore_stub + stub.define_singleton_method :call_rpc do |method, _request, options:| + sent_timeouts[method] = options.timeout + nil + end + + service.lookup Google::Cloud::Datastore::V1::Key.new + service.commit [] + service.allocate_ids Google::Cloud::Datastore::V1::Key.new + + _(sent_timeouts).must_equal({ lookup: timeout, commit: timeout, allocate_ids: timeout }) + end + + it "does not change the default client configuration" do + Google::Cloud::Datastore::Service.new(project, credentials, database, host: host, timeout: timeout).service + + _(default_config.timeout).must_be :nil? + _(default_config.rpcs.lookup.timeout).must_equal 60.0 + end + + # @param [Numeric, nil] timeout Expected non-default timeout. + def assert_config_rpcs_timeouts expected_rpcs, actual_rpcs, timeout: nil + expected_rpc_names = expected_rpcs.methods - Object.methods + # Explicit sanity check of the number of expected rpcs + _(expected_rpc_names.count).must_equal 8 + _((actual_rpcs.methods - Object.methods).count).must_equal 8 + expected_rpc_names.each do |rpc_name| + expected_timeout = timeout || expected_rpcs.send(rpc_name).timeout + actual_timeout = actual_rpcs.send(rpc_name).timeout + if expected_timeout.nil? + assert_nil actual_timeout, "Unexpected timeout for #{rpc_name}" + else + assert_equal expected_timeout, actual_timeout, "Unexpected timeout for #{rpc_name}" + end + end + end +end