|
24 | 24 | import os |
25 | 25 | import re |
26 | 26 | import shutil |
| 27 | +import subprocess |
27 | 28 | import sys |
28 | 29 | from unittest.mock import patch |
29 | 30 |
|
|
34 | 35 | from ansys.mapdl.core.common_grpc import DEFAULT_CHUNKSIZE |
35 | 36 | from ansys.mapdl.core.errors import ( |
36 | 37 | MapdlCommandIgnoredError, |
| 38 | + MapdlConnectionError, |
| 39 | + MapdlDidNotStart, |
37 | 40 | MapdlExitedError, |
38 | 41 | MapdlgRPCError, |
39 | 42 | MapdlRuntimeError, |
@@ -577,19 +580,101 @@ def test_input_compatibility_api_change(mapdl, cleared): |
577 | 580 |
|
578 | 581 |
|
579 | 582 | @requires("grpc") |
580 | | -@requires("local") |
581 | | -@requires("nostudent") |
582 | | -def test__check_stds(): |
| 583 | +def test__check_stds(mapdl): |
583 | 584 | """Test that the standard input is checked.""" |
584 | | - from ansys.mapdl.core import launch_mapdl |
| 585 | + from ansys.mapdl.core.launcher import _check_server_is_alive, _get_std_output |
| 586 | + |
| 587 | + cmd = "counter=1; while true; do echo $counter; ((counter++)); sleep 1; done" |
| 588 | + |
| 589 | + process = subprocess.Popen( |
| 590 | + ["bash", "-c", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 591 | + ) # nosec B603 B607 |
| 592 | + |
| 593 | + if not hasattr(mapdl, "_stdout_queue"): |
| 594 | + mapdl._stdout_queue = None |
| 595 | + if not hasattr(mapdl, "_stdout_thread"): |
| 596 | + mapdl._stdout_thread = None |
| 597 | + |
| 598 | + with ( |
| 599 | + # To avoid overwriting the values for the rest of the tests. |
| 600 | + patch.object(mapdl, "_stdout_queue"), |
| 601 | + patch.object(mapdl, "_stdout_thread"), |
| 602 | + patch.object(mapdl, "_mapdl_process"), |
| 603 | + patch( |
| 604 | + "ansys.mapdl.core.launcher._get_std_output", autospec=True |
| 605 | + ) as mock_get_std_output, |
| 606 | + ): |
| 607 | + |
| 608 | + mock_get_std_output.side_effect = _get_std_output |
| 609 | + |
| 610 | + mapdl._mapdl_process = process |
| 611 | + mapdl._create_process_stds_queue(process) |
| 612 | + |
| 613 | + # this should raise no issues |
| 614 | + mapdl._post_mortem_checks(process) |
585 | 615 |
|
586 | | - mapdl = launch_mapdl(port=50058) |
| 616 | + with pytest.raises(MapdlDidNotStart): |
| 617 | + _check_server_is_alive(mapdl._stdout_queue, 0.5) |
587 | 618 |
|
588 | | - mapdl._read_stds() |
589 | | - assert mapdl._stdout is not None |
590 | | - assert mapdl._stderr is not None |
| 619 | + assert isinstance(mapdl._stdout, list) |
| 620 | + assert isinstance(mapdl._stderr, list) |
591 | 621 |
|
592 | | - mapdl.exit(force=True) |
| 622 | + assert ( |
| 623 | + mapdl._stdout |
| 624 | + and len(mapdl._stdout) > 0 |
| 625 | + and mapdl._stdout[-1] |
| 626 | + and mapdl._stdout[-1].strip().isdigit() |
| 627 | + ) |
| 628 | + |
| 629 | + mock_get_std_output.assert_called() |
| 630 | + |
| 631 | + |
| 632 | +@requires("grpc") |
| 633 | +@requires("nowindows") # since we are using bash |
| 634 | +def test__post_mortem_checks(mapdl): |
| 635 | + """Test that the standard input is checked.""" |
| 636 | + from ansys.mapdl.core.launcher import _get_std_output |
| 637 | + |
| 638 | + bash_command = """ |
| 639 | +counter=1; while true; do |
| 640 | + echo $counter; |
| 641 | + ((counter++)); |
| 642 | + sleep 0.1; |
| 643 | + if [ $counter -eq 7 ]; then |
| 644 | + echo ""; |
| 645 | + echo "ERROR: Expected MapdlConnection error"; |
| 646 | + echo ""; |
| 647 | + fi; |
| 648 | +done |
| 649 | +""" |
| 650 | + process = subprocess.Popen( |
| 651 | + ["bash", "-c", bash_command], stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 652 | + ) |
| 653 | + |
| 654 | + if not hasattr(mapdl, "_stdout_queue"): |
| 655 | + mapdl._stdout_queue = None |
| 656 | + if not hasattr(mapdl, "_stdout_thread"): |
| 657 | + mapdl._stdout_thread = None |
| 658 | + |
| 659 | + with ( |
| 660 | + # To avoid overwriting the values for the rest of the tests. |
| 661 | + patch.object(mapdl, "_stdout_queue"), |
| 662 | + patch.object(mapdl, "_stdout_thread"), |
| 663 | + patch.object(mapdl, "_mapdl_process"), |
| 664 | + patch( |
| 665 | + "ansys.mapdl.core.launcher._get_std_output", autospec=True |
| 666 | + ) as mock_get_std_output, |
| 667 | + ): |
| 668 | + |
| 669 | + mock_get_std_output.side_effect = _get_std_output |
| 670 | + |
| 671 | + mapdl._mapdl_process = process |
| 672 | + mapdl._create_process_stds_queue(process) |
| 673 | + |
| 674 | + with pytest.raises( |
| 675 | + MapdlConnectionError, match="Expected MapdlConnection error" |
| 676 | + ): |
| 677 | + mapdl._post_mortem_checks(process) |
593 | 678 |
|
594 | 679 |
|
595 | 680 | def test_subscribe_to_channel(mapdl, cleared): |
|
0 commit comments