From 2fa13c206b8f679973d986d3d5cb1764e9ebcb1e Mon Sep 17 00:00:00 2001 From: Ableytner Date: Wed, 13 Mar 2024 09:11:55 +0100 Subject: [PATCH] fix assert_port_is_free --- mcserverwrapper/test/helpers.py | 17 +++++++++++------ mcserverwrapper/test/test_wrapper.py | 10 +++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/mcserverwrapper/test/helpers.py b/mcserverwrapper/test/helpers.py index bb1d921..429d910 100644 --- a/mcserverwrapper/test/helpers.py +++ b/mcserverwrapper/test/helpers.py @@ -42,19 +42,26 @@ def reset_workspace(): else: shutil.rmtree(os.path.join("testdir", entry)) -def assert_port_is_free(port: int = 25565) -> bool: +def assert_port_is_free(port: int = 25565, strict=True) -> bool: """Skips the current test if the given port is not free""" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: s.bind(("127.0.0.1", port)) + return True # windows error except PermissionError: - pytest.skip(reason=f"Port {port} is in use") + if strict: + pytest.skip(reason=f"Port {port} is in use") + else: + return False # linux error? except socket.error as e: if e.errno == errno.EADDRINUSE: - pytest.skip(reason=f"Port {port} is in use") + if strict: + pytest.skip(reason=f"Port {port} is in use") + else: + return False else: # something else raised the socket.error exception raise e @@ -62,8 +69,6 @@ def assert_port_is_free(port: int = 25565) -> bool: def run_vanilla_test_url(url, offline_mode=False, version_name=None): """Run all tests for a single vanilla minecraft server url""" - assert_port_is_free() - setup_workspace() jarfile = download_file(url) @@ -74,7 +79,7 @@ def run_vanilla_test(jarfile, offline_mode=False, version_name=None): """Run all tests for a single vanilla minecraft server jar""" port = 25565 - while not assert_port_is_free(port): + while not assert_port_is_free(port, False): port = randint(25500, 25600) if not offline_mode: diff --git a/mcserverwrapper/test/test_wrapper.py b/mcserverwrapper/test/test_wrapper.py index 7a86e13..dbb2fed 100644 --- a/mcserverwrapper/test/test_wrapper.py +++ b/mcserverwrapper/test/test_wrapper.py @@ -1,6 +1,7 @@ """Module containing tests for the mcserverwrapper""" import os +from random import randint from time import sleep from datetime import datetime, timedelta @@ -72,12 +73,19 @@ def test_single_vanilla_offline(newest_server_jar): def test_mineflayer(newest_server_jar): """Test the mineflayer bot""" - assert_port_is_free() + port = 25565 + while not assert_port_is_free(port, False): + port = randint(25500, 25600) start_cmd = f"java -Xmx2G -jar {newest_server_jar} nogui" + server_params = { + "port": port + } + wrapper = Wrapper(os.path.join(os.getcwd(), "testdir", newest_server_jar), server_start_command=start_cmd, + server_property_args=server_params, print_output=False) wrapper.startup() assert wrapper.server_running()