|
18 | 18 |
|
19 | 19 | from ..._utils import poll_until_condition |
20 | 20 | from apify import Actor |
| 21 | +from apify._actor import _ActorType |
21 | 22 | from apify._charging import ChargingManagerImplementation |
22 | 23 | from apify._consts import EXIT_CODE_ERROR_USER_FUNCTION_THREW, ActorEnvVars, ApifyEnvVars |
23 | 24 |
|
24 | 25 | if TYPE_CHECKING: |
25 | 26 | from collections.abc import AsyncGenerator, Callable |
26 | 27 |
|
27 | | - from apify._actor import _ActorType |
28 | | - |
29 | 28 |
|
30 | 29 | @pytest.fixture( |
31 | 30 | params=[ |
@@ -199,6 +198,27 @@ async def test_unhandled_exception_sets_error_exit_code() -> None: |
199 | 198 | assert actor.exit_code == EXIT_CODE_ERROR_USER_FUNCTION_THREW |
200 | 199 |
|
201 | 200 |
|
| 201 | +# The autouse `_isolate_test_environment` fixture forces the `exit_process` default to False so a clean |
| 202 | +# context exit does not call `sys.exit()`. Capture the genuine detector at import time and call it |
| 203 | +# directly so these tests exercise the real logic rather than the test-environment override. |
| 204 | +_detect_default_exit_process = _ActorType._get_default_exit_process |
| 205 | + |
| 206 | + |
| 207 | +def test_default_exit_process_true_when_scrapy_importable_but_not_running(monkeypatch: pytest.MonkeyPatch) -> None: |
| 208 | + """Regression for B7: `scrapy` merely being importable must not disable `exit_process`.""" |
| 209 | + pytest.importorskip('scrapy') |
| 210 | + monkeypatch.delenv('SCRAPY_SETTINGS_MODULE', raising=False) |
| 211 | + actor = Actor(exit_process=False) |
| 212 | + assert _detect_default_exit_process(actor) is True |
| 213 | + |
| 214 | + |
| 215 | +def test_default_exit_process_false_when_running_under_scrapy(monkeypatch: pytest.MonkeyPatch) -> None: |
| 216 | + """The Scrapy runner sets `SCRAPY_SETTINGS_MODULE`, which must disable `exit_process` by default.""" |
| 217 | + monkeypatch.setenv('SCRAPY_SETTINGS_MODULE', 'src.settings') |
| 218 | + actor = Actor(exit_process=False) |
| 219 | + assert _detect_default_exit_process(actor) is False |
| 220 | + |
| 221 | + |
202 | 222 | async def test_actor_stops_periodic_events_after_exit(monkeypatch: pytest.MonkeyPatch) -> None: |
203 | 223 | """Test that periodic events (PERSIST_STATE and SYSTEM_INFO) stop emitting after Actor exits.""" |
204 | 224 | monkeypatch.setenv(ApifyEnvVars.SYSTEM_INFO_INTERVAL_MILLIS, '100') |
|
0 commit comments