From e606a6252dfa4a395e327d45d8baaebf23effa01 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:12:36 +0200 Subject: [PATCH 01/22] feat: packing locals in launch_mapdl for debugging --- src/ansys/mapdl/core/launcher.py | 76 ++++++++------------------------ 1 file changed, 18 insertions(+), 58 deletions(-) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 51f98937b8..349602c667 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -1663,21 +1663,7 @@ def launch_mapdl( return if _debug_no_launch: - return pack_parameters( - port, - ip, - add_env_vars, - replace_env_vars, - cleanup_on_exit, - loglevel, - set_no_abort, - remove_temp_dir_on_exit, - log_apdl, - use_vtk, - start_parm, - start_instance, - version, - ) # type: ignore + return pack_parameters(locals()) # type: ignore mapdl = MapdlGrpc( ip=ip, @@ -1821,21 +1807,7 @@ def launch_mapdl( elif mode == "grpc": if _debug_no_launch: # Early exit, just for testing - return pack_parameters( - port, - ip, - add_env_vars, - replace_env_vars, - cleanup_on_exit, - loglevel, - set_no_abort, - remove_temp_dir_on_exit, - log_apdl, - use_vtk, - start_parm, - start_instance, - version, - ) # type: ignore + return pack_parameters(locals()) # type: ignore port, actual_run_location, process = launch_grpc( port=port, @@ -2283,34 +2255,22 @@ def get_value( return exec_file, jobname, nproc, ram, additional_switches -def pack_parameters( - port, - ip, - add_env_vars, - replace_env_vars, - cleanup_on_exit, - loglevel, - set_no_abort, - remove_temp_dir_on_exit, - log_apdl, - use_vtk, - start_parm, - start_instance, - version, -): +def pack_parameters(locals_var): # pack all the arguments in a dict for debugging purposes + # We prefer to explicitly output the desired output dict_ = {} - dict_["port"] = port - dict_["ip"] = ip - dict_["add_env_vars"] = add_env_vars - dict_["replace_env_vars"] = replace_env_vars - dict_["cleanup_on_exit"] = cleanup_on_exit - dict_["loglevel"] = loglevel - dict_["set_no_abort"] = set_no_abort - dict_["remove_temp_dir_on_exit"] = remove_temp_dir_on_exit - dict_["log_apdl"] = log_apdl - dict_["use_vtk"] = use_vtk - dict_["start_parm"] = start_parm - dict_["start_instance"] = start_instance - dict_["version"] = version + dict_["port"] = locals_var["port"] + dict_["ip"] = locals_var["ip"] + dict_["add_env_vars"] = locals_var["add_env_vars"] + dict_["replace_env_vars"] = locals_var["replace_env_vars"] + dict_["cleanup_on_exit"] = locals_var["cleanup_on_exit"] + dict_["loglevel"] = locals_var["loglevel"] + dict_["set_no_abort"] = locals_var["set_no_abort"] + dict_["remove_temp_dir_on_exit"] = locals_var["remove_temp_dir_on_exit"] + dict_["log_apdl"] = locals_var["log_apdl"] + dict_["use_vtk"] = locals_var["use_vtk"] + dict_["start_parm"] = locals_var["start_parm"] + dict_["start_instance"] = locals_var["start_instance"] + dict_["version"] = locals_var["version"] + dict_["additional_switches"] = locals_var["additional_switches"] return dict_ From d66bcb51b47e787802363134cb4b4234cecfadaa Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:20:15 +0200 Subject: [PATCH 02/22] perf: reducing testing time of licensing --- tests/test_launcher.py | 81 +++++++----------------------------------- 1 file changed, 12 insertions(+), 69 deletions(-) diff --git a/tests/test_launcher.py b/tests/test_launcher.py index f497d9ca3e..3362005533 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -24,7 +24,6 @@ import os import tempfile -from time import sleep import warnings import psutil @@ -34,7 +33,6 @@ from ansys.mapdl.core.errors import ( DeprecationError, LicenseServerConnectionError, - MapdlDidNotStart, NotEnoughResources, PortAlreadyInUseByAnMAPDLInstance, ) @@ -184,75 +182,20 @@ def test_launch_console(version): @requires("local") @requires("nostudent") -def test_license_type_keyword(mapdl): - checks = [] - for license_name, license_description in LICENSES.items(): - try: - mapdl_ = launch_mapdl( - license_type=license_name, - start_timeout=start_timeout, - port=mapdl.port + 1, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) - - # Using first line to ensure not picking up other stuff. - checks.append(license_description in mapdl_.__str__().split("\n")[0]) - mapdl_.exit() - del mapdl_ - sleep(2) - - except MapdlDidNotStart as e: - if "ANSYS license not available" in str(e): - continue - else: - raise e - - assert any(checks) - - -@requires("local") -@requires("nostudent") -def test_license_type_keyword_names(mapdl): - # This test might became a way to check available licenses, which is not the purpose. +@pytest.mark.parametrize("license_name", LICENSES) +def test_license_type_keyword_names(mapdl, license_name): + args = launch_mapdl(license_type=license_name, _debug_no_launch=True) + assert f"-p {license_name}" in args["additional_switches"] - successful_check = False - for license_name, license_description in LICENSES.items(): - mapdl_ = launch_mapdl( - license_type=license_name, - start_timeout=start_timeout, - port=mapdl.port + 1, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) - # Using first line to ensure not picking up other stuff. - successful_check = ( - license_description in mapdl_.__str__().split("\n")[0] or successful_check - ) - assert license_description in mapdl_.__str__().split("\n")[0] - mapdl_.exit() - - assert successful_check # if at least one license is ok, this should be true. - - -@requires("local") -@requires("nostudent") -def test_license_type_additional_switch(mapdl): - # This test might became a way to check available licenses, which is not the purpose. - successful_check = False - for license_name, license_description in LICENSES.items(): - mapdl_ = launch_mapdl( - additional_switches=QUICK_LAUNCH_SWITCHES + " -p " + license_name, - start_timeout=start_timeout, - port=mapdl.port + 1, - ) - - # Using first line to ensure not picking up other stuff. - successful_check = ( - license_description in mapdl_.__str__().split("\n")[0] or successful_check - ) - mapdl_.exit() - - assert successful_check # if at least one license is ok, this should be true. +# @requires("local") +@pytest.mark.parametrize("license_name", LICENSES) +def test_license_type_additional_switch(mapdl, license_name): + args = launch_mapdl( + additional_switches=QUICK_LAUNCH_SWITCHES + " -p " + license_name, + _debug_no_launch=True, + ) + assert f"-p {license_name}" in args["additional_switches"] @requires("ansys-tools-path") From abdf809ade79212ca5c0006085b83c16307aa3d0 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:22:28 +0000 Subject: [PATCH 03/22] chore: adding changelog file 3427.added.md --- doc/changelog.d/3427.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3427.added.md diff --git a/doc/changelog.d/3427.added.md b/doc/changelog.d/3427.added.md new file mode 100644 index 0000000000..ce52a9880d --- /dev/null +++ b/doc/changelog.d/3427.added.md @@ -0,0 +1 @@ +perf: reduce-testing-time \ No newline at end of file From 73695d4984f2a55b0e7fc3f282fba16689a41563 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:38:39 +0200 Subject: [PATCH 04/22] perf: improving testing on pool. --- src/ansys/mapdl/core/pool.py | 8 ++++++-- tests/test_pool.py | 10 ++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/ansys/mapdl/core/pool.py b/src/ansys/mapdl/core/pool.py index 28a74e2809..16b8c94159 100755 --- a/src/ansys/mapdl/core/pool.py +++ b/src/ansys/mapdl/core/pool.py @@ -219,7 +219,7 @@ def __init__( self._n_instances = n_instances # Getting debug arguments - _debug_no_launch = kwargs.pop("_debug_no_launch", None) + _debug_no_launch = kwargs.get("_debug_no_launch", None) if run_location is None: run_location = create_temp_dir() @@ -338,7 +338,6 @@ def __init__( "exec_file": exec_file, "n_instances": n_instances, } - return # Converting ip or hostname to ip self._ips = [socket.gethostbyname(each) for each in self._ips] @@ -357,6 +356,11 @@ def __init__( ) for i, (ip, port) in enumerate(zip(ips, ports)) ] + + # Early exit due to debugging + if _debug_no_launch: + return + if wait: [thread.join() for thread in threads] diff --git a/tests/test_pool.py b/tests/test_pool.py index 467b4e285b..17ddaf05a3 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -22,7 +22,6 @@ import os from pathlib import Path -import socket import time import numpy as np @@ -276,7 +275,6 @@ def test_directory_names_default_with_restart(self, pool): assert pool._names(i) in dirs_path_pool assert f"Instance_{i}" in dirs_path_pool - @requires("local") @skip_if_ignore_pool def test_directory_names_custom_string(self, tmpdir): pool = MapdlPool( @@ -287,6 +285,7 @@ def test_directory_names_custom_string(self, tmpdir): names="my_instance", port=50056, additional_switches=QUICK_LAUNCH_SWITCHES, + _debug_no_launch=True, ) dirs_path_pool = os.listdir(pool._root_dir) @@ -295,7 +294,6 @@ def test_directory_names_custom_string(self, tmpdir): pool.exit(block=True) - @requires("local") @skip_if_ignore_pool def test_directory_names_function(self, tmpdir): def myfun(i): @@ -313,6 +311,7 @@ def myfun(i): names=myfun, run_location=tmpdir, additional_switches=QUICK_LAUNCH_SWITCHES, + _debug_no_launch=True, ) dirs_path_pool = os.listdir(pool._root_dir) @@ -417,8 +416,6 @@ def test_multiple_ips(self, monkeypatch): conf = MapdlPool(ip=ips, _debug_no_launch=True)._debug_no_launch - ips = [socket.gethostbyname(each) for each in ips] - assert conf["ips"] == ips assert conf["ports"] == [50052 for i in range(len(ips))] assert conf["start_instance"] is False @@ -786,9 +783,6 @@ def test_ip_port_n_instance( n_instances=n_instances, ip=ip, port=port, _debug_no_launch=True )._debug_no_launch - if exp_ip: - exp_ip = [socket.gethostbyname(each) for each in exp_ip] - assert conf["n_instances"] == exp_n_instances assert len(conf["ips"]) == exp_n_instances assert len(conf["ports"]) == exp_n_instances From b7329694af531033b2908e94174d063e2662299d Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:56:25 +0200 Subject: [PATCH 05/22] perf: checking just sending the message or not. --- tests/test_mapdl.py | 58 +++++++++++---------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index bebf02622e..fc780be80c 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1926,56 +1926,26 @@ def test_igesin_whitespace(mapdl, cleared, tmpdir): assert int(n_ent[0]) > 0 -@requires("local") -@requires("nostudent") -@pytest.mark.xfail(reason="Save on exit is broken.") def test_save_on_exit(mapdl, cleared): - mapdl2 = launch_mapdl( - license_server_check=False, - additional_switches=QUICK_LAUNCH_SWITCHES, - port=PORT1, - ) - mapdl2.parameters["my_par"] = "initial_value" + with mapdl.non_interactive: + mapdl.exit(save=True) - db_name = mapdl2.jobname + ".db" - db_dir = mapdl2.directory - db_path = os.path.join(db_dir, db_name) + lines = "\n".join(mapdl._stored_commands.copy()) + assert "SAVE" in lines.upper() - mapdl2.save(db_name) - assert os.path.exists(db_path) + mapdl._stored_commands = [""] # resetting + mapdl.prep7() - mapdl2.parameters["my_par"] = "final_value" - mapdl2.exit(force=True) - mapdl2 = launch_mapdl( - license_server_check=False, - additional_switches=QUICK_LAUNCH_SWITCHES, - port=PORT1, - ) - mapdl2.resume(db_path) - if mapdl.version >= 24.2: - assert mapdl2.parameters["my_par"] == "initial_value" - else: - # This fails in earlier versions of MAPDL - assert mapdl2.parameters["my_par"] != "initial_value" - assert mapdl2.parameters["my_par"] == "final_value" - - mapdl2.parameters["my_par"] = "new_initial_value" - db_name = mapdl2.jobname + ".db" # reupdating db path - db_dir = mapdl2.directory - db_path = os.path.join(db_dir, db_name) - mapdl2.exit(save=True, force=True) - - mapdl2 = launch_mapdl( - license_server_check=False, - additional_switches=QUICK_LAUNCH_SWITCHES, - port=PORT1, - ) - mapdl2.resume(db_path) - assert mapdl2.parameters["my_par"] == "new_initial_value" +def test_save_on_exit_not(mapdl, cleared): + with mapdl.non_interactive: + mapdl.exit(save=False) + + lines = "\n".join(mapdl._stored_commands.copy()) + assert "SAVE" not in lines.upper() - # cleaning up - mapdl2.exit(force=True) + mapdl._stored_commands = [""] # resetting + mapdl.prep7() def test_input_strings_inside_non_interactive(mapdl, cleared): From 52270f98ea5b02712d920c772227eca7fa904607 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:02:47 +0200 Subject: [PATCH 06/22] fix: find_version test --- tests/test_launcher.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 3362005533..5be0570c31 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -390,7 +390,9 @@ def test_find_ansys(mapdl): assert find_ansys(version=version) is not None # Checking floats - assert find_ansys(version=22.2) is not None + with pytest.raises(ValueError): + find_ansys(version=22.2) + assert find_ansys(version=mapdl.version) is not None with pytest.raises(ValueError): From e86de498db0da222cb1d799e98209fccc0f63278 Mon Sep 17 00:00:00 2001 From: German Martinez Ayuso Date: Mon, 23 Sep 2024 17:49:32 +0200 Subject: [PATCH 07/22] feat: using cache for path --- src/ansys/mapdl/core/mapdl_core.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index e4502ece48..d4a94d0124 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -283,6 +283,7 @@ def __init__( self._start_parm: Dict[str, Any] = start_parm self._jobname: str = start_parm.get("jobname", "file") self._path: Union[str, pathlib.Path] = start_parm.get("run_location", None) + self._path_cache = None # Cache self._print_com: bool = print_com # print the command /COM input. self.check_parameter_names = start_parm.get("check_parameter_names", True) @@ -510,10 +511,14 @@ def directory(self) -> str: # new line to fix path issue, see #416 self._path = repr(self._path)[1:-1] else: # pragma: no cover - raise IOError( - f"The directory returned by /INQUIRE is not valid ('{self._path}')." - ) + if self._path_cache: + return self._path_cache + else: + raise IOError( + f"The directory returned by /INQUIRE is not valid ('{self._path}')." + ) + self._path_cache = self._path # update return self._path @directory.setter From 03d32043226e6fbdda83c55b8770d8177f9c5c89 Mon Sep 17 00:00:00 2001 From: German Martinez Ayuso Date: Mon, 23 Sep 2024 17:49:54 +0200 Subject: [PATCH 08/22] refactor: moving call later. --- src/ansys/mapdl/core/mapdl_grpc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 903622b609..3cafff0abf 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -1069,8 +1069,6 @@ def exit(self, save=False, force=False): elif self._exited: # Already exited. return - else: - mapdl_path = self.directory if save: self._log.debug("Saving MAPDL database") @@ -1095,6 +1093,7 @@ def exit(self, save=False, force=False): self._log.debug("Exiting MAPDL") if self._local: + mapdl_path = self.directory self._cache_pids() # Recache processes if os.name == "nt": From fdc298249008250b5f5a179ff69324e134c54116 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:50:27 +0000 Subject: [PATCH 09/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/ansys/mapdl/core/mapdl_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index d4a94d0124..c2af6dca63 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -518,7 +518,7 @@ def directory(self) -> str: f"The directory returned by /INQUIRE is not valid ('{self._path}')." ) - self._path_cache = self._path # update + self._path_cache = self._path # update return self._path @directory.setter From 121bdff481523008e729d1169a8a13a0ca3f0339 Mon Sep 17 00:00:00 2001 From: German Martinez Ayuso Date: Mon, 23 Sep 2024 17:54:44 +0200 Subject: [PATCH 10/22] chore: empty commit to trigger CICD From 7f1d6f9d95fb4e09e3811331de2fa1edcdcef779 Mon Sep 17 00:00:00 2001 From: German Martinez Ayuso Date: Mon, 23 Sep 2024 18:21:19 +0200 Subject: [PATCH 11/22] fix: rename job to pull-request --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46ebc759e6..b948fb1ca9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,12 +84,12 @@ jobs: uses: ansys/actions/branch-name-style@v7 - commit-name: + pull-request-name: if: github.event_name == 'pull_request' - name: Check the name of the commit + name: Check the name of the pull-request runs-on: ubuntu-latest steps: - - name: Check commit name + - name: Check pull-request name uses: ansys/actions/commit-style@v7 with: token: ${{ secrets.GITHUB_TOKEN }} From 594ee189139b81c3c68f4cb0a5ef1dce5781136a Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:02:34 +0200 Subject: [PATCH 12/22] feat: cache directory at starting --- src/ansys/mapdl/core/mapdl_grpc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 3cafff0abf..662aab4b39 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -899,6 +899,7 @@ def _run_at_connect(self): with self.run_as_routine("POST26"): self.numvar(200, mute=True) + self.inquire("", "DIRECTORY") self.show(self._file_type_for_plots) self.version # Caching version self.file_type_for_plots # Setting /show,png and caching it. From 61c33f8144598227b87d5e6e62eec32cda95dc78 Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:26:13 +0000 Subject: [PATCH 13/22] feat: adding 'fake_exit' to mapdl.exit --- src/ansys/mapdl/core/mapdl_grpc.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 662aab4b39..a9313a8e1a 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -1039,7 +1039,7 @@ def _threaded_heartbeat(self): continue @protect_from(ValueError, "I/O operation on closed file.") - def exit(self, save=False, force=False): + def exit(self, save=False, force=False, **kwargs): """Exit MAPDL. Parameters @@ -1093,18 +1093,21 @@ def exit(self, save=False, force=False): self._exiting = True self._log.debug("Exiting MAPDL") - if self._local: - mapdl_path = self.directory - self._cache_pids() # Recache processes + if not kwargs.pop("fake_exit", False): + # This cannot should not be faked + if self._local: + mapdl_path = self.directory + self._cache_pids() # Recache processes - if os.name == "nt": + if os.name == "nt": + self._kill_server() + self._close_process() + self._remove_lock_file(mapdl_path) + else: self._kill_server() - self._close_process() - self._remove_lock_file(mapdl_path) - else: - self._kill_server() self._exited = True + self._exiting = False if self._remote_instance: # pragma: no cover # No cover: The CI is working with a single MAPDL instance From 05bd04d68c301f7463d95dc8406adf85578120fc Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:30:28 +0000 Subject: [PATCH 14/22] feat: adding early exit in flush_stored --- src/ansys/mapdl/core/mapdl_core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index a6033a83bb..1f99aaaf3a 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -1900,6 +1900,9 @@ def _flush_stored(self): Overridden by gRPC. """ + if not self._stored_commands: + return # Early exit in case it is empty + self._log.debug("Flushing stored commands") rnd_str = random_string() tmp_out = os.path.join(tempfile.gettempdir(), f"tmp_{rnd_str}.out") From 224b0f0993de1925c76b327c66c88abae9354203 Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:35:48 +0000 Subject: [PATCH 15/22] fix: test --- tests/test_mapdl.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index fc780be80c..93c0253fd4 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1928,24 +1928,28 @@ def test_igesin_whitespace(mapdl, cleared, tmpdir): def test_save_on_exit(mapdl, cleared): with mapdl.non_interactive: - mapdl.exit(save=True) + mapdl.exit(save=True, fake_exit=True) + mapdl._exited = False # avoiding set exited on the class. lines = "\n".join(mapdl._stored_commands.copy()) assert "SAVE" in lines.upper() - mapdl._stored_commands = [""] # resetting - mapdl.prep7() + mapdl._stored_commands = [] # resetting + + mapdl.prep7() def test_save_on_exit_not(mapdl, cleared): with mapdl.non_interactive: - mapdl.exit(save=False) + mapdl.exit(save=False, fake_exit=True) + mapdl._exited = False # avoiding set exited on the class. lines = "\n".join(mapdl._stored_commands.copy()) assert "SAVE" not in lines.upper() - mapdl._stored_commands = [""] # resetting - mapdl.prep7() + mapdl._stored_commands = [] # resetting + + mapdl.prep7() def test_input_strings_inside_non_interactive(mapdl, cleared): From 2ec8b2b65f1b76a2ba43d63a47cb282e32467bbf Mon Sep 17 00:00:00 2001 From: Ger <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 09:21:49 +0000 Subject: [PATCH 16/22] revert: "feat: adding early exit in flush_stored" This reverts commit 05bd04d68c301f7463d95dc8406adf85578120fc. --- src/ansys/mapdl/core/mapdl_core.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index 1f99aaaf3a..a6033a83bb 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -1900,9 +1900,6 @@ def _flush_stored(self): Overridden by gRPC. """ - if not self._stored_commands: - return # Early exit in case it is empty - self._log.debug("Flushing stored commands") rnd_str = random_string() tmp_out = os.path.join(tempfile.gettempdir(), f"tmp_{rnd_str}.out") From 2a84c40228c1dc58484dd6ea3acf386f98890414 Mon Sep 17 00:00:00 2001 From: Ger <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:44:28 +0000 Subject: [PATCH 17/22] fix: wrong indentation and adding early exit. --- src/ansys/mapdl/core/mapdl_core.py | 36 +++++++++++++++++------------- tests/test_mapdl.py | 13 +++++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index a6033a83bb..76a300376c 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -1900,7 +1900,13 @@ def _flush_stored(self): Overridden by gRPC. """ + if not self._stored_commands: + self._log.debug("There is no commands to be flushed.") + self._store_commands = False + return + self._log.debug("Flushing stored commands") + rnd_str = random_string() tmp_out = os.path.join(tempfile.gettempdir(), f"tmp_{rnd_str}.out") self._stored_commands.insert(0, f"/OUTPUT, {tmp_out}") @@ -1909,25 +1915,25 @@ def _flush_stored(self): if self._apdl_log: self._apdl_log.write(commands + "\n") - self._store_commands = False - self._stored_commands = [] + self._store_commands = False + self._stored_commands = [] - # write to a temporary input file - self._log.debug( - "Writing the following commands to a temporary " "apdl input file:\n%s", - commands, - ) + # write to a temporary input file + self._log.debug( + "Writing the following commands to a temporary " "apdl input file:\n%s", + commands, + ) - tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{random_string()}.inp") - with open(tmp_inp, "w") as f: - f.writelines(commands) + tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{random_string()}.inp") + with open(tmp_inp, "w") as f: + f.writelines(commands) - # interactive result - _ = self.input(tmp_inp, write_to_log=False) + # interactive result + _ = self.input(tmp_inp, write_to_log=False) - time.sleep(0.1) # allow MAPDL to close the file - if os.path.isfile(tmp_out): - self._response = "\n" + open(tmp_out).read() + time.sleep(0.1) # allow MAPDL to close the file + if os.path.isfile(tmp_out): + self._response = "\n" + open(tmp_out).read() if self._response is None: # pragma: no cover self._log.warning("Unable to read response from flushed commands") diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 93c0253fd4..1de6fe4958 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1935,6 +1935,7 @@ def test_save_on_exit(mapdl, cleared): assert "SAVE" in lines.upper() mapdl._stored_commands = [] # resetting + mapdl.prep7() mapdl.prep7() @@ -1948,6 +1949,7 @@ def test_save_on_exit_not(mapdl, cleared): assert "SAVE" not in lines.upper() mapdl._stored_commands = [] # resetting + mapdl.prep7() mapdl.prep7() @@ -2444,3 +2446,14 @@ def test_cleanup_loggers(mapdl): assert mapdl.logger is not None assert mapdl.logger.std_out_handler is None assert mapdl.logger.file_handler is None + + +def test_no_flush_stored(mapdl): + assert not mapdl._store_commands + mapdl._store_commands = True + mapdl._stored_commands = [] + + mapdl._flush_stored() + + assert not mapdl._store_commands + assert mapdl._stored_commands == [] From 70cc6d042d1863abba6881c45f18c3c9be207494 Mon Sep 17 00:00:00 2001 From: Ger <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:22:17 +0000 Subject: [PATCH 18/22] fix: adding early exit to _flush_stored in mapdlgrpc. --- src/ansys/mapdl/core/mapdl_grpc.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index a9313a8e1a..632c38b80b 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2034,6 +2034,11 @@ def _flush_stored(self): """Writes stored commands to an input file and runs the input file. Used with non_interactive. """ + if not self._stored_commands: + self._log.debug("There is no commands to be flushed.") + self._store_commands = False + return + self._log.debug("Flushing stored commands") commands = "\n".join(self._stored_commands) From 8d00cdd8bd53874a8d5889de700a22ecba3a2729 Mon Sep 17 00:00:00 2001 From: Ger <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:27:15 +0000 Subject: [PATCH 19/22] test: reducing testing time in test_only_one_instance --- tests/test_pool.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 17ddaf05a3..59633cf4a4 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -333,16 +333,17 @@ def test_num_instances(self): @skip_if_ignore_pool @requires("local") def test_only_one_instance(self): - pool = MapdlPool( + pool_ = MapdlPool( 1, exec_file=EXEC_FILE, nproc=NPROC, additional_switches=QUICK_LAUNCH_SWITCHES, + _debug_no_launch=True, ) - pool_sz = len(pool) - _ = pool.map(lambda mapdl: mapdl.prep7()) - assert len(pool) == pool_sz - pool.exit() + args = pool_._debug_no_launch + pool_sz = len(pool_) + assert len(args["ips"]) == 1 + assert len(args["port"]) == 1 def test_ip(self, monkeypatch): monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) From 762240ad340aceba49b3a5984b860bd27493dc88 Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:47:50 +0000 Subject: [PATCH 20/22] fix: test key --- tests/test_pool.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 59633cf4a4..37d8199520 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -287,13 +287,11 @@ def test_directory_names_custom_string(self, tmpdir): additional_switches=QUICK_LAUNCH_SWITCHES, _debug_no_launch=True, ) - dirs_path_pool = os.listdir(pool._root_dir) + assert "my_instance_0" in dirs_path_pool assert "my_instance_1" in dirs_path_pool - pool.exit(block=True) - @skip_if_ignore_pool def test_directory_names_function(self, tmpdir): def myfun(i): @@ -343,7 +341,7 @@ def test_only_one_instance(self): args = pool_._debug_no_launch pool_sz = len(pool_) assert len(args["ips"]) == 1 - assert len(args["port"]) == 1 + assert len(args["ports"]) == 1 def test_ip(self, monkeypatch): monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) From b10ff10a161e3ee23deb414b9d97ad9b6c7d13a0 Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:10:46 +0000 Subject: [PATCH 21/22] fix: test instance name --- tests/test_pool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 37d8199520..101e98aaea 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -288,9 +288,9 @@ def test_directory_names_custom_string(self, tmpdir): _debug_no_launch=True, ) dirs_path_pool = os.listdir(pool._root_dir) - - assert "my_instance_0" in dirs_path_pool - assert "my_instance_1" in dirs_path_pool + time.sleep(2) + assert len(pool) == 2 + assert all(["my_instance" in each for each in dirs_path_pool]) @skip_if_ignore_pool def test_directory_names_function(self, tmpdir): From 5e037e61b611c6e5cf36a576583e7b1701a40e2a Mon Sep 17 00:00:00 2001 From: root <28149841+germa89@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:25:08 +0000 Subject: [PATCH 22/22] fix: test instance name --- tests/test_pool.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 101e98aaea..4a17ed0fa0 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -289,7 +289,6 @@ def test_directory_names_custom_string(self, tmpdir): ) dirs_path_pool = os.listdir(pool._root_dir) time.sleep(2) - assert len(pool) == 2 assert all(["my_instance" in each for each in dirs_path_pool]) @skip_if_ignore_pool