-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PortHelpers from SpaceShoes with one simple test (#558)
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |