diff --git a/lib/gitlab/topology_service_client/base_service.rb b/lib/gitlab/topology_service_client/base_service.rb index febd8950d48efb4dfcf269289f45cbc483db0292..1aaa4b8a0c024939e4a75e5e5773a61869a9da5a 100644 --- a/lib/gitlab/topology_service_client/base_service.rb +++ b/lib/gitlab/topology_service_client/base_service.rb @@ -2,9 +2,13 @@ module Gitlab module TopologyServiceClient + DEFAULT_TIMEOUT_IN_SECONDS = 1 + class BaseService - def initialize + def initialize(timeout: nil) raise NotImplementedError unless enabled? + + @timeout = timeout end private @@ -13,10 +17,15 @@ def client @client ||= service_class.new( topology_service_address, service_credentials, - interceptors: [MetadataInterceptor.new] + interceptors: [MetadataInterceptor.new], + **options ) end + def options + { timeout: @timeout || DEFAULT_TIMEOUT_IN_SECONDS } + end + def cell_id @cell_id ||= Gitlab.config.cell.id end diff --git a/spec/lib/gitlab/topology_service_client/base_service_spec.rb b/spec/lib/gitlab/topology_service_client/base_service_spec.rb index 34683edf2928e528e73e34ae80a4cff157f024be..61df6c4f4e5bd245eb63922e2fce0eaca553fc56 100644 --- a/spec/lib/gitlab/topology_service_client/base_service_spec.rb +++ b/spec/lib/gitlab/topology_service_client/base_service_spec.rb @@ -172,6 +172,12 @@ end describe '#client' do + let(:mock_service_class) { class_double(Class) } + let(:mock_service) { instance_double(Class, new: instance_double(Object)) } + let(:timeout) { 0.5 } + + let(:base_service) { described_class.new(timeout: timeout) } + before do stub_config(cell: { enabled: true, @@ -180,15 +186,39 @@ tls: { enabled: false } } }) + + allow(base_service).to receive(:service_class).and_return(mock_service_class) end it 'includes MetadataInterceptor in client initialization' do expect(Gitlab::TopologyServiceClient::MetadataInterceptor).to receive(:new) - mock_service = instance_double(Class, new: instance_double(Object)) allow(base_service).to receive(:service_class).and_return(mock_service) base_service.send(:client) end + + it 'includes timeout in client initialization' do + expect(mock_service_class).to receive(:new).with('test:50051', :this_channel_is_insecure, + hash_including( + interceptors: array_including(instance_of(Gitlab::TopologyServiceClient::MetadataInterceptor)), + timeout: timeout + ) + ).and_return(mock_service) + + base_service.send(:client) + end + end + + describe '#options' do + before do + stub_config(cell: { enabled: true }) + end + + it 'returns a hash with timeout set to DEFAULT_TIMEOUT_IN_SECONDS from now' do + options = base_service.send(:options) + + expect(options).to eq({ timeout: Gitlab::TopologyServiceClient::DEFAULT_TIMEOUT_IN_SECONDS }) + end end end