File tree 3 files changed +55
-4
lines changed
provider/verification_results
spec/lib/pact/provider/verification_results
3 files changed +55
-4
lines changed Original file line number Diff line number Diff line change 1
1
require 'json'
2
2
require 'pact/errors'
3
+ require 'pact/retry'
3
4
4
5
# TODO move this to the pact broker client
5
6
# TODO retries
@@ -75,8 +76,10 @@ def tag_versions
75
76
response = nil
76
77
begin
77
78
options = { :use_ssl => uri . scheme == 'https' }
78
- response = Net ::HTTP . start ( uri . host , uri . port , options ) do |http |
79
- http . request request
79
+ Retry . until_true do
80
+ response = Net ::HTTP . start ( uri . host , uri . port , options ) do |http |
81
+ http . request request
82
+ end
80
83
end
81
84
rescue StandardError => e
82
85
error_message = "Failed to tag provider version due to: #{ e . class } #{ e . message } "
@@ -95,8 +98,10 @@ def publish_verification_results
95
98
response = nil
96
99
begin
97
100
options = { :use_ssl => uri . scheme == 'https' }
98
- response = Net ::HTTP . start ( uri . host , uri . port , options ) do |http |
99
- http . request request
101
+ Retry . until_true do
102
+ response = Net ::HTTP . start ( uri . host , uri . port , options ) do |http |
103
+ http . request request
104
+ end
100
105
end
101
106
rescue StandardError => e
102
107
error_message = "Failed to publish verification results due to: #{ e . class } #{ e . message } "
Original file line number Diff line number Diff line change
1
+ module Pact
2
+ class Retry
3
+ class RescuableError
4
+ UNRESCUEABLE = [ Pact ::Error ]
5
+
6
+ def self . ===( e )
7
+ case e
8
+ when *UNRESCUEABLE then
9
+ false
10
+ else
11
+ true
12
+ end
13
+ end
14
+ end
15
+
16
+ def self . until_true options = { }
17
+ max_tries = options . fetch ( :times , 3 )
18
+ tries = 0
19
+ while true
20
+ begin
21
+ return yield
22
+ rescue RescuableError => e
23
+ tries += 1
24
+ $stderr. puts "Error making request - #{ e . class } #{ e . message } #{ e . backtrace . find { |l | l . include? ( 'pact_provider' ) } } , attempt #{ tries } of #{ max_tries } "
25
+ raise e if max_tries == tries
26
+ sleep options
27
+ end
28
+ end
29
+ end
30
+
31
+ def self . sleep options
32
+ Kernel . sleep options . fetch ( :sleep , 5 )
33
+ end
34
+ end
35
+ end
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ module VerificationResults
54
54
allow ( Pact . configuration ) . to receive ( :provider ) . and_return ( provider_configuration )
55
55
stub_request ( :post , stubbed_publish_verification_url ) . to_return ( status : 200 , body : created_verification_body )
56
56
stub_request ( :put , 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo' )
57
+ allow ( Retry ) . to receive ( :until_true ) { |&block | block . call }
57
58
end
58
59
59
60
subject { Publish . call ( pact_source , verification ) }
@@ -76,6 +77,11 @@ module VerificationResults
76
77
expect ( WebMock ) . to have_requested ( :post , publish_verification_url ) . with ( body : verification_json , headers : { 'Content-Type' => 'application/json' } )
77
78
end
78
79
80
+ it "should call Retry.until_true once" do
81
+ subject
82
+ expect ( Retry ) . to have_received ( :until_true ) . once ( )
83
+ end
84
+
79
85
context "when the verification result is not publishable" do
80
86
let ( :publishable ) { false }
81
87
@@ -93,6 +99,11 @@ module VerificationResults
93
99
expect ( WebMock ) . to have_requested ( :put , 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo' ) . with ( headers : { 'Content-Type' => 'application/json' } )
94
100
end
95
101
102
+ it "should call Retry.until_true twice" do
103
+ subject
104
+ expect ( Retry ) . to have_received ( :until_true ) . twice ( )
105
+ end
106
+
96
107
context "when there is no pb:publish-verification-results link" do
97
108
before do
98
109
pact_hash [ '_links' ] . delete ( 'pb:publish-verification-results' )
You can’t perform that action at this time.
0 commit comments