Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PortHelpers with one simple test #558

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions scarpe-components/lib/scarpe/components/port_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "socket"

module Scarpe::Components
module PortHelpers
MAX_SERVER_STARTUP_WAIT = 5.0

def port_working?(ip, port_num)
begin
TCPSocket.new(ip, port_num)
rescue Errno::ECONNREFUSED
return false
end
return true
end

def wait_until_port_working(ip, port_num, max_wait: MAX_SERVER_STARTUP_WAIT)
t_start = Time.now
loop do
if Time.now - t_start > max_wait
raise "Server on port #{port_num} didn't start up in time!"
end

sleep 0.1
return if port_working?(ip, port_num)
end
end
end
end
12 changes: 12 additions & 0 deletions scarpe-components/test/test_port_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative "test_helper"

require "scarpe/components/port_helpers"
class TestPortHelpers < Minitest::Test
include Scarpe::Components::PortHelpers

def test_port_finder
assert_equal false, port_working?("127.0.0.1", 9832), "Port 9832 should be unused!"
end
end
Loading