Skip to content

Commit

Permalink
add new method of determining free port
Browse files Browse the repository at this point in the history
fixes intermittent unit test failures due to port being in use
  • Loading branch information
alexskr committed Oct 3, 2024
1 parent 9d1b6ba commit b279f9c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 7 additions & 0 deletions test/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ def _run_suite(suite, type)
##
# Base test class. Put shared test methods or setup here.
class TestCase < MiniTest::Unit::TestCase
# http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
def self.unused_port
server = TCPServer.new('127.0.0.1', 0)
port = server.addr[1]
server.close
port
end
end
4 changes: 2 additions & 2 deletions test/test_ontology_pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestOntologyPull < TestCase
def self.before_suite
ont_path = File.expand_path("../data/ontology_files/BRO_v3.2.owl", __FILE__)
file = File.new(ont_path)
@@port = Random.rand(55000..65535) # http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
@@port = TestCase.unused_port
@@url = "http://localhost:#{@@port}/"
@@thread = Thread.new do
server = WEBrick::HTTPServer.new(Port: @@port)
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_remote_pull_parsing_action
end

def test_pull_error_notification
server_port = Random.rand(55000..65535)
server_port = TestCase.unused_port

begin
thread = Thread.new do
Expand Down
13 changes: 6 additions & 7 deletions test/test_scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require 'minitest/unit'
MiniTest::Unit.autorun
require_relative 'test_case'
# require_relative '../lib/ncbo_cron'

require_relative '../lib/ncbo_cron'

class TestScheduler < MiniTest::Unit::TestCase
class TestScheduler < TestCase
def test_scheduler
begin
logger = Logger.new($stdout)
Expand All @@ -19,7 +17,8 @@ def test_scheduler
# Create a simple TCPServer to listen from the fork
require 'socket'
listen_string = ""
port = Random.rand(55000..65535) # http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
port = TestCase.unused_port

socket_server = Thread.new do
server = TCPServer.new(port)
loop {
Expand Down Expand Up @@ -69,7 +68,7 @@ def test_scheduler_locking
# Create a simple TCPServer to listen from the fork
require 'socket'
listen_string = ""
port = Random.rand(55000..65535) # http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
port = TestCase.unused_port
socket_server = Thread.new do
server = TCPServer.new(port)
loop {
Expand Down

0 comments on commit b279f9c

Please sign in to comment.