|
1 | 1 | import pytest |
2 | 2 | import os |
| 3 | +import sys |
3 | 4 | import numpy as np |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
@@ -579,3 +580,142 @@ def send_json_with_retry(self, message): |
579 | 580 | concore.simtime = orig_simtime |
580 | 581 | elif hasattr(concore, "simtime"): |
581 | 582 | delattr(concore, "simtime") |
| 583 | + |
| 584 | + |
| 585 | +class TestPidRegistry: |
| 586 | + """Tests for the Windows PID registry mechanism (Issue #391).""" |
| 587 | + |
| 588 | + @pytest.fixture(autouse=True) |
| 589 | + def use_temp_dir(self, temp_dir, monkeypatch): |
| 590 | + self.temp_dir = temp_dir |
| 591 | + monkeypatch.chdir(temp_dir) |
| 592 | + import concore |
| 593 | + |
| 594 | + monkeypatch.setattr(concore, "_BASE_DIR", temp_dir) |
| 595 | + monkeypatch.setattr( |
| 596 | + concore, |
| 597 | + "_PID_REGISTRY_FILE", |
| 598 | + os.path.join(temp_dir, "concorekill_pids.txt"), |
| 599 | + ) |
| 600 | + monkeypatch.setattr( |
| 601 | + concore, |
| 602 | + "_KILL_SCRIPT_FILE", |
| 603 | + os.path.join(temp_dir, "concorekill.bat"), |
| 604 | + ) |
| 605 | + |
| 606 | + def test_register_pid_creates_registry_file(self): |
| 607 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 608 | + |
| 609 | + _register_pid() |
| 610 | + assert os.path.exists(_PID_REGISTRY_FILE) |
| 611 | + with open(_PID_REGISTRY_FILE) as f: |
| 612 | + pids = [line.strip() for line in f if line.strip()] |
| 613 | + assert str(os.getpid()) in pids |
| 614 | + |
| 615 | + def test_register_pid_appends_not_overwrites(self): |
| 616 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 617 | + |
| 618 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 619 | + f.write("11111\n") |
| 620 | + f.write("22222\n") |
| 621 | + _register_pid() |
| 622 | + with open(_PID_REGISTRY_FILE) as f: |
| 623 | + pids = [line.strip() for line in f if line.strip()] |
| 624 | + assert "11111" in pids |
| 625 | + assert "22222" in pids |
| 626 | + assert str(os.getpid()) in pids |
| 627 | + assert len(pids) == 3 |
| 628 | + |
| 629 | + def test_cleanup_pid_removes_current_pid(self): |
| 630 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 631 | + |
| 632 | + current_pid = str(os.getpid()) |
| 633 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 634 | + f.write("99999\n") |
| 635 | + f.write(current_pid + "\n") |
| 636 | + f.write("88888\n") |
| 637 | + _cleanup_pid() |
| 638 | + with open(_PID_REGISTRY_FILE) as f: |
| 639 | + pids = [line.strip() for line in f if line.strip()] |
| 640 | + assert current_pid not in pids |
| 641 | + assert "99999" in pids |
| 642 | + assert "88888" in pids |
| 643 | + |
| 644 | + def test_cleanup_pid_deletes_files_when_last_pid(self): |
| 645 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE, _KILL_SCRIPT_FILE |
| 646 | + |
| 647 | + current_pid = str(os.getpid()) |
| 648 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 649 | + f.write(current_pid + "\n") |
| 650 | + with open(_KILL_SCRIPT_FILE, "w") as f: |
| 651 | + f.write("@echo off\n") |
| 652 | + _cleanup_pid() |
| 653 | + assert not os.path.exists(_PID_REGISTRY_FILE) |
| 654 | + assert not os.path.exists(_KILL_SCRIPT_FILE) |
| 655 | + |
| 656 | + def test_cleanup_pid_handles_missing_registry(self): |
| 657 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 658 | + |
| 659 | + assert not os.path.exists(_PID_REGISTRY_FILE) |
| 660 | + _cleanup_pid() # Should not raise |
| 661 | + |
| 662 | + def test_write_kill_script_generates_bat_file(self): |
| 663 | + from concore import _write_kill_script, _KILL_SCRIPT_FILE, _PID_REGISTRY_FILE |
| 664 | + |
| 665 | + _write_kill_script() |
| 666 | + assert os.path.exists(_KILL_SCRIPT_FILE) |
| 667 | + with open(_KILL_SCRIPT_FILE) as f: |
| 668 | + content = f.read() |
| 669 | + assert os.path.basename(_PID_REGISTRY_FILE) in content |
| 670 | + assert "wmic" in content |
| 671 | + assert "taskkill" in content |
| 672 | + assert "concore" in content.lower() |
| 673 | + |
| 674 | + def test_multi_node_registration(self): |
| 675 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 676 | + |
| 677 | + fake_pids = ["1204", "1932", "8120"] |
| 678 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 679 | + for pid in fake_pids: |
| 680 | + f.write(pid + "\n") |
| 681 | + _register_pid() |
| 682 | + with open(_PID_REGISTRY_FILE) as f: |
| 683 | + pids = [line.strip() for line in f if line.strip()] |
| 684 | + for pid in fake_pids: |
| 685 | + assert pid in pids |
| 686 | + assert str(os.getpid()) in pids |
| 687 | + assert len(pids) == 4 |
| 688 | + |
| 689 | + def test_cleanup_preserves_other_pids(self): |
| 690 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 691 | + |
| 692 | + current_pid = str(os.getpid()) |
| 693 | + other_pids = ["1111", "2222", "3333"] |
| 694 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 695 | + for pid in other_pids: |
| 696 | + f.write(pid + "\n") |
| 697 | + f.write(current_pid + "\n") |
| 698 | + _cleanup_pid() |
| 699 | + with open(_PID_REGISTRY_FILE) as f: |
| 700 | + pids = [line.strip() for line in f if line.strip()] |
| 701 | + assert len(pids) == 3 |
| 702 | + assert current_pid not in pids |
| 703 | + for pid in other_pids: |
| 704 | + assert pid in pids |
| 705 | + |
| 706 | + @pytest.mark.skipif( |
| 707 | + not hasattr(sys, "getwindowsversion"), reason="Windows-only test" |
| 708 | + ) |
| 709 | + def test_import_registers_pid_on_windows(self): |
| 710 | + """Verify module-level PID registration on Windows.""" |
| 711 | + import importlib |
| 712 | + |
| 713 | + for mod_name in ("concore", "concore_base"): |
| 714 | + sys.modules.pop(mod_name, None) |
| 715 | + import concore |
| 716 | + |
| 717 | + assert os.path.exists(concore._PID_REGISTRY_FILE) |
| 718 | + with open(concore._PID_REGISTRY_FILE) as f: |
| 719 | + pids = [line.strip() for line in f if line.strip()] |
| 720 | + assert str(os.getpid()) in pids |
| 721 | + importlib.reload(concore) |
0 commit comments