Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Azure] Add retries for Azure client POST #3843

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions common/lib/dependabot/clients/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,29 @@ def get(url)
end

def post(url, json)
response = Excon.post(
url,
body: json,
user: credentials&.fetch("username", nil),
password: credentials&.fetch("password", nil),
idempotent: true,
**SharedHelpers.excon_defaults(
headers: auth_header.merge(
{
"Content-Type" => "application/json"
}
response = nil

retry_connection_failures do
response = Excon.post(
url,
body: json,
user: credentials&.fetch("username", nil),
password: credentials&.fetch("password", nil),
idempotent: true,
**SharedHelpers.excon_defaults(
headers: auth_header.merge(
{
"Content-Type" => "application/json"
}
)
)
)
)

raise InternalServerError if response.status == 500
raise BadGateway if response.status == 502
raise ServiceNotAvailable if response.status == 503
end

raise NotFound if response.status == 404

response
Expand Down
56 changes: 41 additions & 15 deletions common/spec/dependabot/clients/azure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,49 @@
end

context "Retries" do
it "with failure count <= max_retries" do
# Request succeeds (200) on second attempt.
stub_request(:get, base_url).
with(basic_auth: [username, password]).
to_return({ status: 502 }, { status: 200 })

response = client.get(base_url)
expect(response.status).to eq(200)
context "for GET" do
it "with failure count <= max_retries" do
# Request succeeds (200) on second attempt.
stub_request(:get, base_url).
with(basic_auth: [username, password]).
to_return({ status: 502 }, { status: 200 })

response = client.get(base_url)
expect(response.status).to eq(200)
end

it "with failure count > max_retries raises error" do
# Request fails (503) multiple times and exceeds max_retry limit
stub_request(:get, base_url).
with(basic_auth: [username, password]).
to_return({ status: 503 }, { status: 503 }, { status: 503 })

expect { client.get(base_url) }.to raise_error(Dependabot::Clients::Azure::ServiceNotAvailable)
end
end

it "with failure count > max_retries raises error" do
# Request fails (503) multiple times and exceeds max_retry limit
stub_request(:get, base_url).
with(basic_auth: [username, password]).
to_return({ status: 503 }, { status: 503 }, { status: 503 })

expect { client.get(base_url) }.to raise_error(Dependabot::Clients::Azure::ServiceNotAvailable)
context "for POST" do
before :each do
@request_body = "request body"
end
it "with failure count <= max_retries" do
# Request succeeds on thrid attempt
stub_request(:post, base_url).
with(basic_auth: [username, password], body: @request_body).
to_return({ status: 503 }, { status: 503 }, { status: 200 })

response = client.post(base_url, @request_body)
expect(response.status).to eq(200)
end

it "with failure count > max_retries raises an error" do
stub_request(:post, base_url).
with(basic_auth: [username, password], body: @request_body).
to_return({ status: 503 }, { status: 503 }, { status: 503 }, { status: 503 })

expect { client.post(base_url, @request_body) }.
to raise_error(Dependabot::Clients::Azure::ServiceNotAvailable)
end
end
end
end
Expand Down