diff --git a/google-cloud-datastore/lib/google/cloud/datastore/service.rb b/google-cloud-datastore/lib/google/cloud/datastore/service.rb index c91037d01358..646f0b53a0b8 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,19 @@ def inspect protected + ## + # Applies the timeout to the client-wide config and to every RPC config. + # The generated client gives each RPC its own default timeout, which takes + # precedence over the client-wide one. + 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..aa784707a2db --- /dev/null +++ b/google-cloud-datastore/test/google/cloud/datastore/service_test.rb @@ -0,0 +1,41 @@ +# 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, :mock_datastore do + let(:rpc_names) { Google::Cloud::Datastore::V1::Datastore::Client.configure.rpcs.methods - Object.methods } + + it "applies the timeout to the client and every RPC config" do + service = Google::Cloud::Datastore::Service.new project, :this_channel_is_insecure, default_database, timeout: 5 + config = service.service.configure + + _(config.timeout).must_equal 5 + _(rpc_names).wont_be :empty? + rpc_names.each do |rpc_name| + rpc = config.rpcs.send rpc_name + next unless rpc.respond_to? :timeout= + _(rpc.timeout).must_equal 5, "expected #{rpc_name} timeout to be 5" + end + end + + it "keeps the RPC default timeouts when no timeout is given" do + service = Google::Cloud::Datastore::Service.new project, :this_channel_is_insecure, default_database + config = service.service.configure + defaults = Google::Cloud::Datastore::V1::Datastore::Client.configure + + _(config.rpcs.commit.timeout).must_equal defaults.rpcs.commit.timeout + _(config.rpcs.lookup.timeout).must_equal defaults.rpcs.lookup.timeout + end +end