From b279f9caa9a6a169bad7350890d7015d56e9e9b2 Mon Sep 17 00:00:00 2001 From: Alex Skrenchuk Date: Wed, 2 Oct 2024 21:12:26 -0700 Subject: [PATCH] add new method of determining free port fixes intermittent unit test failures due to port being in use --- test/test_case.rb | 7 +++++++ test/test_ontology_pull.rb | 4 ++-- test/test_scheduler.rb | 13 ++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/test/test_case.rb b/test/test_case.rb index 75bb0454..cae57cda 100644 --- a/test/test_case.rb +++ b/test/test_case.rb @@ -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 diff --git a/test/test_ontology_pull.rb b/test/test_ontology_pull.rb index 8ac6dce6..112fd11b 100644 --- a/test/test_ontology_pull.rb +++ b/test/test_ontology_pull.rb @@ -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) @@ -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 diff --git a/test/test_scheduler.rb b/test/test_scheduler.rb index d3d0ac04..db28f26e 100644 --- a/test/test_scheduler.rb +++ b/test/test_scheduler.rb @@ -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) @@ -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 { @@ -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 {