From 82fe3f6f6edeb7f09ae358bad257464f6d724d36 Mon Sep 17 00:00:00 2001 From: Ronny Fahlberg <86288939+rfahlberg@users.noreply.github.com> Date: Mon, 22 May 2023 18:04:55 +0200 Subject: [PATCH] Maint/unit tests fixes (#130) --- src/ansys/optislang/core/tcp_osl_server.py | 33 +-- tests/test_examples.py | 33 +-- tests/test_optislang_termination.py | 94 +++++--- tests/test_start_stop_combinations.py | 2 - tests/test_tcp_osl_server.py | 241 +++++++++++---------- 5 files changed, 205 insertions(+), 198 deletions(-) diff --git a/src/ansys/optislang/core/tcp_osl_server.py b/src/ansys/optislang/core/tcp_osl_server.py index 970398715..0f9bf28b3 100644 --- a/src/ansys/optislang/core/tcp_osl_server.py +++ b/src/ansys/optislang/core/tcp_osl_server.py @@ -7,7 +7,6 @@ from pathlib import Path from queue import Queue import re -import select import signal import socket import struct @@ -400,29 +399,17 @@ def _recv_response_length(self, timeout: Union[float, None]) -> int: response_len = -1 bytes_to_receive = self.__class__._RESPONSE_SIZE_BYTES + # read from socket until response size (twice) has been received - while True: - try: - # Test if we will be able to read something from the connection. - readable_sockets, _, _ = select.select([self.__socket], [], [], 1) - if self.__socket in readable_sockets: - # Read and convert response size. Assume server sent response size twice. - # Sizes need to match. - response_len_1 = struct.unpack("!Q", self.__socket.recv(bytes_to_receive))[0] - response_len_2 = struct.unpack("!Q", self.__socket.recv(bytes_to_receive))[0] - if response_len_1 != response_len_2: - raise ResponseFormatError("The message size values do not match.") - - response_len = response_len_1 - if response_len >= 0: - break - except Exception as e: - self._logger.debug(e) - pass - now = time.time() - elapsed = now - start_time - if timeout is not None and elapsed > timeout: - raise TimeoutError("Time to receive message length has expired.") + response_len_1 = struct.unpack("!Q", self._receive_bytes(bytes_to_receive, timeout))[0] + response_len_2 = struct.unpack("!Q", self._receive_bytes(bytes_to_receive, timeout))[0] + + if response_len_1 != response_len_2: + raise ResponseFormatError( + "Server response format unrecognized. Response sizes do not match." + ) + + response_len = response_len_1 return response_len diff --git a/tests/test_examples.py b/tests/test_examples.py index 9ad94a49d..84668c01a 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -1,8 +1,8 @@ from contextlib import nullcontext as does_not_raise import os import pathlib -import time +import matplotlib.pyplot as plt import pytest pytestmark = pytest.mark.local_osl @@ -26,7 +26,6 @@ def test_01_ten_bar_truss(): name = "01_ten_bar_truss" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -36,7 +35,6 @@ def test_02_1_oscillator_robustness(): name = "02_1_oscillator_robustness" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -46,7 +44,6 @@ def test_02_2_oscillator_python_system(): name = "02_2_oscillator_python_system" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -56,7 +53,6 @@ def test_02_3_oscillator_optimization_on_EA(): name = "02_3_oscillator_optimization_on_EA" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -66,7 +62,6 @@ def test_02_4_oscillator_MOP_sensitivity_and_optimization(): name = "02_4_oscillator_MOP_sensitivity_and_optimization" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -76,7 +71,6 @@ def test_02_5_oscillator_calibration_systems(): name = "02_5_oscillator_calibration_systems" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -86,18 +80,18 @@ def test_03_etk_abaqus(): name = "03_etk_abaqus" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None -def test_04_python_node_and_help(): - """Test 04_python_node_and_help.py.""" - with does_not_raise() as dnr: - name = "04_python_node_and_help" - file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] - exec(open(file).read()) - time.sleep(5) - assert dnr is None +# The use of "help" function (intended for interactive use) in this example, +# leads to a straying osl process. Deactivate this test for now. +# def test_04_python_node_and_help(): +# """Test 04_python_node_and_help.py.""" +# with does_not_raise() as dnr: +# name = "04_python_node_and_help" +# file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] +# exec(open(file).read()) +# assert dnr is None def test_05_optimizer_settings(): @@ -106,7 +100,6 @@ def test_05_optimizer_settings(): name = "05_optimizer_settings" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -116,7 +109,6 @@ def test_06_sensitivity_settings(): name = "06_sensitivity_settings" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -126,7 +118,6 @@ def test_07_simple_calculator(): name = "07_simple_calculator" file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0] exec(open(file).read()) - time.sleep(5) assert dnr is None @@ -139,11 +130,11 @@ def test_07_simple_calculator(): evaluate_design_example_files_paths.append(os.path.join(evaluate_design_examples_dir, file)) -def test_01_ten_bar_truss_evaluate_design(): +def test_01_ten_bar_truss_evaluate_design(monkeypatch): """Test 01_ten_bar_truss_evaluate_design.py.""" with does_not_raise() as dnr: name = "01_ten_bar_truss" file = list(filter(lambda path: name in path, evaluate_design_example_files_paths))[0] + monkeypatch.setattr(plt, "show", lambda: None) exec(open(file).read()) - time.sleep(5) assert dnr is None diff --git a/tests/test_optislang_termination.py b/tests/test_optislang_termination.py index 4a2fd6924..465e417c2 100644 --- a/tests/test_optislang_termination.py +++ b/tests/test_optislang_termination.py @@ -1,5 +1,9 @@ +from contextlib import closing from contextlib import nullcontext as does_not_raise +import os +from pathlib import Path import socket +import tempfile import time import pytest @@ -8,7 +12,40 @@ from ansys.optislang.core.errors import ConnectionNotEstablishedError, OslCommunicationError _host = socket.gethostbyname(socket.gethostname()) -_port = 5310 + + +def find_free_port(): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + s.bind(("", 0)) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + return s.getsockname()[1] + + +def create_osl_server_process(shutdown_on_finished=False) -> OslServerProcess: + port = find_free_port() + with tempfile.TemporaryDirectory() as temp_dir: + server_info_file = Path(temp_dir) / "osl_server_info.ini" + osl_server_process = OslServerProcess( + shutdown_on_finished=shutdown_on_finished, + port_range=(port, port), + server_info=server_info_file, + ) + osl_server_process.start() + + start_timeout = 30 + time_counter = 0 + while not os.path.exists(server_info_file): + time.sleep(1) + time_counter += 1 + if time_counter > start_timeout: + break + + if os.path.exists(server_info_file): + return osl_server_process + osl_server_process.terminate() + raise TimeoutError("optiSLang Process start timed out") + + pytestmark = pytest.mark.local_osl @@ -23,9 +60,11 @@ ], ) def test_local_default_cm(send_dispose, send_shutdown, osl_none): + osl_port = None with Optislang(shutdown_on_finished=True) as osl: version = osl.get_osl_version() osl.start() + osl_port = osl._Optislang__osl_server._TcpOslServer__port if send_dispose: osl.dispose() if send_shutdown: @@ -34,7 +73,6 @@ def test_local_default_cm(send_dispose, send_shutdown, osl_none): osl = None # server not running - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -43,9 +81,8 @@ def test_local_default_cm(send_dispose, send_shutdown, osl_none): RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) @pytest.mark.parametrize( @@ -58,9 +95,11 @@ def test_local_default_cm(send_dispose, send_shutdown, osl_none): ], ) def test_local_shutdown_on_finished_false_cm(send_dispose, send_shutdown, osl_none): + osl_port = None with Optislang(shutdown_on_finished=False) as osl: version = osl.get_osl_version() osl.start() + osl_port = osl._Optislang__osl_server._TcpOslServer__port if send_dispose: osl.dispose() if send_shutdown: @@ -70,12 +109,10 @@ def test_local_shutdown_on_finished_false_cm(send_dispose, send_shutdown, osl_no if not send_shutdown: with does_not_raise() as dnr: - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) assert dnr is None else: - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -84,9 +121,8 @@ def test_local_shutdown_on_finished_false_cm(send_dispose, send_shutdown, osl_no RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) @pytest.mark.parametrize( @@ -100,11 +136,9 @@ def test_local_shutdown_on_finished_false_cm(send_dispose, send_shutdown, osl_no ) def test_remote_cm(send_dispose, send_shutdown, osl_none): # start optiSLang server - osl_server_process = OslServerProcess(shutdown_on_finished=False) - osl_server_process.start() - time.sleep(5) + osl_server_process = create_osl_server_process(shutdown_on_finished=False) # connect to running optiSLang server - with Optislang(host=_host, port=_port) as osl: + with Optislang(host=_host, port=osl_server_process.port_range[0]) as osl: version = osl.get_osl_version() osl.start() if send_dispose: @@ -116,12 +150,10 @@ def test_remote_cm(send_dispose, send_shutdown, osl_none): if not send_shutdown: with does_not_raise() as dnr: - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_server_process.port_range[0], ini_timeout=10) osl.shutdown() - time.sleep(5) assert dnr is None else: - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -130,9 +162,8 @@ def test_remote_cm(send_dispose, send_shutdown, osl_none): RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_server_process.port_range[0], ini_timeout=10) osl.shutdown() - time.sleep(5) # %% WITHOUT CM @@ -147,13 +178,13 @@ def test_local_default_wocm(send_dispose, send_shutdown): osl = Optislang(shutdown_on_finished=True) version = osl.get_osl_version() osl.start() + osl_port = osl._Optislang__osl_server._TcpOslServer__port if send_dispose: osl.dispose() if send_shutdown: osl.shutdown() # server not running - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -162,9 +193,8 @@ def test_local_default_wocm(send_dispose, send_shutdown): RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) @pytest.mark.parametrize( @@ -178,6 +208,7 @@ def test_local_shutdown_on_finished_false_wocm(send_dispose, send_shutdown): osl = Optislang(shutdown_on_finished=False) version = osl.get_osl_version() osl.start() + osl_port = osl._Optislang__osl_server._TcpOslServer__port if send_dispose: osl.dispose() if send_shutdown: @@ -185,12 +216,10 @@ def test_local_shutdown_on_finished_false_wocm(send_dispose, send_shutdown): if not send_shutdown: with does_not_raise() as dnr: - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) assert dnr is None else: - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -199,9 +228,8 @@ def test_local_shutdown_on_finished_false_wocm(send_dispose, send_shutdown): RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_port, ini_timeout=10) osl.shutdown() - time.sleep(5) @pytest.mark.parametrize( @@ -212,12 +240,11 @@ def test_local_shutdown_on_finished_false_wocm(send_dispose, send_shutdown): ], ) def test_remote_wocm(send_dispose, send_shutdown): + port = find_free_port() # start optiSLang server - osl_server_process = OslServerProcess(shutdown_on_finished=False) - osl_server_process.start() - time.sleep(5) + osl_server_process = create_osl_server_process(shutdown_on_finished=False) # connect to running optiSLang server - osl = Optislang(host=_host, port=_port) + osl = Optislang(host=_host, port=osl_server_process.port_range[0]) version = osl.get_osl_version() osl.start() if send_dispose: @@ -227,12 +254,10 @@ def test_remote_wocm(send_dispose, send_shutdown): if not send_shutdown: with does_not_raise() as dnr: - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_server_process.port_range[0], ini_timeout=10) osl.shutdown() - time.sleep(5) assert dnr is None else: - time.sleep(5) with pytest.raises( ( ConnectionNotEstablishedError, @@ -241,6 +266,5 @@ def test_remote_wocm(send_dispose, send_shutdown): RuntimeError, ) ): - osl = Optislang(host="127.0.0.1", port=5310, ini_timeout=10) + osl = Optislang(host="127.0.0.1", port=osl_server_process.port_range[0], ini_timeout=10) osl.shutdown() - time.sleep(5) diff --git a/tests/test_start_stop_combinations.py b/tests/test_start_stop_combinations.py index 4ad2ae7a0..4272cfc79 100644 --- a/tests/test_start_stop_combinations.py +++ b/tests/test_start_stop_combinations.py @@ -1,6 +1,5 @@ """Test different start/stop/stop_gently combinations with Optislang class.""" from contextlib import nullcontext as does_not_raise -import time import pytest @@ -44,5 +43,4 @@ def test_combinations(optislang: Optislang, input, expected): if method[0] == "stop": optislang.stop(method[1]) optislang.dispose() - time.sleep(5) assert dnr is expected diff --git a/tests/test_tcp_osl_server.py b/tests/test_tcp_osl_server.py index deaed893c..f2d258adf 100644 --- a/tests/test_tcp_osl_server.py +++ b/tests/test_tcp_osl_server.py @@ -1,8 +1,10 @@ +from contextlib import closing from contextlib import nullcontext as does_not_raise import logging import os from pathlib import Path import socket +import tempfile import time import uuid @@ -12,7 +14,13 @@ import ansys.optislang.core.tcp_osl_server as tos _host = socket.gethostbyname(socket.gethostname()) -_port = 5310 + + +def find_free_port(): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + s.bind(("", 0)) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + return s.getsockname()[1] _msg = '{ "What": "SYSTEMS_STATUS_INFO" }' @@ -21,14 +29,36 @@ pytestmark = pytest.mark.local_osl +def create_osl_server_process(shutdown_on_finished=False, project_path=None) -> OslServerProcess: + port = find_free_port() + with tempfile.TemporaryDirectory() as temp_dir: + server_info_file = Path(temp_dir) / "osl_server_info.ini" + osl_server_process = OslServerProcess( + shutdown_on_finished=shutdown_on_finished, + project_path=project_path, + port_range=(port, port), + server_info=server_info_file, + ) + osl_server_process.start() + + start_timeout = 30 + time_counter = 0 + while not os.path.exists(server_info_file): + time.sleep(1) + time_counter += 1 + if time_counter > start_timeout: + break + + if os.path.exists(server_info_file): + return osl_server_process + osl_server_process.terminate() + raise TimeoutError("optiSLang Process start timed out") + + @pytest.fixture(scope="function", autouse=False) def osl_server_process(): - time.sleep(2) # Will be executed before each test - osl_server_process = OslServerProcess(shutdown_on_finished=False) - osl_server_process.start() - time.sleep(5) - return osl_server_process + return create_osl_server_process(shutdown_on_finished=False) @pytest.fixture(scope="function", autouse=False) @@ -55,8 +85,7 @@ def tcp_client() -> tos.TcpClient: return tos.TcpClient() -@pytest.fixture(scope="function", autouse=False) -def tcp_osl_server() -> tos.TcpOslServer: +def create_tcp_osl_server(osl_server_process: OslServerProcess) -> tos.TcpOslServer: """Create TcpOslServer. Parameters @@ -71,7 +100,7 @@ def tcp_osl_server() -> tos.TcpOslServer: TcpOslServer: Class which provides access to optiSLang server using plain TCP/IP communication protocol. """ - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = tos.TcpOslServer(host=_host, port=osl_server_process.port_range[0]) tcp_osl_server.set_timeout(timeout=10) return tcp_osl_server @@ -80,7 +109,7 @@ def tcp_osl_server() -> tos.TcpOslServer: def test_client_properties(osl_server_process: OslServerProcess, tcp_client: tos.TcpClient): "Test ``local_address`` and ``remote_address``." with does_not_raise() as dnr: - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) ra = tcp_client.remote_address assert isinstance(ra, tuple) assert isinstance(ra[0], str) @@ -97,7 +126,7 @@ def test_client_properties(osl_server_process: OslServerProcess, tcp_client: tos def test_connect_and_disconnect(osl_server_process: OslServerProcess, tcp_client: tos.TcpClient): "Test ``connect``." with does_not_raise() as dnr: - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) tcp_client.disconnect() osl_server_process.terminate() assert dnr is None @@ -105,7 +134,7 @@ def test_connect_and_disconnect(osl_server_process: OslServerProcess, tcp_client def test_tcpclient_properties(osl_server_process: OslServerProcess, tcp_client: tos.TcpClient): "Test clients properties." - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) remote_address = tcp_client.remote_address assert isinstance(remote_address, tuple) assert isinstance(remote_address[0], str) @@ -121,7 +150,7 @@ def test_tcpclient_properties(osl_server_process: OslServerProcess, tcp_client: def test_send_msg(osl_server_process: OslServerProcess, tcp_client: tos.TcpClient): "Test ``send_msg`" with does_not_raise() as dnr: - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) tcp_client.send_msg(_msg) tcp_client.disconnect() osl_server_process.terminate() @@ -145,7 +174,7 @@ def test_send_file( with open(file_path, "w") as testfile: testfile.write(_msg) with does_not_raise() as dnr: - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) tcp_client.send_file(file_path) tcp_client.disconnect() osl_server_process.terminate() @@ -154,7 +183,7 @@ def test_send_file( def test_receive_msg(osl_server_process: OslServerProcess, tcp_client: tos.TcpClient): "Test ``receive_msg``." - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) tcp_client.send_msg(_msg) msg = tcp_client.receive_msg() tcp_client.disconnect() @@ -180,7 +209,7 @@ def test_receive_file( with open(file_path, "w") as testfile: testfile.write(_msg) - tcp_client.connect(host=_host, port=_port) + tcp_client.connect(host=_host, port=osl_server_process.port_range[0]) tcp_client.send_file(file_path) with does_not_raise() as dnr: tcp_client.receive_file(received_path) @@ -253,7 +282,8 @@ def test_start_stop_listening( # TcpOslServer -# def test_close(tcp_osl_server: tos.TcpOslServer): +# def test_close(osl_server_process: OslServerProcess): +# tcp_osl_server = create_tcp_osl_server(osl_server_process) # with does_not_raise() as dnr: # tcp_osl_server.close() # tcp_osl_server.new() @@ -263,13 +293,10 @@ def test_start_stop_listening( def test_evaluate_design(tmp_path: Path): "Test ``evaluate_design``." - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) tcp_osl_server.save_copy(file_path=tmp_path / "test_evaluate_design.opf") tcp_osl_server.reset() result = tcp_osl_server.evaluate_design({"a": 5, "b": 10}) @@ -288,13 +315,10 @@ def test_evaluate_design(tmp_path: Path): ) def test_get_actor_info(uid, expected): """Test ``get_actor_info``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): info = tcp_osl_server.get_actor_info(uid) @@ -314,13 +338,10 @@ def test_get_actor_info(uid, expected): ) def test_get_actor_properties(uid, expected): """Test ``get_actor_properties``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): properties = tcp_osl_server.get_actor_properties(uid) @@ -340,13 +361,10 @@ def test_get_actor_properties(uid, expected): ) def test_get_actor_states(uid, expected): """Test ``get_actor_states``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): states = tcp_osl_server.get_actor_states(uid) @@ -367,13 +385,10 @@ def test_get_actor_states(uid, expected): ) def test_get_actor_status_info(uid, expected): """Test ``get_actor_status_info``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): status_info = tcp_osl_server.get_actor_status_info(uid, "0") @@ -394,13 +409,10 @@ def test_get_actor_status_info(uid, expected): ) def test_get_actor_supports(uid, feature_name, expected): """Test ``get_actor_status_info``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): status_info = tcp_osl_server.get_actor_supports(uid, feature_name) @@ -411,10 +423,9 @@ def test_get_actor_supports(uid, feature_name, expected): tcp_osl_server.dispose() -def test_get_full_project_status_info( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_full_project_status_info(osl_server_process: OslServerProcess): """Test ``get_full_project_status_info``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) status_info = tcp_osl_server.get_full_project_status_info() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -422,10 +433,9 @@ def test_get_full_project_status_info( assert bool(status_info) -def test_get_full_project_tree( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_full_project_tree(osl_server_process: OslServerProcess): """Test ``get_full_project_tree``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_tree = tcp_osl_server.get_full_project_tree() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -433,10 +443,9 @@ def test_get_full_project_tree( assert bool(project_tree) -def test_get_full_project_tree_with_properties( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_full_project_tree_with_properties(osl_server_process: OslServerProcess): """Test `get_full_project_tree_with_properties`.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_tree = tcp_osl_server.get_full_project_tree_with_properties() assert isinstance(project_tree, dict) tcp_osl_server.shutdown() @@ -445,13 +454,10 @@ def test_get_full_project_tree_with_properties( def test_get_hpc_licensing_forwarded_environment(): """Test ``get_hpc_licensing_forwarded_environment``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) hpc_licensing = tcp_osl_server.get_hpc_licensing_forwarded_environment( "3577cb69-15b9-4ad1-a53c-ac8af8aaea82" ) @@ -471,13 +477,10 @@ def test_get_hpc_licensing_forwarded_environment(): ) def test_get_input_slot_value(uid, hid, slot_name, expected): """Test ``get_input_slot_value``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): input_slot_value = tcp_osl_server.get_input_slot_value(uid, hid, slot_name) @@ -500,13 +503,10 @@ def test_get_input_slot_value(uid, hid, slot_name, expected): ) def test_get_output_slot_value(uid, hid, slot_name, expected): """Test ``get_output_slot_value``.""" - time.sleep(2) - osl_server_process = OslServerProcess( + osl_server_process = create_osl_server_process( shutdown_on_finished=False, project_path=parametric_project ) - osl_server_process.start() - time.sleep(5) - tcp_osl_server = tos.TcpOslServer(host=_host, port=_port) + tcp_osl_server = create_tcp_osl_server(osl_server_process) if expected == errors.OslCommandError: with pytest.raises(expected): output_slot_value = tcp_osl_server.get_output_slot_value(uid, hid, slot_name) @@ -518,10 +518,9 @@ def test_get_output_slot_value(uid, hid, slot_name, expected): tcp_osl_server.dispose() -def test_get_project_tree_systems( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_project_tree_systems(osl_server_process: OslServerProcess): """Test `get_project_tree_systems`.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_tree_systems = tcp_osl_server.get_project_tree_systems() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -529,18 +528,18 @@ def test_get_project_tree_systems( assert bool(dict) -def test_get_project_tree_systems_with_properties( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_project_tree_systems_with_properties(osl_server_process: OslServerProcess): """Test `get_project_tree_systems_with_properties`.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_tree_systems = tcp_osl_server.get_project_tree_systems_with_properties() assert isinstance(project_tree_systems, dict) tcp_osl_server.shutdown() tcp_osl_server.dispose() -def test_get_server_info(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_server_info(osl_server_process: OslServerProcess): """Test ``_get_server_info``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) server_info = tcp_osl_server._get_server_info() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -548,10 +547,9 @@ def test_get_server_info(osl_server_process: OslServerProcess, tcp_osl_server: t assert bool(server_info) -def test_get_basic_project_info( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_basic_project_info(osl_server_process: OslServerProcess): """Test ``_get_basic_project_info``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) basic_project_info = tcp_osl_server._get_basic_project_info() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -559,10 +557,9 @@ def test_get_basic_project_info( assert bool(basic_project_info) -def test_get_osl_version_string( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_osl_version_string(osl_server_process: OslServerProcess): """Test ``get_osl_version_string``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) version = tcp_osl_server.get_osl_version_string() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -570,8 +567,9 @@ def test_get_osl_version_string( assert bool(version) -def test_get_osl_version(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_osl_version(osl_server_process: OslServerProcess): """Test ``get_osl_version``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) major_version, minor_version, maintenance_version, revision = tcp_osl_server.get_osl_version() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -581,10 +579,9 @@ def test_get_osl_version(osl_server_process: OslServerProcess, tcp_osl_server: t assert isinstance(revision, int) or revision == None -def test_get_project_description( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_project_description(osl_server_process: OslServerProcess): """Test ``get_project_description``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_description = tcp_osl_server.get_project_description() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -592,10 +589,9 @@ def test_get_project_description( assert not bool(project_description) -def test_get_project_location( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_project_location(osl_server_process: OslServerProcess): """Test ``get_project_location``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_location = tcp_osl_server.get_project_location() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -603,8 +599,9 @@ def test_get_project_location( assert bool(project_location) -def test_get_project_name(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_project_name(osl_server_process: OslServerProcess): """Test ``get_project_name``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_name = tcp_osl_server.get_project_name() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -612,8 +609,9 @@ def test_get_project_name(osl_server_process: OslServerProcess, tcp_osl_server: assert bool(project_name) -def test_get_project_status(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_project_status(osl_server_process: OslServerProcess): """Test ``get_get_project_status``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project_status = tcp_osl_server.get_project_status() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -621,18 +619,18 @@ def test_get_project_status(osl_server_process: OslServerProcess, tcp_osl_server assert bool(project_status) -def test_get_server_is_alive( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_server_is_alive(osl_server_process: OslServerProcess): """Test ``get_server_is_alive``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) is_alive = tcp_osl_server.get_server_is_alive() tcp_osl_server.shutdown() tcp_osl_server.dispose() assert isinstance(is_alive, bool) -def test_get_set_timeout(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_set_timeout(osl_server_process: OslServerProcess): """Test ``get_get_timeout``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) timeout = tcp_osl_server.get_timeout() assert isinstance(timeout, (int, float)) assert timeout == 10 @@ -644,10 +642,9 @@ def test_get_set_timeout(osl_server_process: OslServerProcess, tcp_osl_server: t tcp_osl_server.dispose() -def test_get_systems_status_info( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer -): +def test_get_systems_status_info(osl_server_process: OslServerProcess): """Test `get_project_tree_systems_with_properties`.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) systems_status_info = tcp_osl_server.get_systems_status_info() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -655,8 +652,9 @@ def test_get_systems_status_info( assert bool(dict) -def test_get_working_dir(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_working_dir(osl_server_process: OslServerProcess): """Test ``get_working_dir``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) working_dir = tcp_osl_server.get_working_dir() tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -664,10 +662,9 @@ def test_get_working_dir(osl_server_process: OslServerProcess, tcp_osl_server: t assert bool(working_dir) -def test_new( - osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer, tmp_path: Path -): +def test_new(osl_server_process: OslServerProcess, tmp_path: Path): """Test ``new``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) tcp_osl_server.new() assert tcp_osl_server.get_project_name() == "Unnamed project" tcp_osl_server.save_as(file_path=tmp_path / "newProject.opf") @@ -676,8 +673,9 @@ def test_new( @pytest.mark.parametrize("path_type", [str, Path]) -def test_open(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer, path_type): +def test_open(osl_server_process: OslServerProcess, path_type): """Test ``open``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) project = examples.get_files("simple_calculator")[1][0] assert project.is_file() if path_type == str: @@ -688,8 +686,9 @@ def test_open(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslSe tcp_osl_server.dispose() -def test_reset(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_reset(osl_server_process: OslServerProcess): """Test ``reset``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) with does_not_raise() as dnr: tcp_osl_server.reset() tcp_osl_server.shutdown() @@ -700,7 +699,6 @@ def test_reset(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslS @pytest.mark.parametrize("path_type", [str, Path]) def test_run_python_file( osl_server_process: OslServerProcess, - tcp_osl_server: tos.TcpOslServer, tmp_path: Path, path_type, ): @@ -719,13 +717,14 @@ def test_run_python_file( with open(cmd_path, "w") as f: f.write(cmd) + tcp_osl_server = create_tcp_osl_server(osl_server_process) run_file = tcp_osl_server.run_python_file(file_path=cmd_path) tcp_osl_server.shutdown() tcp_osl_server.dispose() assert isinstance(run_file, tuple) -def test_run_python_script(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_run_python_script(osl_server_process: OslServerProcess): """Test ``run_python_script``.""" cmd = """ a = 5 @@ -733,14 +732,16 @@ def test_run_python_script(osl_server_process: OslServerProcess, tcp_osl_server: result = a + b print(result) """ + tcp_osl_server = create_tcp_osl_server(osl_server_process) run_script = tcp_osl_server.run_python_script(script=cmd) tcp_osl_server.shutdown() tcp_osl_server.dispose() assert isinstance(run_script, tuple) -def test_save(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_save(osl_server_process: OslServerProcess): """Test ``save``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) file_path = tcp_osl_server.get_project_location() assert file_path.is_file() mod_time = os.path.getmtime(str(file_path)) @@ -754,7 +755,6 @@ def test_save(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslSe @pytest.mark.parametrize("path_type", [str, Path]) def test_save_as( osl_server_process: OslServerProcess, - tcp_osl_server: tos.TcpOslServer, tmp_path: Path, path_type, ): @@ -765,6 +765,7 @@ def test_save_as( elif path_type == Path: arg_path = file_path + tcp_osl_server = create_tcp_osl_server(osl_server_process) tcp_osl_server.save_as(file_path=arg_path) assert file_path.is_file() tcp_osl_server.shutdown() @@ -775,7 +776,6 @@ def test_save_as( def test_save_copy( osl_server_process: OslServerProcess, tmp_path: Path, - tcp_osl_server: tos.TcpOslServer, path_type, ): """Test ``save_copy``.""" @@ -785,14 +785,16 @@ def test_save_copy( elif path_type == Path: arg_path = copy_path + tcp_osl_server = create_tcp_osl_server(osl_server_process) tcp_osl_server.save_copy(arg_path) tcp_osl_server.shutdown() tcp_osl_server.dispose() assert copy_path.is_file() -def test_set_timeout(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_set_timeout(osl_server_process: OslServerProcess): """Test ``set_timeout``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) assert tcp_osl_server.get_timeout() == 10 with pytest.raises(ValueError): tcp_osl_server.set_timeout(-5) @@ -802,8 +804,9 @@ def test_set_timeout(osl_server_process: OslServerProcess, tcp_osl_server: tos.T tcp_osl_server.dispose() -def test_start(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_start(osl_server_process: OslServerProcess): """Test ``start``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) with does_not_raise() as dnr: tcp_osl_server.start() tcp_osl_server.shutdown() @@ -811,8 +814,9 @@ def test_start(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslS assert dnr is None -def test_stop(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_stop(osl_server_process: OslServerProcess): """Test ``stop``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) with does_not_raise() as dnr: tcp_osl_server.stop() tcp_osl_server.shutdown() @@ -820,16 +824,18 @@ def test_stop(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslSe assert dnr is None -# def test_stop_gently(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +# def test_stop_gently(osl_server_process: OslServerProcess): # """Test ``stop_gently``.""" +# tcp_osl_server = create_tcp_osl_server(osl_server_process) # with does_not_raise() as dnr: # tcp_osl_server.stop_gently() # tcp_osl_server.shutdown() # assert dnr is None -def test_shutdown(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_shutdown(osl_server_process: OslServerProcess): """Test ``shutdown``.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) with does_not_raise() as dnr: tcp_osl_server.shutdown() tcp_osl_server.dispose() @@ -845,8 +851,9 @@ def test_force_shutdown_local_process(): assert dnr is None -def test_get_project_uid(osl_server_process: OslServerProcess, tcp_osl_server: tos.TcpOslServer): +def test_get_project_uid(osl_server_process: OslServerProcess): """Test `project_uid`.""" + tcp_osl_server = create_tcp_osl_server(osl_server_process) uid = tcp_osl_server.get_project_uid() assert isinstance(uid, str) tcp_osl_server.shutdown()