Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: check no instances are left between tests #3257

Closed
1 change: 1 addition & 0 deletions doc/changelog.d/3257.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test: check no instances are left between tests
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from sys import platform

from _pytest.terminal import TerminalReporter # for terminal customization
import psutil
import pytest

from common import (
Expand Down Expand Up @@ -69,6 +70,7 @@
IS_SMP = is_smp()

QUICK_LAUNCH_SWITCHES = "-smp -m 100 -db 100"
VALID_PORTS = []

## Skip ifs
skip_on_windows = pytest.mark.skipif(ON_WINDOWS, reason="Skip on Windows")
Expand Down Expand Up @@ -455,6 +457,41 @@ def run_before_and_after_tests_2(request, mapdl):
assert prev == mapdl.is_local


@pytest.fixture(autouse=True, scope="function")
def run_before_and_after_tests_2(request, mapdl):
"""Make sure we leave no MAPDL running behind"""
from ansys.mapdl.core.cli.stop import is_ansys_process

PROCESS_OK_STATUS = [
psutil.STATUS_RUNNING, #
psutil.STATUS_SLEEPING, #
psutil.STATUS_DISK_SLEEP,
psutil.STATUS_DEAD,
psutil.STATUS_PARKED, # (Linux)
psutil.STATUS_IDLE, # (Linux, macOS, FreeBSD)
]

yield

if ON_LOCAL:
for proc in psutil.process_iter():
if (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
):
# Killing by ports
connections = proc.connections()
to_kill = True

for each_connection in connections:
if each_connection.local_address[1] in VALID_PORTS:
to_kill = False

if to_kill:
raise Exception("MAPDL instances are alive after test")


@pytest.fixture(scope="session")
def mapdl_console(request):
if os.name != "posix":
Expand Down Expand Up @@ -512,6 +549,8 @@ def mapdl(request, tmpdir_factory):
mapdl._show_matplotlib_figures = False # CI: don't show matplotlib figures
MAPDL_VERSION = mapdl.version # Caching version

VALID_PORTS.append(mapdl.port)

if ON_CI:
mapdl._local = ON_LOCAL # CI: override for testing

Expand All @@ -521,6 +560,7 @@ def mapdl(request, tmpdir_factory):
# using yield rather than return here to be able to test exit
yield mapdl

VALID_PORTS.remove(mapdl.port)
###########################################################################
# test exit: only when allowed to start PYMAPDL
###########################################################################
Expand Down
10 changes: 3 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,9 @@ def test_launch_mapdl_cli(monkeypatch, run_cli, start_instance):
pid = int(re.search(r"\(PID=(\d+)\)", output).groups()[0])

output = run_cli(f"stop --pid {pid}")

try:
p = psutil.Process(pid)
assert not p.status()
except:
# An exception means the process is dead?
pass
p = psutil.Process(pid)
time.time(1)
assert not p.status()


@requires("click")
Expand Down
17 changes: 9 additions & 8 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import numpy as np
import pytest

from conftest import ON_LOCAL, ON_STUDENT, START_INSTANCE, has_dependency
from conftest import ON_LOCAL, ON_STUDENT, has_dependency

if has_dependency("ansys-tools-path"):
from ansys.tools.path import find_ansys
Expand All @@ -41,7 +41,7 @@
from ansys.mapdl.core import Mapdl, MapdlPool, examples
from ansys.mapdl.core.errors import VersionError
from ansys.mapdl.core.launcher import LOCALHOST, MAPDL_DEFAULT_PORT
from conftest import QUICK_LAUNCH_SWITCHES, NullContext, requires
from conftest import QUICK_LAUNCH_SWITCHES, VALID_PORTS, NullContext, requires

# skip entire module unless HAS_GRPC
pytestmark = requires("grpc")
Expand Down Expand Up @@ -96,11 +96,16 @@ def pool(tmpdir_factory):
wait=True,
)

VALID_PORTS.extend(mapdl_pool._ports)

yield mapdl_pool

for each in mapdl_pool._ports:
VALID_PORTS.remove(each)

##########################################################################
# test exit
mapdl_pool.exit()
mapdl_pool.exit(block=True)

timeout = time.time() + TWAIT

Expand Down Expand Up @@ -197,15 +202,13 @@ def func(mapdl):
assert len(pool) == pool_sz


# fails intermittently
@skip_if_ignore_pool
def test_batch(pool):
input_files = [examples.vmfiles["vm%d" % i] for i in range(1, len(pool) + 3)]
outputs = pool.run_batch(input_files)
assert len(outputs) == len(input_files)


# fails intermittently
@skip_if_ignore_pool
def test_map(pool):
completed_indices = []
Expand All @@ -225,9 +228,7 @@ def func(mapdl, input_file, index):


@skip_if_ignore_pool
@pytest.mark.skipif(
not START_INSTANCE, reason="This test requires the pool to be local"
)
@requires("local")
def test_abort(pool, tmpdir):
pool_sz = len(pool) # initial pool size

Expand Down
Loading