Skip to content

Commit

Permalink
Merge pull request #3843 from AlekhyaYalla/alekhyayalla/retry_azure_p…
Browse files Browse the repository at this point in the history
…ost_request

[Azure] Add retries for Azure client POST
  • Loading branch information
jurre authored Jun 8, 2021
2 parents 6c760a9 + 90adeb8 commit f5d81dc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 27 deletions.
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

0 comments on commit f5d81dc

Please sign in to comment.