diff --git a/.github/workflows/test-jruby.yml b/.github/workflows/test-jruby.yml index 94931a0..bc066fa 100644 --- a/.github/workflows/test-jruby.yml +++ b/.github/workflows/test-jruby.yml @@ -25,5 +25,7 @@ jobs: # Mark failures as warnings for now due to high level of flakiness # and occasional OOM-s on CI continue-on-error: true + env: + DEBUG_NATS_TEST: ${{ runner.debug }} run: | bundle exec rspec || DEBUG_NATS_TEST=true bundle exec rspec --only-failures diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fab8e2a..1e78c7a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,6 +16,7 @@ jobs: CI: true NATS_SERVER_VERSION: ${{ matrix.nats_version }} SKIP_RAILS: true + DEBUG_NATS_TEST: ${{ runner.debug }} strategy: fail-fast: false matrix: @@ -31,6 +32,8 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true - name: Run RSpec + env: + DEBUG_NATS_TEST: ${{ runner.debug }} run: | bundle exec rspec diff --git a/.rspec b/.rspec index c99d2e7..3687797 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,2 @@ --require spec_helper +--color diff --git a/spec/support/nats_server_helper.rb b/spec/support/nats_server_helper.rb index 5b6f4d1..9691ee0 100644 --- a/spec/support/nats_server_helper.rb +++ b/spec/support/nats_server_helper.rb @@ -19,7 +19,7 @@ def init_with_config(config_file) end def init_with_config_from_string(config_string, config={}) - puts config_string if ENV["DEBUG_NATS_TEST"] == "true" + puts config_string if debug? config_file = Tempfile.new(['nats-cluster-tests', '.conf']) File.open(config_file.path, 'w') do |f| f.puts(config_string) @@ -34,6 +34,9 @@ def init_with_config_from_string(config_string, config={}) NatsServerControl.new(uri, config['pid_file'], "-c #{config_file.path}", config_file) end + def debug? + %w[1 true t].include?(ENV["DEBUG_NATS_TEST"]) + end end attr_reader :uri @@ -45,6 +48,10 @@ def initialize(uri='nats://127.0.0.1:4222', pid_file='/tmp/test-nats.pid', flags @config_file = config_file end + def debug? + self.class.debug? + end + def server_pid @pid ||= File.read(@pid_file).chomp.to_i end @@ -72,7 +79,7 @@ def start_server(wait_for_server=true) end args += " #{@flags}" if @flags - if ENV["DEBUG_NATS_TEST"] == "true" + if debug? system("#{BIN_PATH} #{args} -DV &") else system("#{BIN_PATH} #{args} 2> /dev/null &") @@ -92,19 +99,21 @@ def kill_server end def wait_for_server(uri, max_wait = 5) # :nodoc: - start = Time.now - while (Time.now - start < max_wait) # Wait max_wait seconds max + wait = max_wait.to_f + loop do return if server_running?(uri) sleep(0.1) - end + wait -= 0.1 - raise "NATS Server did not start in #{max_wait} seconds" + raise "NATS Server did not start in #{max_wait} seconds" if wait <= 0 + end end def server_running?(uri) # :nodoc: s = TCPSocket.new(uri.host, uri.port, nil, nil, connect_timeout: 0.5) true - rescue + rescue => e + puts "Server is not available: #{e}" if debug? false ensure s&.close