Skip to content

Commit

Permalink
feat: Add retries to verification publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rashiagarwal committed Apr 19, 2018
1 parent 8a0a73f commit 6620165
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/pact/provider/verification_results/publish.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'json'
require 'pact/errors'
require 'pact/retry'

# TODO move this to the pact broker client
# TODO retries
Expand Down Expand Up @@ -75,8 +76,10 @@ def tag_versions
response = nil
begin
options = {:use_ssl => uri.scheme == 'https'}
response = Net::HTTP.start(uri.host, uri.port, options) do |http|
http.request request
Retry.until_true do
response = Net::HTTP.start(uri.host, uri.port, options) do |http|
http.request request
end
end
rescue StandardError => e
error_message = "Failed to tag provider version due to: #{e.class} #{e.message}"
Expand All @@ -95,8 +98,10 @@ def publish_verification_results
response = nil
begin
options = {:use_ssl => uri.scheme == 'https'}
response = Net::HTTP.start(uri.host, uri.port, options) do |http|
http.request request
Retry.until_true do
response = Net::HTTP.start(uri.host, uri.port, options) do |http|
http.request request
end
end
rescue StandardError => e
error_message = "Failed to publish verification results due to: #{e.class} #{e.message}"
Expand Down
35 changes: 35 additions & 0 deletions lib/pact/retry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Pact
class Retry
class RescuableError
UNRESCUEABLE = [Pact::Error]

def self.===(e)
case e
when *UNRESCUEABLE then
false
else
true
end
end
end

def self.until_true options = {}
max_tries = options.fetch(:times, 3)
tries = 0
while true
begin
return yield
rescue RescuableError => e
tries += 1
$stderr.puts "Error making request - #{e.class} #{e.message} #{e.backtrace.find {|l| l.include?('pact_provider')}}, attempt #{tries} of #{max_tries}"
raise e if max_tries == tries
sleep options
end
end
end

def self.sleep options
Kernel.sleep options.fetch(:sleep, 5)
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/pact/provider/verification_results/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module VerificationResults
allow(Pact.configuration).to receive(:provider).and_return(provider_configuration)
stub_request(:post, stubbed_publish_verification_url).to_return(status: 200, body: created_verification_body)
stub_request(:put, 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo')
allow(Retry).to receive(:until_true) { |&block| block.call }
end

subject { Publish.call(pact_source, verification) }
Expand All @@ -76,6 +77,11 @@ module VerificationResults
expect(WebMock).to have_requested(:post, publish_verification_url).with(body: verification_json, headers: {'Content-Type' => 'application/json'})
end

it "should call Retry.until_true once" do
subject
expect(Retry).to have_received(:until_true).once()
end

context "when the verification result is not publishable" do
let(:publishable) { false }

Expand All @@ -93,6 +99,11 @@ module VerificationResults
expect(WebMock).to have_requested(:put, 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo').with(headers: {'Content-Type' => 'application/json'})
end

it "should call Retry.until_true twice" do
subject
expect(Retry).to have_received(:until_true).twice()
end

context "when there is no pb:publish-verification-results link" do
before do
pact_hash['_links'].delete('pb:publish-verification-results')
Expand Down

0 comments on commit 6620165

Please sign in to comment.