Skip to content

Commit a092358

Browse files
authored
Merge pull request #637 from alphagov/timeout
Apply timeout on web Google calls
2 parents 2f06022 + cb35f09 commit a092358

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

app/services/discovery_engine/clients.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ module Clients
33
module_function
44

55
def completion_service
6-
@completion_service ||= Google::Cloud::DiscoveryEngine.completion_service(version: :v1)
6+
@completion_service ||= Google::Cloud::DiscoveryEngine.completion_service(version: :v1) do |config|
7+
config.timeout = 1
8+
end
79
end
810

911
def document_service
1012
@document_service ||= Google::Cloud::DiscoveryEngine.document_service(version: :v1)
1113
end
1214

1315
def search_service
14-
@search_service ||= Google::Cloud::DiscoveryEngine.search_service(version: :v1)
16+
@search_service ||= Google::Cloud::DiscoveryEngine.search_service(version: :v1) do |config|
17+
config.timeout = 4
18+
end
1519
end
1620

1721
def user_event_service
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
RSpec.describe DiscoveryEngine::Clients do
2+
after do
3+
described_class.instance_variable_set(:@completion_service, nil)
4+
described_class.instance_variable_set(:@search_service, nil)
5+
end
6+
7+
shared_examples "a client with timeout configured" do |service_name, timeout|
8+
let(:config) { double("config") }
9+
let(:client) { double("client") }
10+
11+
before do
12+
allow(config).to receive(:timeout=)
13+
allow(Google::Cloud::DiscoveryEngine).to receive(service_name) do |**_kwargs, &block|
14+
block.call(config) if block
15+
client
16+
end
17+
end
18+
19+
it "initialises the client with the v1 API version" do
20+
subject
21+
expect(Google::Cloud::DiscoveryEngine).to have_received(service_name).with(version: :v1)
22+
end
23+
24+
it "configures the client with a timeout" do
25+
subject
26+
expect(config).to have_received(:timeout=).with(timeout)
27+
end
28+
end
29+
30+
describe ".search_service with 3 second timeout" do
31+
subject { described_class.search_service }
32+
33+
include_examples "a client with timeout configured", :search_service, 4
34+
end
35+
36+
describe ".completion_service with one second timeout" do
37+
subject { described_class.completion_service }
38+
39+
include_examples "a client with timeout configured", :completion_service, 1
40+
end
41+
end

0 commit comments

Comments
 (0)