|
1 | 1 | import pytest |
2 | 2 | import os |
| 3 | +import sys |
3 | 4 | import numpy as np |
4 | 5 |
|
5 | 6 |
|
@@ -450,3 +451,129 @@ def test_write_timestamp_matches_cpp_semantics(self, temp_dir): |
450 | 451 | "After 3 writes with delta=1 simtime must remain 0 " |
451 | 452 | "(matching C++/MATLAB/Verilog); got %s" % concore.simtime |
452 | 453 | ) |
| 454 | + |
| 455 | + |
| 456 | +class TestPidRegistry: |
| 457 | + """Tests for the Windows PID registry mechanism (Issue #391).""" |
| 458 | + |
| 459 | + @pytest.fixture(autouse=True) |
| 460 | + def use_temp_dir(self, temp_dir, monkeypatch): |
| 461 | + self.temp_dir = temp_dir |
| 462 | + monkeypatch.chdir(temp_dir) |
| 463 | + |
| 464 | + def test_register_pid_creates_registry_file(self): |
| 465 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 466 | + |
| 467 | + _register_pid() |
| 468 | + assert os.path.exists(_PID_REGISTRY_FILE) |
| 469 | + with open(_PID_REGISTRY_FILE) as f: |
| 470 | + pids = [line.strip() for line in f if line.strip()] |
| 471 | + assert str(os.getpid()) in pids |
| 472 | + |
| 473 | + def test_register_pid_appends_not_overwrites(self): |
| 474 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 475 | + |
| 476 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 477 | + f.write("11111\n") |
| 478 | + f.write("22222\n") |
| 479 | + _register_pid() |
| 480 | + with open(_PID_REGISTRY_FILE) as f: |
| 481 | + pids = [line.strip() for line in f if line.strip()] |
| 482 | + assert "11111" in pids |
| 483 | + assert "22222" in pids |
| 484 | + assert str(os.getpid()) in pids |
| 485 | + assert len(pids) == 3 |
| 486 | + |
| 487 | + def test_cleanup_pid_removes_current_pid(self): |
| 488 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 489 | + |
| 490 | + current_pid = str(os.getpid()) |
| 491 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 492 | + f.write("99999\n") |
| 493 | + f.write(current_pid + "\n") |
| 494 | + f.write("88888\n") |
| 495 | + _cleanup_pid() |
| 496 | + with open(_PID_REGISTRY_FILE) as f: |
| 497 | + pids = [line.strip() for line in f if line.strip()] |
| 498 | + assert current_pid not in pids |
| 499 | + assert "99999" in pids |
| 500 | + assert "88888" in pids |
| 501 | + |
| 502 | + def test_cleanup_pid_deletes_files_when_last_pid(self): |
| 503 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE, _KILL_SCRIPT_FILE |
| 504 | + |
| 505 | + current_pid = str(os.getpid()) |
| 506 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 507 | + f.write(current_pid + "\n") |
| 508 | + with open(_KILL_SCRIPT_FILE, "w") as f: |
| 509 | + f.write("@echo off\n") |
| 510 | + _cleanup_pid() |
| 511 | + assert not os.path.exists(_PID_REGISTRY_FILE) |
| 512 | + assert not os.path.exists(_KILL_SCRIPT_FILE) |
| 513 | + |
| 514 | + def test_cleanup_pid_handles_missing_registry(self): |
| 515 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 516 | + |
| 517 | + assert not os.path.exists(_PID_REGISTRY_FILE) |
| 518 | + _cleanup_pid() # Should not raise |
| 519 | + |
| 520 | + def test_write_kill_script_generates_bat_file(self): |
| 521 | + from concore import _write_kill_script, _KILL_SCRIPT_FILE, _PID_REGISTRY_FILE |
| 522 | + |
| 523 | + _write_kill_script() |
| 524 | + assert os.path.exists(_KILL_SCRIPT_FILE) |
| 525 | + with open(_KILL_SCRIPT_FILE) as f: |
| 526 | + content = f.read() |
| 527 | + assert _PID_REGISTRY_FILE in content |
| 528 | + assert "tasklist" in content |
| 529 | + assert "taskkill" in content |
| 530 | + assert "python" in content.lower() |
| 531 | + |
| 532 | + def test_multi_node_registration(self): |
| 533 | + from concore import _register_pid, _PID_REGISTRY_FILE |
| 534 | + |
| 535 | + fake_pids = ["1204", "1932", "8120"] |
| 536 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 537 | + for pid in fake_pids: |
| 538 | + f.write(pid + "\n") |
| 539 | + _register_pid() |
| 540 | + with open(_PID_REGISTRY_FILE) as f: |
| 541 | + pids = [line.strip() for line in f if line.strip()] |
| 542 | + for pid in fake_pids: |
| 543 | + assert pid in pids |
| 544 | + assert str(os.getpid()) in pids |
| 545 | + assert len(pids) == 4 |
| 546 | + |
| 547 | + def test_cleanup_preserves_other_pids(self): |
| 548 | + from concore import _cleanup_pid, _PID_REGISTRY_FILE |
| 549 | + |
| 550 | + current_pid = str(os.getpid()) |
| 551 | + other_pids = ["1111", "2222", "3333"] |
| 552 | + with open(_PID_REGISTRY_FILE, "w") as f: |
| 553 | + for pid in other_pids: |
| 554 | + f.write(pid + "\n") |
| 555 | + f.write(current_pid + "\n") |
| 556 | + _cleanup_pid() |
| 557 | + with open(_PID_REGISTRY_FILE) as f: |
| 558 | + pids = [line.strip() for line in f if line.strip()] |
| 559 | + assert len(pids) == 3 |
| 560 | + assert current_pid not in pids |
| 561 | + for pid in other_pids: |
| 562 | + assert pid in pids |
| 563 | + |
| 564 | + @pytest.mark.skipif( |
| 565 | + not hasattr(sys, "getwindowsversion"), reason="Windows-only test" |
| 566 | + ) |
| 567 | + def test_import_registers_pid_on_windows(self): |
| 568 | + """Verify module-level PID registration on Windows.""" |
| 569 | + import importlib |
| 570 | + |
| 571 | + for mod_name in ("concore", "concore_base"): |
| 572 | + sys.modules.pop(mod_name, None) |
| 573 | + import concore |
| 574 | + |
| 575 | + assert os.path.exists(concore._PID_REGISTRY_FILE) |
| 576 | + with open(concore._PID_REGISTRY_FILE) as f: |
| 577 | + pids = [line.strip() for line in f if line.strip()] |
| 578 | + assert str(os.getpid()) in pids |
| 579 | + importlib.reload(concore) |
0 commit comments