Skip to content

Commit 0cfeba8

Browse files
committed
fix: update monitoring thread condition and test mocks
- Add _path check to monitoring thread start condition (requires both process and path) - Mock _check_process_is_alive in all tests to prevent Mock.poll() issues - All 8 tests now pass successfully (2 skipped on non-local)
1 parent 9bf6306 commit 0cfeba8

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def monitor_mapdl_alive():
651651

652652
# Start the monitoring thread
653653
monitor_thread = None
654-
if self._local and self._mapdl_process:
654+
if self._local and self._mapdl_process and self._path:
655655
monitor_thread = threading.Thread(target=monitor_mapdl_alive, daemon=True)
656656
monitor_thread.start()
657657
self._log.debug("Started MAPDL monitoring thread")

tests/test_launcher.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,20 +2505,22 @@ def test_multi_connect_monitoring_conditions(mapdl, has_process, has_path, shoul
25052505
mapdl._mapdl_process = Mock() if has_process else None
25062506
mapdl._path = "/some/path" if has_path else None
25072507

2508-
# Mock _connect to succeed immediately
2509-
with patch.object(mapdl, '_connect', return_value=True):
2510-
# Track if thread was started by checking log calls
2511-
with patch.object(mapdl._log, 'debug') as mock_debug:
2512-
mapdl._multi_connect(n_attempts=1, timeout=1)
2513-
2514-
# Check if monitoring thread debug message was logged
2515-
debug_calls = [str(call) for call in mock_debug.call_args_list]
2516-
thread_started = any("Started MAPDL monitoring thread" in str(call) for call in debug_calls)
2517-
2518-
if should_monitor:
2519-
assert thread_started, "Monitoring thread should have started"
2520-
else:
2521-
assert not thread_started, "Monitoring thread should not have started"
2508+
# Mock _check_process_is_alive to not raise exceptions during monitoring
2509+
with patch('ansys.mapdl.core.launcher._check_process_is_alive'):
2510+
# Mock _connect to succeed immediately
2511+
with patch.object(mapdl, '_connect', return_value=True):
2512+
# Track if thread was started by checking log calls
2513+
with patch.object(mapdl._log, 'debug') as mock_debug:
2514+
mapdl._multi_connect(n_attempts=1, timeout=1)
2515+
2516+
# Check if monitoring thread debug message was logged
2517+
debug_calls = [str(call) for call in mock_debug.call_args_list]
2518+
thread_started = any("Started MAPDL monitoring thread" in str(call) for call in debug_calls)
2519+
2520+
if should_monitor:
2521+
assert thread_started, "Monitoring thread should have started"
2522+
else:
2523+
assert not thread_started, "Monitoring thread should not have started"
25222524

25232525
finally:
25242526
# Restore original state
@@ -2541,24 +2543,26 @@ def test_multi_connect_monitoring_thread_cleanup(mapdl):
25412543
mapdl._mapdl_process = Mock(pid=12345)
25422544
mapdl._path = "/some/path"
25432545

2544-
# Mock psutil to say process exists
2545-
with patch('psutil.pid_exists', return_value=True):
2546-
# Mock _connect to succeed quickly
2547-
with patch.object(mapdl, '_connect', return_value=True):
2548-
# Track active threads before
2549-
threads_before = threading.active_count()
2550-
2551-
# Call _multi_connect
2552-
mapdl._multi_connect(n_attempts=1, timeout=2)
2553-
2554-
# Give a moment for thread cleanup
2555-
sleep(0.2)
2556-
2557-
# Thread count should be back to normal (or close)
2558-
threads_after = threading.active_count()
2559-
# Allow for some variance in thread count
2560-
assert abs(threads_after - threads_before) <= 1, \
2561-
"Monitoring thread should be cleaned up"
2546+
# Mock _check_process_is_alive to not raise exceptions
2547+
with patch('ansys.mapdl.core.launcher._check_process_is_alive'):
2548+
# Mock psutil to say process exists
2549+
with patch('psutil.pid_exists', return_value=True):
2550+
# Mock _connect to succeed quickly
2551+
with patch.object(mapdl, '_connect', return_value=True):
2552+
# Track active threads before
2553+
threads_before = threading.active_count()
2554+
2555+
# Call _multi_connect
2556+
mapdl._multi_connect(n_attempts=1, timeout=2)
2557+
2558+
# Give a moment for thread cleanup
2559+
sleep(0.2)
2560+
2561+
# Thread count should be back to normal (or close)
2562+
threads_after = threading.active_count()
2563+
# Allow for some variance in thread count
2564+
assert abs(threads_after - threads_before) <= 1, \
2565+
"Monitoring thread should be cleaned up"
25622566

25632567
finally:
25642568
mapdl._local = original_local

0 commit comments

Comments
 (0)