Skip to content

Commit 521ee2f

Browse files
feat: avoiding reconnecting if MAPDL exited already (#3708)
* test: refactor check_stds and post_mortem_checks * test: backing off algorithm * chore: adding changelog file 3703.added.md [dependabot-skip] * fix: codacity warnings * feat: using get_value to obtain the n elements * revert: revert "feat: using get_value to obtain the n elements" Performance is not as go This reverts commit 877f803. * feat: using get_value to obtain the n elements * revert: revert "feat: using get_value to obtain the n elements" Performance is not as go This reverts commit 877f803. * feat: using mapdl.exit when raising final error. * test: fix test by avoiding killing mapdl. * fix: test * fix: test * feat: adding warnings when restarting MAPDL during testing * fix: test * feat: caching requires_package * test: adding more tests * chore: adding changelog file 3705.added.md [dependabot-skip] * chore: adding changelog file 3705.added.md [dependabot-skip] * fix: warnings import * feat: not reconnecting if MAPDL already exited * test: adding tests * chore: adding changelog file 3708.miscellaneous.md [dependabot-skip] * fix: tests --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 9d63421 commit 521ee2f

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

doc/changelog.d/3708.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: avoiding reconnecting if MAPDL exited already

src/ansys/mapdl/core/errors.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,29 @@ def wrapper(*args, **kwargs):
327327

328328
except grpc.RpcError as error:
329329
mapdl = retrieve_mapdl_from_args(args)
330+
330331
mapdl._log.debug("A gRPC error has been detected.")
331332

332-
i_attemps += 1
333-
if i_attemps <= n_attempts:
333+
if not mapdl.exited:
334+
i_attemps += 1
335+
if i_attemps <= n_attempts:
334336

335-
wait = (
336-
initial_backoff * multiplier_backoff**i_attemps
337-
) # Exponential backoff
337+
wait = (
338+
initial_backoff * multiplier_backoff**i_attemps
339+
) # Exponential backoff
338340

339-
# reconnect
340-
mapdl._log.debug(
341-
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
342-
)
341+
# reconnect
342+
mapdl._log.debug(
343+
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
344+
)
343345

344-
if not mapdl.is_alive:
345-
connected = mapdl._connect(timeout=wait)
346-
else:
347-
sleep(wait)
346+
if not mapdl.is_alive:
347+
connected = mapdl._connect(timeout=wait)
348+
else:
349+
sleep(wait)
348350

349-
# Retry again
350-
continue
351+
# Retry again
352+
continue
351353

352354
# Custom errors
353355
reason = ""

tests/test_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_launch_mapdl_cli(monkeypatch, run_cli, start_instance):
112112
assert pid != 0
113113

114114

115+
@requires("click")
115116
@pytest.mark.parametrize(
116117
"mapping",
117118
((False, True), (False, True, False), (False, False, False), (True, True, False)),

tests/test_mapdl.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,7 +2749,8 @@ def test_comment_on_debug_mode(mapdl, cleared):
27492749

27502750
@patch("ansys.mapdl.core.errors.N_ATTEMPTS", 2)
27512751
@patch("ansys.mapdl.core.errors.MULTIPLIER_BACKOFF", 1)
2752-
def test_timeout_when_exiting(mapdl):
2752+
@pytest.mark.parametrize("is_exited", [True, False])
2753+
def test_timeout_when_exiting(mapdl, is_exited):
27532754
from ansys.mapdl.core import errors
27542755

27552756
def raise_exception(*args, **kwargs):
@@ -2759,9 +2760,13 @@ def raise_exception(*args, **kwargs):
27592760
e.code = lambda: grpc.StatusCode.ABORTED
27602761
e.details = lambda: "My gRPC error details"
27612762

2763+
# Simulating MAPDL exiting by force
2764+
mapdl._exited = is_exited
2765+
27622766
raise e
27632767

27642768
handle_generic_grpc_error = errors.handle_generic_grpc_error
2769+
27652770
with (
27662771
patch("ansys.mapdl.core.mapdl_grpc.pb_types.CmdRequest") as mock_cmdrequest,
27672772
patch(
@@ -2787,9 +2792,17 @@ def raise_exception(*args, **kwargs):
27872792
assert mapdl._exited
27882793

27892794
assert mock_handle.call_count == 1
2790-
assert mock_connect.call_count == errors.N_ATTEMPTS
2791-
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
2792-
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1
2795+
2796+
if is_exited:
2797+
# Checking no trying to reconnect
2798+
assert mock_connect.call_count == 0
2799+
assert mock_cmdrequest.call_count == 1
2800+
assert mock_is_alive.call_count == 1
2801+
2802+
else:
2803+
assert mock_connect.call_count == errors.N_ATTEMPTS
2804+
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
2805+
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1
27932806

27942807
mapdl._exited = False
27952808

@@ -2838,6 +2851,7 @@ def test_none_on_selecting(mapdl, cleared, func):
28382851
assert len(selfunc(None)) == 0
28392852

28402853

2854+
@requires("pyvista")
28412855
def test_requires_package_speed():
28422856
from ansys.mapdl.core.misc import requires_package
28432857

0 commit comments

Comments
 (0)