|
| 1 | +"""Tests for PythonExecutorService with Ray service registry.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +# Skip all tests if Ray is not available |
| 7 | +pytest.importorskip("ray") |
| 8 | + |
| 9 | +import ray |
| 10 | +from torchrl.envs.llm.transforms import PythonExecutorService, PythonInterpreter |
| 11 | +from torchrl.services import get_services |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def ray_init(): |
| 16 | + """Initialize Ray for tests.""" |
| 17 | + if not ray.is_initialized(): |
| 18 | + ray.init() |
| 19 | + yield |
| 20 | + if ray.is_initialized(): |
| 21 | + ray.shutdown() |
| 22 | + |
| 23 | + |
| 24 | +class TestPythonExecutorService: |
| 25 | + """Test suite for PythonExecutorService.""" |
| 26 | + |
| 27 | + def test_service_initialization(self, ray_init): |
| 28 | + """Test that the service can be created and registered.""" |
| 29 | + namespace = "test_executor_init" |
| 30 | + services = get_services(backend="ray", namespace=namespace) |
| 31 | + |
| 32 | + try: |
| 33 | + services.register( |
| 34 | + "python_executor", |
| 35 | + PythonExecutorService, |
| 36 | + pool_size=2, |
| 37 | + timeout=5.0, |
| 38 | + num_cpus=2, |
| 39 | + max_concurrency=2, |
| 40 | + ) |
| 41 | + |
| 42 | + # Verify it was registered |
| 43 | + assert "python_executor" in services |
| 44 | + |
| 45 | + # Get the service |
| 46 | + executor = services["python_executor"] |
| 47 | + assert executor is not None |
| 48 | + |
| 49 | + finally: |
| 50 | + services.reset() |
| 51 | + |
| 52 | + def test_service_execution(self, ray_init): |
| 53 | + """Test that the service can execute Python code.""" |
| 54 | + namespace = "test_executor_exec" |
| 55 | + services = get_services(backend="ray", namespace=namespace) |
| 56 | + |
| 57 | + try: |
| 58 | + services.register( |
| 59 | + "python_executor", |
| 60 | + PythonExecutorService, |
| 61 | + pool_size=2, |
| 62 | + timeout=5.0, |
| 63 | + num_cpus=2, |
| 64 | + max_concurrency=2, |
| 65 | + ) |
| 66 | + |
| 67 | + executor = services["python_executor"] |
| 68 | + |
| 69 | + # Execute simple code |
| 70 | + code = """ |
| 71 | +x = 10 |
| 72 | +y = 20 |
| 73 | +result = x + y |
| 74 | +print(f"Result: {result}") |
| 75 | +""" |
| 76 | + result = ray.get(executor.execute.remote(code), timeout=2) |
| 77 | + |
| 78 | + assert result["success"] is True |
| 79 | + assert "Result: 30" in result["stdout"] |
| 80 | + assert result["returncode"] == 0 |
| 81 | + |
| 82 | + finally: |
| 83 | + services.reset() |
| 84 | + |
| 85 | + def test_service_execution_error(self, ray_init): |
| 86 | + """Test that the service handles execution errors.""" |
| 87 | + namespace = "test_executor_error" |
| 88 | + services = get_services(backend="ray", namespace=namespace) |
| 89 | + |
| 90 | + try: |
| 91 | + services.register( |
| 92 | + "python_executor", |
| 93 | + PythonExecutorService, |
| 94 | + pool_size=2, |
| 95 | + timeout=5.0, |
| 96 | + num_cpus=2, |
| 97 | + max_concurrency=2, |
| 98 | + ) |
| 99 | + |
| 100 | + executor = services["python_executor"] |
| 101 | + |
| 102 | + # Execute code with an error |
| 103 | + code = "raise ValueError('Test error')" |
| 104 | + result = ray.get(executor.execute.remote(code), timeout=2) |
| 105 | + |
| 106 | + assert result["success"] is False |
| 107 | + assert "ValueError: Test error" in result["stderr"] |
| 108 | + |
| 109 | + finally: |
| 110 | + services.reset() |
| 111 | + |
| 112 | + def test_multiple_executions(self, ray_init): |
| 113 | + """Test multiple concurrent executions.""" |
| 114 | + namespace = "test_executor_multi" |
| 115 | + services = get_services(backend="ray", namespace=namespace) |
| 116 | + |
| 117 | + try: |
| 118 | + services.register( |
| 119 | + "python_executor", |
| 120 | + PythonExecutorService, |
| 121 | + pool_size=4, |
| 122 | + timeout=5.0, |
| 123 | + num_cpus=4, |
| 124 | + max_concurrency=4, |
| 125 | + ) |
| 126 | + |
| 127 | + executor = services["python_executor"] |
| 128 | + |
| 129 | + # Submit multiple executions |
| 130 | + futures = [] |
| 131 | + for i in range(8): |
| 132 | + code = f"print('Execution {i}')" |
| 133 | + futures.append(executor.execute.remote(code)) |
| 134 | + |
| 135 | + # Wait for all to complete |
| 136 | + results = ray.get(futures, timeout=5) |
| 137 | + |
| 138 | + # All should succeed |
| 139 | + assert len(results) == 8 |
| 140 | + for i, result in enumerate(results): |
| 141 | + assert result["success"] is True |
| 142 | + assert f"Execution {i}" in result["stdout"] |
| 143 | + |
| 144 | + finally: |
| 145 | + services.reset() |
| 146 | + |
| 147 | + |
| 148 | +class TestPythonInterpreterWithService: |
| 149 | + """Test suite for PythonInterpreter using the service.""" |
| 150 | + |
| 151 | + def test_interpreter_with_service(self, ray_init): |
| 152 | + """Test that PythonInterpreter can use the service.""" |
| 153 | + namespace = "test_interp_service" |
| 154 | + services = get_services(backend="ray", namespace=namespace) |
| 155 | + |
| 156 | + try: |
| 157 | + # Register service |
| 158 | + services.register( |
| 159 | + "python_executor", |
| 160 | + PythonExecutorService, |
| 161 | + pool_size=2, |
| 162 | + timeout=5.0, |
| 163 | + num_cpus=2, |
| 164 | + max_concurrency=2, |
| 165 | + ) |
| 166 | + |
| 167 | + # Create interpreter with service |
| 168 | + interpreter = PythonInterpreter( |
| 169 | + services="ray", |
| 170 | + service_name="python_executor", |
| 171 | + namespace=namespace, |
| 172 | + ) |
| 173 | + |
| 174 | + # Verify it's using the service |
| 175 | + assert interpreter.python_service is not None |
| 176 | + assert interpreter.processes is None |
| 177 | + assert interpreter.services == "ray" |
| 178 | + |
| 179 | + finally: |
| 180 | + services.reset() |
| 181 | + |
| 182 | + def test_interpreter_without_service(self): |
| 183 | + """Test that PythonInterpreter works without service.""" |
| 184 | + # Create interpreter without service |
| 185 | + interpreter = PythonInterpreter( |
| 186 | + services=None, |
| 187 | + persistent=True, |
| 188 | + ) |
| 189 | + |
| 190 | + # Verify it's using local processes |
| 191 | + assert interpreter.python_service is None |
| 192 | + assert interpreter.processes is not None |
| 193 | + assert interpreter.services is None |
| 194 | + |
| 195 | + def test_interpreter_execution_with_service(self, ray_init): |
| 196 | + """Test code execution through interpreter with service.""" |
| 197 | + namespace = "test_interp_exec" |
| 198 | + services = get_services(backend="ray", namespace=namespace) |
| 199 | + |
| 200 | + try: |
| 201 | + # Register service |
| 202 | + services.register( |
| 203 | + "python_executor", |
| 204 | + PythonExecutorService, |
| 205 | + pool_size=2, |
| 206 | + timeout=5.0, |
| 207 | + num_cpus=2, |
| 208 | + max_concurrency=2, |
| 209 | + ) |
| 210 | + |
| 211 | + # Create interpreter with service |
| 212 | + interpreter = PythonInterpreter(services="ray", namespace=namespace) |
| 213 | + |
| 214 | + # Execute code |
| 215 | + code = "print('Hello from service')" |
| 216 | + result = interpreter._execute_python_code(code, 0) |
| 217 | + |
| 218 | + assert result["success"] is True |
| 219 | + assert "Hello from service" in result["stdout"] |
| 220 | + |
| 221 | + finally: |
| 222 | + services.reset() |
| 223 | + |
| 224 | + def test_interpreter_clone_preserves_service(self, ray_init): |
| 225 | + """Test that cloning an interpreter preserves service settings.""" |
| 226 | + namespace = "test_interp_clone" |
| 227 | + services = get_services(backend="ray", namespace=namespace) |
| 228 | + |
| 229 | + try: |
| 230 | + # Register service |
| 231 | + services.register( |
| 232 | + "python_executor", |
| 233 | + PythonExecutorService, |
| 234 | + pool_size=2, |
| 235 | + timeout=5.0, |
| 236 | + num_cpus=2, |
| 237 | + max_concurrency=2, |
| 238 | + ) |
| 239 | + |
| 240 | + # Create interpreter with service |
| 241 | + interpreter1 = PythonInterpreter( |
| 242 | + services="ray", |
| 243 | + service_name="python_executor", |
| 244 | + namespace=namespace, |
| 245 | + ) |
| 246 | + |
| 247 | + # Clone it |
| 248 | + interpreter2 = interpreter1.clone() |
| 249 | + |
| 250 | + # Verify clone has same settings |
| 251 | + assert interpreter2.services == "ray" |
| 252 | + assert interpreter2.service_name == "python_executor" |
| 253 | + assert interpreter2.python_service is not None |
| 254 | + |
| 255 | + finally: |
| 256 | + services.reset() |
| 257 | + |
| 258 | + def test_interpreter_invalid_service_backend(self): |
| 259 | + """Test that invalid service backend raises error.""" |
| 260 | + with pytest.raises(ValueError, match="Invalid services backend"): |
| 261 | + PythonInterpreter(services="invalid") |
| 262 | + |
| 263 | + def test_interpreter_missing_service(self, ray_init): |
| 264 | + """Test that missing service raises error.""" |
| 265 | + with pytest.raises(RuntimeError, match="Failed to get Ray service"): |
| 266 | + PythonInterpreter(services="ray", service_name="nonexistent_service") |
| 267 | + |
| 268 | + |
| 269 | +if __name__ == "__main__": |
| 270 | + pytest.main([__file__, "-v"]) |
0 commit comments