Skip to content

Commit

Permalink
fix: not exiting MAPDL when faking exiting MAPDL in tests.
Browse files Browse the repository at this point in the history
feat: splitting test in two.
  • Loading branch information
germa89 committed Aug 26, 2024
1 parent 6289513 commit 201e911
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions tests/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@
pytestmark = requires("grpc")


class UnavailableError(grpc.RpcError):
def __init__(self, message="Service is temporarily unavailable."):
self._message = message
self._code = grpc.StatusCode.UNAVAILABLE
super().__init__(message)

def code(self):
return self._code

def details(self):
return self._message


def write_tmp_in_mapdl_instance(mapdl, filename, ext="txt"):
"""Write a temporary file from MAPDL."""
with mapdl.non_interactive:
Expand Down Expand Up @@ -628,38 +641,45 @@ def test_generic_grpc_exception(monkeypatch, grpc_channel):
mapdl = MapdlGrpc(channel=grpc_channel)
assert mapdl.is_alive

class UnavailableError(grpc.RpcError):
def __init__(self, message="Service is temporarily unavailable."):
self._message = message
self._code = grpc.StatusCode.UNAVAILABLE
super().__init__(message)

def code(self):
return self._code

def details(self):
return self._message

@protect_grpc
def _raise_error_code(*args, **kwargs):
raise UnavailableError()

# Monkey patch to raise the same issue.
monkeypatch.setattr(mapdl, "prep7", _raise_error_code)

with pytest.raises(
MapdlRuntimeError, match="MAPDL server connection terminated unexpectedly while"
):
mapdl.prep7(
mapdl
) # passing mapdl to simulate the function `_raise_error_code` to be a method.
# passing mapdl to simulate the function `_raise_error_code` to be a method.
mapdl.prep7(mapdl)

assert mapdl.is_alive


def test_generic_grpc_exception_exited(monkeypatch, grpc_channel):
mapdl = MapdlGrpc(channel=grpc_channel)
assert mapdl.is_alive

@protect_grpc
def _raise_error_code(*args, **kwargs):
raise UnavailableError()

def _null_close_process():
return None

# faking exiting MAPDL
mapdl._exited = True

# Monkey patch to raise the same issue.
monkeypatch.setattr(mapdl, "prep7", _raise_error_code)

# monkey patch `_close_process` so MAPDL does not exit when
monkeypatch.setattr(mapdl, "_close_process", _null_close_process)

with pytest.raises(
MapdlExitedError, match="MAPDL server connection terminated unexpectedly while"
):
mapdl.prep7(mapdl)

mapdl._exited = False
mapdl._exited = False # Restoring

0 comments on commit 201e911

Please sign in to comment.