|
| 1 | +# Copyright 2025 Dimensional Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import signal |
| 17 | +import subprocess |
| 18 | +import time |
| 19 | + |
| 20 | +import lcm |
| 21 | +import pytest |
| 22 | + |
| 23 | +from dimos.core.transport import pLCMTransport |
| 24 | +from dimos.protocol.service.lcmservice import LCMService |
| 25 | + |
| 26 | + |
| 27 | +class LCMSpy(LCMService): |
| 28 | + messages: dict[str, list[bytes]] = {} |
| 29 | + |
| 30 | + def __init__(self, **kwargs) -> None: |
| 31 | + super().__init__(**kwargs) |
| 32 | + self.l = lcm.LCM() |
| 33 | + |
| 34 | + def start(self) -> None: |
| 35 | + super().start() |
| 36 | + if self.l: |
| 37 | + self.l.subscribe(".*", self.msg) |
| 38 | + |
| 39 | + def wait_for_topic(self, topic: str, timeout: float = 30.0) -> list[bytes]: |
| 40 | + start_time = time.time() |
| 41 | + while time.time() - start_time < timeout: |
| 42 | + if topic in self.messages: |
| 43 | + return self.messages[topic] |
| 44 | + time.sleep(0.1) |
| 45 | + raise TimeoutError(f"Timeout waiting for topic {topic}") |
| 46 | + |
| 47 | + def wait_for_message_content( |
| 48 | + self, topic: str, content_contains: bytes, timeout: float = 30.0 |
| 49 | + ) -> None: |
| 50 | + start_time = time.time() |
| 51 | + while time.time() - start_time < timeout: |
| 52 | + if topic in self.messages: |
| 53 | + for msg in self.messages[topic]: |
| 54 | + if content_contains in msg: |
| 55 | + return |
| 56 | + time.sleep(0.1) |
| 57 | + raise TimeoutError(f"Timeout waiting for message content on topic {topic}") |
| 58 | + |
| 59 | + def stop(self) -> None: |
| 60 | + super().stop() |
| 61 | + |
| 62 | + def msg(self, topic, data) -> None: |
| 63 | + self.messages.setdefault(topic, []).append(data) |
| 64 | + |
| 65 | + |
| 66 | +class DimosRobotCall: |
| 67 | + process: subprocess.Popen | None |
| 68 | + |
| 69 | + def __init__(self) -> None: |
| 70 | + self.process = None |
| 71 | + |
| 72 | + def start(self): |
| 73 | + self.process = subprocess.Popen( |
| 74 | + ["dimos-robot", "run", "demo-skill"], |
| 75 | + stdout=subprocess.PIPE, |
| 76 | + stderr=subprocess.PIPE, |
| 77 | + ) |
| 78 | + |
| 79 | + def stop(self) -> None: |
| 80 | + if self.process is None: |
| 81 | + return |
| 82 | + |
| 83 | + try: |
| 84 | + # Send the kill signal (SIGTERM for graceful shutdown) |
| 85 | + self.process.send_signal(signal.SIGTERM) |
| 86 | + |
| 87 | + # Record the time when we sent the kill signal |
| 88 | + shutdown_start = time.time() |
| 89 | + |
| 90 | + # Wait for the process to terminate with a 30-second timeout |
| 91 | + try: |
| 92 | + self.process.wait(timeout=30) |
| 93 | + shutdown_duration = time.time() - shutdown_start |
| 94 | + |
| 95 | + # Verify it shut down in time |
| 96 | + assert shutdown_duration <= 30, ( |
| 97 | + f"Process took {shutdown_duration:.2f} seconds to shut down, " |
| 98 | + f"which exceeds the 30-second limit" |
| 99 | + ) |
| 100 | + except subprocess.TimeoutExpired: |
| 101 | + # If we reach here, the process didn't terminate in 30 seconds |
| 102 | + self.process.kill() # Force kill |
| 103 | + self.process.wait() # Clean up |
| 104 | + raise AssertionError( |
| 105 | + "Process did not shut down within 30 seconds after receiving SIGTERM" |
| 106 | + ) |
| 107 | + |
| 108 | + except Exception: |
| 109 | + # Clean up if something goes wrong |
| 110 | + if self.process.poll() is None: # Process still running |
| 111 | + self.process.kill() |
| 112 | + self.process.wait() |
| 113 | + raise |
| 114 | + |
| 115 | + |
| 116 | +@pytest.fixture |
| 117 | +def lcm_spy(): |
| 118 | + lcm_spy = LCMSpy() |
| 119 | + lcm_spy.start() |
| 120 | + yield lcm_spy |
| 121 | + lcm_spy.stop() |
| 122 | + |
| 123 | + |
| 124 | +@pytest.fixture |
| 125 | +def dimos_robot_call(): |
| 126 | + dimos_robot_call = DimosRobotCall() |
| 127 | + dimos_robot_call.start() |
| 128 | + yield dimos_robot_call |
| 129 | + dimos_robot_call.stop() |
| 130 | + |
| 131 | + |
| 132 | +@pytest.fixture |
| 133 | +def human_input(): |
| 134 | + transport = pLCMTransport("/human_input") |
| 135 | + transport.lcm.start() |
| 136 | + |
| 137 | + def send_human_input(message: str) -> None: |
| 138 | + transport.publish(message) |
| 139 | + |
| 140 | + yield send_human_input |
| 141 | + |
| 142 | + transport.lcm.stop() |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.skipif(bool(os.getenv("CI")), reason="LCM spy doesn't work in CI.") |
| 146 | +def test_dimos_robot_demo_e2e(lcm_spy, dimos_robot_call, human_input): |
| 147 | + lcm_spy.wait_for_topic("/rpc/DemoCalculatorSkill/set_LlmAgent_register_skills/res") |
| 148 | + lcm_spy.wait_for_topic("/rpc/HumanInput/start/res") |
| 149 | + lcm_spy.wait_for_message_content("/agent", b"AIMessage") |
| 150 | + |
| 151 | + human_input("what is 52983 + 587237") |
| 152 | + |
| 153 | + lcm_spy.wait_for_message_content("/agent", b"640220") |
| 154 | + |
| 155 | + assert "/rpc/DemoCalculatorSkill/sum_numbers/req" in lcm_spy.messages |
| 156 | + assert "/rpc/DemoCalculatorSkill/sum_numbers/res" in lcm_spy.messages |
0 commit comments