fix: 安全关闭 MuJoCo 显示窗口

This commit is contained in:
2026-08-04 13:46:31 +08:00
parent 002484b610
commit 0dfe3d77dc
2 changed files with 36 additions and 0 deletions
@@ -1,9 +1,12 @@
import math
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import yaml
from xr_rm_mujoco import dual_arm_simulator as simulator_module
from xr_rm_mujoco.dual_arm_simulator import (
ARM_JOINT_NAMES,
DualArmKinematicModel,
@@ -70,6 +73,34 @@ def test_mujoco_config_contains_only_render_parameters() -> None:
assert parameters == {"render_rate_hz": 60.0}
def test_main_closes_viewer_cleanly_on_keyboard_interrupt(monkeypatch) -> None:
close_viewer = simulator_module.DualArmSimulator.close_viewer
viewer = Mock()
node = SimpleNamespace(_viewer=viewer, destroy_node=Mock())
node.close_viewer = lambda: close_viewer(node)
init = Mock()
spin = Mock(side_effect=KeyboardInterrupt)
sleep = Mock()
shutdown = Mock()
monkeypatch.setattr(simulator_module, "DualArmSimulator", lambda: node)
monkeypatch.setattr(simulator_module.rclpy, "init", init)
monkeypatch.setattr(simulator_module.rclpy, "spin", spin)
monkeypatch.setattr(simulator_module.rclpy, "ok", lambda: True)
monkeypatch.setattr(simulator_module.time, "sleep", sleep)
monkeypatch.setattr(simulator_module.rclpy, "shutdown", shutdown)
simulator_module.main(["--test"])
init.assert_called_once_with(args=["--test"])
spin.assert_called_once_with(node)
viewer.close.assert_called_once_with()
sleep.assert_called_once_with(0.1)
node.destroy_node.assert_called_once_with()
shutdown.assert_called_once_with()
assert node._viewer is None
@pytest.mark.parametrize(
("names", "positions", "match"),
[