diff --git a/python/mujoco_mpc/agent.py b/python/mujoco_mpc/agent.py index 1198fc685..9af752a64 100644 --- a/python/mujoco_mpc/agent.py +++ b/python/mujoco_mpc/agent.py @@ -59,7 +59,7 @@ def parse_port(server_addr: str) -> int: Returns: int: the port """ - match = re.search(r":(\d+)", server_addr) + match = re.search(r":([0-9]+)\Z", server_addr) if match is None: raise ValueError(f"Port not specified in server address: '{server_addr}'") return int(match.group(1)) diff --git a/python/mujoco_mpc/agent_test.py b/python/mujoco_mpc/agent_test.py index 68b40385a..1fb681695 100644 --- a/python/mujoco_mpc/agent_test.py +++ b/python/mujoco_mpc/agent_test.py @@ -43,6 +43,19 @@ def environment_reset(model, data): class AgentTest(parameterized.TestCase): + @parameterized.parameters( + ("localhost:12345", 12345), + ("[::1]:12345", 12345), + ("[2001:db8:12::1]:54321", 54321), + ) + def test_parse_port(self, address, expected_port): + self.assertEqual(agent_lib.parse_port(address), expected_port) + + @parameterized.parameters("[::1]", "localhost:123junk") + def test_parse_port_requires_terminal_port(self, address): + with self.assertRaisesRegex(ValueError, "Port not specified"): + agent_lib.parse_port(address) + def test_set_task_parameters(self): model_path = ( pathlib.Path(__file__).parent.parent.parent