|
4 | 4 | import contextlib |
5 | 5 | import logging |
6 | 6 | from typing import TYPE_CHECKING |
| 7 | +from unittest.mock import AsyncMock |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 |
|
@@ -232,3 +233,28 @@ async def test_actor_fail_prevents_further_execution(caplog: pytest.LogCaptureFi |
232 | 233 | status_records = [r for r in caplog.records if r.msg == '[Terminal status message]: cde'] |
233 | 234 | assert len(status_records) == 1 |
234 | 235 | assert status_records[0].levelno == logging.INFO |
| 236 | + |
| 237 | + |
| 238 | +@pytest.mark.parametrize( |
| 239 | + ('first_with_call', 'second_with_call'), |
| 240 | + [ |
| 241 | + pytest.param(False, False, id='both_without_call'), |
| 242 | + pytest.param(False, True, id='first_without_call'), |
| 243 | + pytest.param(True, False, id='second_without_call'), |
| 244 | + pytest.param(True, True, id='both_with_call'), |
| 245 | + ], |
| 246 | +) |
| 247 | +async def test_actor_sequential_contexts(*, first_with_call: bool, second_with_call: bool) -> None: |
| 248 | + """Test that Actor and Actor() can be used in two sequential async context manager blocks.""" |
| 249 | + mock = AsyncMock() |
| 250 | + async with Actor(exit_process=False) if first_with_call else Actor as actor: |
| 251 | + await mock() |
| 252 | + assert actor._is_initialized is True |
| 253 | + |
| 254 | + # After exiting the context, new Actor instance can be created without conflicts. |
| 255 | + async with Actor() if second_with_call else Actor as actor: |
| 256 | + await mock() |
| 257 | + assert actor._is_initialized is True |
| 258 | + |
| 259 | + # The mock should have been called twice, once in each context. |
| 260 | + assert mock.call_count == 2 |
0 commit comments