|
7 | 7 | from litestar.cli._utils import LitestarGroup |
8 | 8 | from redis.asyncio import Redis |
9 | 9 |
|
10 | | -from litestar_saq.cli import _terminate_worker_processes |
| 10 | +from litestar_saq.cli import _terminate_worker_processes, get_max_shutdown_timeout |
11 | 11 | from tests.test_cli.conftest import CreateAppFileFixture |
12 | 12 |
|
13 | 13 | if TYPE_CHECKING: |
@@ -230,3 +230,38 @@ def tracking_add_handler(sig: int, _callback: Any, *_args: Any) -> None: |
230 | 230 | asyncio.set_event_loop(None) |
231 | 231 | else: |
232 | 232 | asyncio.set_event_loop(original_loop) |
| 233 | + |
| 234 | + |
| 235 | +def test_get_max_shutdown_timeout_handles_none_grace_period() -> None: |
| 236 | + """Test that get_max_shutdown_timeout handles None values for _shutdown_grace_period_s.""" |
| 237 | + mock_worker = Mock() |
| 238 | + mock_worker._shutdown_grace_period_s = None |
| 239 | + mock_worker._cancellation_hard_deadline_s = 10 |
| 240 | + |
| 241 | + timeout = get_max_shutdown_timeout([mock_worker]) |
| 242 | + |
| 243 | + assert timeout == 12.0 |
| 244 | + |
| 245 | + |
| 246 | +def test_get_max_shutdown_timeout_with_valid_values() -> None: |
| 247 | + """Test that get_max_shutdown_timeout calculates correctly with valid values.""" |
| 248 | + mock_worker1 = Mock() |
| 249 | + mock_worker1._shutdown_grace_period_s = 3.0 |
| 250 | + mock_worker1._cancellation_hard_deadline_s = 5.0 |
| 251 | + |
| 252 | + mock_worker2 = Mock() |
| 253 | + mock_worker2._shutdown_grace_period_s = 2.0 |
| 254 | + mock_worker2._cancellation_hard_deadline_s = 3.0 |
| 255 | + |
| 256 | + timeout = get_max_shutdown_timeout([mock_worker1, mock_worker2]) |
| 257 | + |
| 258 | + assert timeout == 10.0 |
| 259 | + |
| 260 | + |
| 261 | +def test_get_max_shutdown_timeout_falls_back_to_default() -> None: |
| 262 | + """Test that get_max_shutdown_timeout falls back to default when no grace periods configured.""" |
| 263 | + mock_worker = Mock(spec=[]) |
| 264 | + |
| 265 | + timeout = get_max_shutdown_timeout([mock_worker]) |
| 266 | + |
| 267 | + assert timeout == 5.0 |
0 commit comments