Skip to content

Commit

Permalink
feat: allow a wait time to be specified to allow the provider to beco…
Browse files Browse the repository at this point in the history
…me available

eg. when waiting for another docker container to come up
  • Loading branch information
bethesque committed Apr 28, 2019
1 parent 1e9298a commit cec8d56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Options:
-v, [--verbose=VERBOSE] # Verbose output
-f, [--format=FORMATTER] # RSpec formatter. Defaults to custom Pact formatter. Other options are json and RspecJunitFormatter (which outputs xml).
-o, [--out=FILE] # Write output to a file instead of $stdout.
[--wait=SECONDS] # The number of seconds to wait for the provider to become available before running the verification
# Default: 0
Verify pact(s) against a provider. Supports local and networked (http-based) files.
```

Expand Down
22 changes: 19 additions & 3 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pact/wait_until_server_available'
require 'pact/provider_verifier/add_header_middlware'
require 'pact/provider_verifier/provider_states/add_provider_states_header'
require 'pact/provider_verifier/provider_states/remove_provider_states_header_middleware'
Expand All @@ -13,6 +14,7 @@
module Pact
module ProviderVerifier
class App
include Pact::WaitUntilServerAvailable

PROXY_PACT_HELPER = File.expand_path(File.join(File.dirname(__FILE__), "pact_helper.rb"))

Expand All @@ -29,7 +31,9 @@ def self.call pact_urls, options
def call
setup

exit_statuses = all_pact_urls.collect do |pact_url|
pact_urls = all_pact_urls
wait_until_provider_available
exit_statuses = pact_urls.collect do |pact_url|
verify_pact pact_url
end

Expand Down Expand Up @@ -197,8 +201,20 @@ def print_deprecation_note
$stderr.puts "WARN: The --provider-states-url option is deprecated and the URL endpoint can be removed from the application"
end
end

def wait_until_provider_available
if options.wait && options.wait != 0
uri = URI(options.provider_base_url)
$stderr.puts "INFO: Waiting for up to #{options.wait} seconds for provider to become available at #{uri.host}:#{uri.port}..."
up = wait_until_server_available(uri.host, uri.port, options.wait)
if up
$stderr.puts "INFO: Provider available, proceeding with verifications"
else
$stderr.puts "INFO: Waiting for up to #{options.wait} seconds for provider to become available..."
$stderr.puts "WARN: Provider does not appear to be up on #{uri.host}:#{uri.port}... proceeding with verifications anyway"
end
end
end
end
end
end


1 change: 1 addition & 0 deletions lib/pact/provider_verifier/cli/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class InvalidArgumentsError < ::Thor::Error; end
method_option :out, aliases: "-o", banner: "FILE", desc: "Write output to a file instead of $stdout."
method_option :ignore_failures, type: :boolean, default: false, desc: "If specified, process will always exit with exit code 0", hide: true
method_option :pact_urls, aliases: "-u", hide: true, :required => false
method_option :wait, banner: "SECONDS", required: false, type: :numeric, desc: "The number of seconds to wait for the provider to become available before running the verification", default: 0

def verify(*pact_urls)
validate_verify
Expand Down
24 changes: 24 additions & 0 deletions lib/pact/wait_until_server_available.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'socket'
require 'time'

module Pact
module WaitUntilServerAvailable
def self.call(host, port, wait_time = 15)
end_time = Time.now + wait_time
tries = 0
begin
sleep 2 if tries != 0
Socket.tcp(host, port, connect_timeout: 3) {}
true
rescue => e
tries += 1
retry if Time.now < end_time
return false
end
end

def wait_until_server_available *args
WaitUntilServerAvailable.call(*args)
end
end
end

0 comments on commit cec8d56

Please sign in to comment.