diff --git a/changelog.d/v2-remove-plain-worker-functions.removed.md b/changelog.d/v2-remove-plain-worker-functions.removed.md new file mode 100644 index 000000000..2fe4793b3 --- /dev/null +++ b/changelog.d/v2-remove-plain-worker-functions.removed.md @@ -0,0 +1 @@ +Removed the plain `run_single_simulation_us`, `run_single_simulation_uk` and `run_single_simulation_segment_us` functions from the API v2 app. The workers exist only as methods of `SimulationWorkerUS`, `SimulationWorkerUK` and `SimulationSegmentWorkerUS`, and the entry service, the report coordinator and the segment fan-out start them only through those classes, with no fallback to a plain function. An API v2 app deployed before the classes, such as an older .py version's app made the default by hand, can no longer run standalone simulation runs or cliff calculations. diff --git a/docs/engineering/skills/observability.md b/docs/engineering/skills/observability.md index 9355e9637..7689dd3dc 100644 --- a/docs/engineering/skills/observability.md +++ b/docs/engineering/skills/observability.md @@ -34,7 +34,7 @@ for these IDs names one. Do not use `job_id` for a v2 call: it names one annual Modal invocation of the v1 executor. A v2 simulation worker's call (the US and UK single-simulation workers and the -US segment worker, as class methods or plain functions) also says which +US segment worker, each a class method) also says which deployment served it, as the versioned app's workers do: its operation's `modal_app_name` names the v2 app, and its records' `service.version` is the bundle's policyengine.py version. Its `service.name` stays the worker's, such @@ -259,8 +259,7 @@ Two attributes are set on the operation of each call of: - the v2 app's simulation workers (the `SimulationWorkerUS.run_single_simulation_us`, `SimulationWorkerUK.run_single_simulation_uk` and - `SimulationSegmentWorkerUS.run_single_simulation_segment_us` methods, and - the plain functions of the same names kept for callers on older code), in + `SimulationSegmentWorkerUS.run_single_simulation_segment_us` methods), in `src/modal/v2_app.py`. Their records come from the services `policyengine-stage12-us-worker`, `policyengine-stage12-uk-worker` and `policyengine-stage12-us-segment-worker`. The v2 coordinator and household @@ -275,7 +274,7 @@ opens, so a call that fails or is preempted carries them too. | Attribute | Values | |---|---| -| `container_start` | `restored_from_snapshot`: the container was restored from a memory snapshot, so it started with the country models loaded. `took_snapshot`: Modal took the snapshot from this container, after the snapshot hook loaded the models in it. `no_snapshot`: the container neither took a snapshot nor was restored from one. That is every container of a plain function, which has no snapshot hook (`run_budget_window_batch`, and the v2 app's plain worker functions), and a worker class container that Modal started without snapshots, whose hook loaded the models before its first call. | +| `container_start` | `restored_from_snapshot`: the container was restored from a memory snapshot, so it started with the country models loaded. `took_snapshot`: Modal took the snapshot from this container, after the snapshot hook loaded the models in it. `no_snapshot`: the container neither took a snapshot nor was restored from one. That is every container of a plain function, which has no snapshot hook (`run_budget_window_batch`), and a worker class container that Modal started without snapshots, whose hook loaded the models before its first call. | | `container_call_number` | The call's place among the container's calls that opened their operation: 1 for the first, 2 for the second, and so on. A call counts even when reading or setting these attributes fails. | How a call tells them apart, in Modal 1.6.4: diff --git a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/stage12_simulation_workers.py b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/stage12_simulation_workers.py index a2ba0880a..231c98c67 100644 --- a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/stage12_simulation_workers.py +++ b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/stage12_simulation_workers.py @@ -16,9 +16,9 @@ coordinator's context and the comparison-run rows all hold it. So callers keep the stored name and look its class up here. -Apps deployed before the classes define the workers only as plain functions -with these names, and the v2 application keeps those functions for callers -still running older code. +Callers start the workers only through these classes. Apps deployed before the +classes, such as older .py versions' apps, define the workers only as plain +functions with these names, so those calls fail there. """ from __future__ import annotations diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_workers.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_workers.py index 72ad1e7ad..9e6f449b5 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_workers.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_workers.py @@ -10,8 +10,9 @@ with ``spawn``, so a route waits only for Modal to accept the call, and is given the run IDs it claims and records, never a Stage 12 evaluation record. The simulation function is a method of a Modal class, whose -containers restore from a memory snapshot with the models loaded, in apps -deployed since the classes; older apps define it as a plain function. +containers restore from a memory snapshot with the models loaded, and is +started only through that class; apps deployed before the classes define it +only as a plain function, so a default naming one of them can't start it. This is the economy path's own Modal access, apart from the Stage 12 comparison path's. @@ -273,33 +274,27 @@ async def _spawn_simulation_worker( ) -> str: """Start the simulation worker's method on its class; return its call ID. - An app deployed before the workers were classes, which the manifest's - default can name after a move or a rollback, has no such class, so it - gets the plain function of the same name. Only the class lookup falls - back: once the class resolves, a failed spawn raises as it is. + The worker is reached only through its class. Modal looks the class up + when the method spawns, so an app without it, such as one deployed + before the workers were classes, fails the spawn with Modal's + ``NotFoundError``. A name with no class is started as a function. """ class_name = worker_class_name(worker.simulation_callable) - if class_name is not None: - worker_class = modal.Cls.from_name( - worker.application_name, - class_name, - environment_name=self._environment, + if class_name is None: + return await self._spawn( + worker, + worker.simulation_callable, + payload, + context_payload, + observability_context=observability_context, ) - try: - await worker_class.hydrate.aio() - except modal.exception.NotFoundError: - pass - else: - method = getattr(worker_class(), worker.simulation_callable) - return await self._spawn_call( - method, - worker.simulation_callable, - payload, - context_payload, - observability_context=observability_context, - ) - return await self._spawn( - worker, + worker_class = modal.Cls.from_name( + worker.application_name, + class_name, + environment_name=self._environment, + ) + return await self._spawn_call( + getattr(worker_class(), worker.simulation_callable), worker.simulation_callable, payload, context_payload, diff --git a/projects/policyengine-simulation-entry/tests/test_economy_workers.py b/projects/policyengine-simulation-entry/tests/test_economy_workers.py index 198d415e8..064b761e5 100644 --- a/projects/policyengine-simulation-entry/tests/test_economy_workers.py +++ b/projects/policyengine-simulation-entry/tests/test_economy_workers.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import dataclasses from types import SimpleNamespace from typing import Any @@ -139,29 +140,41 @@ def from_name(application: str, name: str, *, environment_name: str): } +CLASS_NOT_FOUND = "the app defines no such class" + + +class MissingClassMethod: + """A method of a class the app doesn't define, looked up by name. + + Modal resolves the class when the method is first spawned, so the spawn + raises Modal's not-found error. + """ + + def __init__(self) -> None: + self.spawn = SimpleNamespace(aio=self._spawn) + + async def _spawn(self, *_arguments: Any, **_keywords: Any) -> SimpleNamespace: + raise modal.exception.NotFoundError(CLASS_NOT_FOUND) + + class FakeWorkerClass: """A Modal class looked up by name, whose worker method spawns ``method``. The instance has only the looked-up class's method, under its name - (``method_name``). With ``deployed`` False, hydrating it fails as it does - for an app deployed before the workers were classes. + (``method_name``). With ``deployed`` False the app has no such class, as + an app deployed before the workers were classes has none, so spawning its + method fails. """ def __init__(self, method: FakeFunction) -> None: self.method = method self.method_name: str | None = None self.deployed = True - self.hydrate = SimpleNamespace(aio=self._hydrate) - - async def _hydrate(self) -> FakeWorkerClass: - if not self.deployed: - msg = "the app defines no such class" - raise modal.exception.NotFoundError(msg) - return self def __call__(self) -> SimpleNamespace: assert self.method_name is not None - return SimpleNamespace(**{self.method_name: self.method}) + method = self.method if self.deployed else MissingClassMethod() + return SimpleNamespace(**{self.method_name: method}) @pytest.fixture @@ -267,29 +280,63 @@ def test_a_cliff_calculation_starts_the_countrys_simulation_worker_class( @pytest.mark.parametrize("start", ["start_simulation_run", "start_cliff_calculation"]) -def test_an_app_without_the_worker_class_gets_the_simulation_function( +def test_an_app_without_the_worker_class_fails_the_call_and_starts_no_function( modal_functions, modal_worker_classes, start ) -> None: - # An app deployed for an older .py version, which the manifest's default - # can name after a move or a rollback, defines the worker as a function. + # An app deployed before the workers were classes, which the manifest's + # default names only if someone moves it back by hand, defines the worker + # only as a plain function. Callers start workers only through the class. looked_up_functions, function = modal_functions _looked_up_classes, worker_class = modal_worker_classes worker_class.deployed = False worker = default_economy_worker(economy_manifest(), "us") + with pytest.raises(modal.exception.NotFoundError, match=CLASS_NOT_FOUND): + asyncio.run( + getattr(ModalEconomyRunCaller("main"), start)( + worker, + {"simulation": 1}, + {"run": "context"}, + observability_context=None, + ) + ) + + assert looked_up_functions == [] + assert function.spawns == [] + + +@pytest.mark.parametrize("start", ["start_simulation_run", "start_cliff_calculation"]) +def test_a_simulation_callable_without_a_worker_class_starts_as_a_function( + modal_functions, modal_worker_classes, start +) -> None: + # Only the v2 simulation workers are class methods; a callable the + # contract maps to no class is started as a plain function of the app. + looked_up_functions, function = modal_functions + looked_up_classes, _worker_class = modal_worker_classes + worker = dataclasses.replace( + default_economy_worker(economy_manifest(), "us"), + simulation_callable="a_function_of_another_kind", + ) + call_id = asyncio.run( getattr(ModalEconomyRunCaller("main"), start)( - worker, {"simulation": 1}, {"run": "context"}, observability_context=None + worker, + {"simulation": 1}, + {"run": "context"}, + observability_context={"observability_id": "observed"}, ) ) assert call_id == "fc-started" + assert looked_up_classes == [] assert looked_up_functions == [ - (worker.application_name, "run_single_simulation_us", "main") + (worker.application_name, "a_function_of_another_kind", "main") ] - assert worker_class.method.spawns == [] assert function.spawns == [ - (({"simulation": 1}, {"run": "context"}), {"observability_context": None}) + ( + ({"simulation": 1}, {"run": "context"}), + {"observability_context": {"observability_id": "observed"}}, + ) ] diff --git a/projects/policyengine-simulation-executor/src/modal/v2_app.py b/projects/policyengine-simulation-executor/src/modal/v2_app.py index e216bd442..3435af165 100644 --- a/projects/policyengine-simulation-executor/src/modal/v2_app.py +++ b/projects/policyengine-simulation-executor/src/modal/v2_app.py @@ -186,13 +186,8 @@ def validate_worker_uk() -> dict: # Each simulation worker is a method of a Modal class whose containers restore -# from a memory snapshot with the country's models loaded, and a plain function -# of the same name and options, without a snapshot, for callers still running -# code from before the classes: an entry service revision (the stable one -# during a deploy, or one a rollback routes traffic back to), and a coordinator -# or US worker call running while this app is redeployed in place. Both run the -# same calculation. Their CPU, memory, time and container limits are in -# src/modal/v2_worker_resources.py. +# from a memory snapshot with the country's models loaded. Their CPU, memory, +# time and container limits are in src/modal/v2_worker_resources.py. _SEGMENT_WORKER_OPTIONS: dict[str, Any] = { **SEGMENT_WORKER_RESOURCES, "image": us_worker_image, @@ -479,47 +474,6 @@ def run_single_simulation_uk( ) -@app.function(**_SEGMENT_WORKER_OPTIONS) -def run_single_simulation_segment_us( - payload: dict, - *, - observability_context: dict | None = None, - simulation_run_id: str | None = None, -) -> dict: - """``SimulationSegmentWorkerUS``'s method, for callers on older code.""" - return _run_single_simulation_segment_us( - payload, - observability_context=observability_context, - simulation_run_id=simulation_run_id, - ) - - -@app.function(**_US_SIMULATION_WORKER_OPTIONS) -def run_single_simulation_us( - payload: dict, - context: dict, - *, - observability_context: dict | None = None, -) -> dict: - """``SimulationWorkerUS``'s method, for callers on older code.""" - return _run_single_simulation_us( - payload, context, observability_context=observability_context - ) - - -@app.function(**_UK_SIMULATION_WORKER_OPTIONS) -def run_single_simulation_uk( - payload: dict, - context: dict, - *, - observability_context: dict | None = None, -) -> dict: - """``SimulationWorkerUK``'s method, for callers on older code.""" - return _run_single_simulation_uk( - payload, context, observability_context=observability_context - ) - - @app.function( image=coordinator_image, cpu=2.0, diff --git a/projects/policyengine-simulation-executor/src/modal/v2_worker_resources.py b/projects/policyengine-simulation-executor/src/modal/v2_worker_resources.py index ab79e7859..6559380e0 100644 --- a/projects/policyengine-simulation-executor/src/modal/v2_worker_resources.py +++ b/projects/policyengine-simulation-executor/src/modal/v2_worker_resources.py @@ -1,7 +1,6 @@ """CPU, memory, time and container limits of the v2 app's simulation workers. -``src/modal/v2_app.py`` gives each simulation worker class, and the plain -function of the same name kept for callers on older code, these settings +``src/modal/v2_app.py`` gives each simulation worker class these settings together with its image and secrets. """ diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/fanout.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/fanout.py index 4b6203053..514fa6a46 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/fanout.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/fanout.py @@ -80,10 +80,11 @@ def __init__( # noqa: PLR0913 # the simulation, then keyword-only app, runtime simulation_run_id: UUID | None = None, seams: SegmentRunnerSeams | None = None, ) -> None: - """Look up the segment worker deployed in ``app_name``. + """Take the segment worker's method from its Modal class in ``app_name``. - It is a method of a Modal class whose containers restore from a - memory snapshot with the US models loaded (``deployed_worker``). + Its containers restore from a memory snapshot with the US models + loaded (``deployed_worker``). Modal looks the class up when ``run`` + spawns the first segment, so an app without it fails there. ``cancellation_requested`` is checked between polls. Each segment's call is passed ``simulation_run_id``, the detailed-save simulation run diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/worker_lookup.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/worker_lookup.py index 04734aa3a..e9a8635a8 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/worker_lookup.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/worker_lookup.py @@ -21,26 +21,19 @@ def deployed_worker( """Return what spawns ``method_name`` in ``application_name``. A simulation worker is a method of a Modal class, whose containers restore - from a memory snapshot with the models loaded. An application deployed - from a commit from before the classes defines it as a plain function of - the same name, and a call still running when its application is rolled - back in place to such a commit finds only that function. So a class Modal - doesn't find gives the function. Only the class lookup falls back: once - the class resolves, a failed spawn raises as it is. A name with no class - is looked up as a function. ``lookup_options``, such as - ``environment_name``, go to either lookup. + from a memory snapshot with the models loaded, and it is reached only + through that class. Modal looks the class up when the method first + spawns, so an application without the class, such as one deployed from a + commit from before the classes, fails that spawn with Modal's + ``NotFoundError``. A name with no class is looked up as a function. + ``lookup_options``, such as ``environment_name``, go to either lookup. """ class_name = worker_class_name(method_name) - if class_name is not None: - worker_class = modal_module.Cls.from_name( - application_name, class_name, **lookup_options + if class_name is None: + return modal_module.Function.from_name( + application_name, method_name, **lookup_options ) - try: - worker_class.hydrate() - except modal_module.exception.NotFoundError: - pass - else: - return getattr(worker_class(), method_name) - return modal_module.Function.from_name( - application_name, method_name, **lookup_options + worker_class = modal_module.Cls.from_name( + application_name, class_name, **lookup_options ) + return getattr(worker_class(), method_name) diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_budget_window_capacity.py b/projects/policyengine-simulation-executor/tests/test_stage12_budget_window_capacity.py index a035518bc..804f6d3aa 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_budget_window_capacity.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_budget_window_capacity.py @@ -14,7 +14,7 @@ US_REGION_GROUPS, ) from policyengine_stage12_persistence import BUDGET_WINDOW_MAX_RUNNING_YEARS -from test_stage12_modal_app import _load +from test_stage12_modal_app import _load, declared_modal_options # Budget windows the coordinator has room for at once, each at its cap, # beside the coordinator calls it had for other reports. @@ -26,7 +26,7 @@ def test_the_coordinator_holds_three_windows_at_their_cap_and_other_reports( monkeypatch, ) -> None: - functions = dict(_load(monkeypatch).app.function_calls) + functions = declared_modal_options(_load(monkeypatch)) assert ( functions["coordinate_report"]["max_containers"] @@ -38,7 +38,7 @@ def test_the_coordinator_holds_three_windows_at_their_cap_and_other_reports( def test_each_simulation_worker_holds_both_simulations_of_every_coordinator( monkeypatch, ) -> None: - functions = dict(_load(monkeypatch).app.function_calls) + functions = declared_modal_options(_load(monkeypatch)) coordinators = functions["coordinate_report"]["max_containers"] for worker in ("run_single_simulation_us", "run_single_simulation_uk"): @@ -50,7 +50,7 @@ def test_each_simulation_worker_holds_both_simulations_of_every_coordinator( def test_every_us_simulation_at_once_has_room_for_its_segments(monkeypatch) -> None: - functions = dict(_load(monkeypatch).app.function_calls) + functions = declared_modal_options(_load(monkeypatch)) assert ( functions["run_single_simulation_segment_us"]["max_containers"] diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_coordination.py b/projects/policyengine-simulation-executor/tests/test_stage12_coordination.py index 479f692a7..b7e3603cf 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_coordination.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_coordination.py @@ -536,8 +536,10 @@ class _ClassNotDeployedError(Exception): def _recording_modal(calls: list, *, class_deployed: bool = True) -> object: """Stand in for ``modal``, recording each lookup, spawn and restore in ``calls``. - With ``class_deployed`` False, hydrating a class raises the not-found - error, as for an app rolled back to a commit from before the classes. + With ``class_deployed`` False the app has no worker class, as an app + deployed from a commit from before the classes has none, so spawning its + method raises the not-found error: Modal resolves a class looked up by + name when its method first spawns. """ from types import SimpleNamespace @@ -545,21 +547,21 @@ def spawn(*spawn_args, **spawn_kwargs): calls.append(("spawn", spawn_args, spawn_kwargs)) return "spawned-call" + def method_spawn(*spawn_args, **spawn_kwargs): + spawned = spawn(*spawn_args, **spawn_kwargs) + if not class_deployed: + msg = "the app defines no such class" + raise _ClassNotDeployedError(msg) + return spawned + def function_from_name(*args, **kwargs): calls.append(("Function.from_name", args, kwargs)) return SimpleNamespace(spawn=spawn) class WorkerClass: - def hydrate(self): - calls.append(("Cls.hydrate", (), {})) - if not class_deployed: - msg = "the app defines no such class" - raise _ClassNotDeployedError(msg) - return self - def __call__(self): return SimpleNamespace( - run_single_simulation_us=SimpleNamespace(spawn=spawn) + run_single_simulation_us=SimpleNamespace(spawn=method_spawn) ) def class_from_name(*args, **kwargs): @@ -616,7 +618,6 @@ def import_modal(module_name): ("application", "SimulationWorkerUS"), {"environment_name": "staging"}, ), - ("Cls.hydrate", (), {}), ( "spawn", ({"role": "baseline"}, {"request_id": "request-1"}), @@ -626,7 +627,7 @@ def import_modal(module_name): ] -def test_modal_child_invoker_spawns_the_function_when_the_app_has_no_worker_class( +def test_modal_child_invoker_fails_the_spawn_when_the_app_has_no_worker_class( monkeypatch, ): from policyengine_simulation_executor.stage12_runtime import dependencies @@ -635,26 +636,14 @@ def test_modal_child_invoker_spawns_the_function_when_the_app_has_no_worker_clas fake_modal = _recording_modal(calls, class_deployed=False) monkeypatch.setattr(dependencies, "import_module", lambda _name: fake_modal) - spawned = dependencies.ModalChildInvoker().spawn( - _spawn_request(dependencies, "run_single_simulation_us") - ) + # An app redeployed in place from a commit from before the classes has + # only a plain function, which the coordinator never starts. + with pytest.raises(_ClassNotDeployedError): + dependencies.ModalChildInvoker().spawn( + _spawn_request(dependencies, "run_single_simulation_us") + ) - # A coordinator call still running when its app is rolled back in place, - # to a commit from before the classes, finds only the plain function. - assert spawned == "spawned-call" - assert calls[1:] == [ - ("Cls.hydrate", (), {}), - ( - "Function.from_name", - ("application", "run_single_simulation_us"), - {"environment_name": "staging"}, - ), - ( - "spawn", - ({"role": "baseline"}, {"request_id": "request-1"}), - {"observability_context": {"traceparent": "trace"}}, - ), - ] + assert [call[0] for call in calls] == ["Cls.from_name", "spawn"] def test_modal_child_invoker_spawns_a_callable_without_a_class_as_a_function( diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_modal_app.py b/projects/policyengine-simulation-executor/tests/test_stage12_modal_app.py index 58b792024..6306b7a0d 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_modal_app.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_modal_app.py @@ -12,6 +12,9 @@ import pytest from fixtures.fake_modal import install_fake_modal +from policyengine_simulation_contract.stage12_simulation_workers import ( + worker_class_name, +) from policyengine_simulation_executor.release_bundle import ( get_bundled_package_version, ) @@ -50,6 +53,31 @@ def _load(monkeypatch): return importlib.import_module("src.modal.v2_app") +# The v2 app's simulation workers, each a method of its own Modal class. +SIMULATION_WORKER_METHODS = ( + "run_single_simulation_us", + "run_single_simulation_uk", + "run_single_simulation_segment_us", +) + + +def _modal_callable(module, name: str): + """Return what serves calls of ``name``: a worker class's method, or a function.""" + class_name = worker_class_name(name) + if class_name is None: + return getattr(module, name) + return getattr(getattr(module, class_name)(), name) + + +def declared_modal_options(module) -> dict[str, dict]: + """Return each Modal function's options, and each worker class's by its method.""" + options = dict(module.app.function_calls) + class_options = dict(module.app.cls_calls) + for method_name in SIMULATION_WORKER_METHODS: + options[method_name] = class_options[worker_class_name(method_name)] + return options + + def _observability_context() -> dict[str, str]: return { "captured_at": "2026-09-24T00:00:00Z", @@ -76,7 +104,7 @@ def model_dump(self, *, mode): monkeypatch.setattr( stage12_runtime, "calculate_segment", lambda *_, **__: Result() ) - return module.run_single_simulation_segment_us( + return _modal_callable(module, function_name)( {"simulation": {"role": "baseline"}, "segment_index": 0}, observability_context=_observability_context(), ) @@ -107,7 +135,7 @@ def model_dump(self, *, mode): raising=False, ) monkeypatch.setattr(stage12_runtime, "run_single_simulation", implementation) - return getattr(module, function_name)( + return _modal_callable(module, function_name)( {"role": "baseline"}, {}, observability_context=_observability_context(), @@ -346,18 +374,17 @@ def test_v2_app_declares_validation_workers_and_non_http_coordinator( monkeypatch, ) -> None: module = _load(monkeypatch) - functions = dict(module.app.function_calls) - assert set(functions) == { + # The simulation workers are methods of Modal classes, with no plain + # function of the same name. + assert {name for name, _options in module.app.function_calls} == { "validate_worker_us", "validate_worker_uk", - "run_single_simulation_segment_us", - "run_single_simulation_us", - "run_single_simulation_uk", "coordinate_report", "calculate_household_us", "calculate_household_uk", } + functions = declared_modal_options(module) assert functions["run_single_simulation_us"]["image"] is module.us_worker_image assert functions["run_single_simulation_uk"]["image"] is module.uk_worker_image assert ( @@ -379,7 +406,7 @@ def test_only_report_run_writers_receive_the_modal_worker_database_secret( monkeypatch, ) -> None: module = _load(monkeypatch) - functions = dict(module.app.function_calls) + functions = declared_modal_options(module) worker_database_secret = { "args": ("policyengine-modal-worker-database",), "kwargs": {"required_keys": ["MODAL_WORKER_DATABASE_URL"]}, @@ -482,7 +509,7 @@ def test_stage12_functions_propagate_identifier_and_close_runtime( assert result == {"ok": True} # A simulation worker's call sets only its container start and call number - # (tests/test_v2_container_start.py); the plain functions have no snapshot. + # (tests/test_v2_container_start.py); no snapshot hook ran here. assert runtime.contexts == ( [] if function_name == "coordinate_report" @@ -985,7 +1012,7 @@ def succeed(*_arguments, **_keywords) -> dict: context = {"cliff_result_id": CLIFF_RESULT_ID} else: context = {"simulation_run_id": SIMULATION_RUN_ID} if names_record else {} - return getattr(module, function_name)( + return _modal_callable(module, function_name)( {"role": "baseline"}, context, observability_context=None ) if call_name == "run_single_simulation_segment_us": @@ -998,7 +1025,7 @@ def model_dump(self, *, mode): stage12_runtime, "calculate_segment", lambda *_, **__: SegmentResult() ) keywords = {"simulation_run_id": SIMULATION_RUN_ID} if names_record else {} - return module.run_single_simulation_segment_us( + return _modal_callable(module, call_name)( {"simulation": {"role": "baseline"}, "segment_index": 0}, observability_context=None, **keywords, @@ -1193,7 +1220,7 @@ def test_a_malformed_simulation_run_id_is_left_out_of_the_operation_record( stage12_runtime, "run_single_simulation", lambda *_, **__: {"ok": True} ) - module.run_single_simulation_us( + _modal_callable(module, "run_single_simulation_us")( {"role": "baseline"}, {"simulation_run_id": "not a run ID"}, observability_context=None, @@ -1246,7 +1273,7 @@ def run_single_simulation(payload, context, **keywords): ) monkeypatch.setattr(stage12_runtime, "run_single_simulation", run_single_simulation) - module.run_single_simulation_us( + _modal_callable(module, "run_single_simulation_us")( {"role": "baseline"}, context, observability_context=None ) @@ -1271,7 +1298,7 @@ def unreadable_call_id(): monkeypatch.setattr(module.modal, "current_function_call_id", unreadable_call_id) with pytest.raises(RuntimeError, match="unreadable"): - getattr(module, function_name)( + _modal_callable(module, function_name)( {"role": "baseline"}, {}, observability_context=None ) diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_segment_fanout.py b/projects/policyengine-simulation-executor/tests/test_stage12_segment_fanout.py index cd897c79a..8ae654cfc 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_segment_fanout.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_segment_fanout.py @@ -115,6 +115,14 @@ class ClassNotDeployedError(Exception): """Stands in for ``modal.exception.NotFoundError``.""" +class MissingClassMethod: + """The segment method of a class the app doesn't define: every spawn fails.""" + + def spawn(self, *_arguments, **_keywords): + msg = "the app defines no such class" + raise ClassNotDeployedError(msg) + + def _segment_worker_lookup( segment_function: object, lookups: list[tuple[str, str, str]] | None = None, @@ -124,8 +132,9 @@ def _segment_worker_lookup( """Stand in for ``modal``: its segment worker spawns ``segment_function``. Each lookup by name is appended to ``lookups`` as ``(kind, app, name)``. - With ``class_deployed`` False, hydrating the class raises the not-found - error, and the plain function of the method's name spawns instead. + With ``class_deployed`` False the app has no segment worker class, so + spawning its method raises the not-found error: Modal resolves a class + looked up by name when its method first spawns. """ def record(kind: str, app_name: str, name: str) -> None: @@ -133,13 +142,11 @@ def record(kind: str, app_name: str, name: str) -> None: lookups.append((kind, app_name, name)) class SegmentWorkerClass: - def hydrate(self): - if not class_deployed: - msg = "the app defines no such class" - raise ClassNotDeployedError(msg) - return self - def __call__(self): + if not class_deployed: + return SimpleNamespace( + run_single_simulation_segment_us=MissingClassMethod() + ) return SimpleNamespace(run_single_simulation_segment_us=segment_function) class ClassLookup: @@ -184,32 +191,29 @@ def test_segments_are_started_through_the_segment_worker_class() -> None: assert len(function.spawns) == len(US_REGION_GROUPS) -def test_segments_start_on_the_function_when_the_app_has_no_segment_class() -> None: - # A US simulation still running when its app is rolled back in place, to a - # commit from before the classes, finds only the plain function. +def test_segments_fail_to_start_when_the_app_has_no_segment_class() -> None: + # An app redeployed in place from a commit from before the classes has + # only a plain segment function, which a US simulation never starts. simulation = planned_simulation(SimulationRole.BASELINE) function = FakeFunction(_results(simulation), {}) lookups: list[tuple[str, str, str]] = [] - runner = Stage12SegmentRunner( - simulation, - app_name="stage12-app", - runtime=cast("ObservabilityRuntime", FakeRuntime()), - seams=SegmentRunnerSeams( - modal_module=_segment_worker_lookup( - function, lookups, class_deployed=False - ), - groups=US_REGION_GROUPS, - sleep=lambda _seconds: None, - ), - ) - runner.run() + with pytest.raises(ClassNotDeployedError): + Stage12SegmentRunner( + simulation, + app_name="stage12-app", + runtime=cast("ObservabilityRuntime", FakeRuntime()), + seams=SegmentRunnerSeams( + modal_module=_segment_worker_lookup( + function, lookups, class_deployed=False + ), + groups=US_REGION_GROUPS, + sleep=lambda _seconds: None, + ), + ).run() - assert lookups == [ - ("class", "stage12-app", "SimulationSegmentWorkerUS"), - ("function", "stage12-app", "run_single_simulation_segment_us"), - ] - assert len(function.spawns) == len(US_REGION_GROUPS) + assert lookups == [("class", "stage12-app", "SimulationSegmentWorkerUS")] + assert function.spawns == [] def _runner( diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_timeout_budgets.py b/projects/policyengine-simulation-executor/tests/test_stage12_timeout_budgets.py index 8e6f81323..7d238799d 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_timeout_budgets.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_timeout_budgets.py @@ -11,15 +11,12 @@ 2026-11-29 (Cloud Trace, 100 segmented US workers and 61 coordinators). """ -import importlib -import sys - -from fixtures.fake_modal import install_fake_modal from policyengine_simulation_executor import artifact_store from policyengine_simulation_executor.stage12_timeouts import ( PRODUCTION_RESULT_WAIT_TIMEOUT_SECONDS, SIMULATION_WAIT_TIMEOUT_SECONDS, ) +from test_stage12_modal_app import _load, declared_modal_options # A segmented US worker's time outside its region-group calls and its input # and output writes: loading, dispatching the calls and merging their results. @@ -39,11 +36,11 @@ def _longest_artifact_write_seconds() -> int: def _function_timeouts(monkeypatch) -> dict[str, int]: - install_fake_modal(monkeypatch) - sys.modules.pop("src.modal.v2_images", None) - sys.modules.pop("src.modal.v2_app", None) - v2_app = importlib.import_module("src.modal.v2_app") - return {name: options["timeout"] for name, options in v2_app.app.function_calls} + """Return each Modal function's timeout, and each worker class's by its method.""" + return { + name: options["timeout"] + for name, options in declared_modal_options(_load(monkeypatch)).items() + } def test_a_segmented_us_worker_has_room_for_its_calls_and_two_longest_writes( diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_wait_window.py b/projects/policyengine-simulation-executor/tests/test_stage12_wait_window.py index 50c72f275..064cee88c 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_wait_window.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_wait_window.py @@ -44,8 +44,7 @@ def _declared_modal_timeouts(monkeypatch): sys.modules.pop("src.modal.v2_images", None) sys.modules.pop("src.modal.v2_app", None) modal_app = importlib.import_module("src.modal.v2_app") - # The coordinator spawns the simulation workers' class methods; callers - # on older code spawn the plain functions. + # The coordinator spawns the simulation workers' class methods. return { name: options.get("timeout") for name, options in [*modal_app.app.function_calls, *modal_app.app.cls_calls] @@ -73,7 +72,8 @@ def test_declared_modal_timeouts_fit_the_shared_wait_window(monkeypatch) -> None if name.startswith("run_single_simulation") or name in worker_classes } - assert set(child_worker_timeouts) == {*worker_methods, *worker_classes} + # No plain function of a worker method's name is deployed. + assert set(child_worker_timeouts) == worker_classes # The window covers every worker's own Modal timeout. assert all(window_seconds >= timeout for timeout in child_worker_timeouts.values()) assert ( diff --git a/projects/policyengine-simulation-executor/tests/test_v2_container_start.py b/projects/policyengine-simulation-executor/tests/test_v2_container_start.py index 016af622c..fbf83ed18 100644 --- a/projects/policyengine-simulation-executor/tests/test_v2_container_start.py +++ b/projects/policyengine-simulation-executor/tests/test_v2_container_start.py @@ -1,11 +1,10 @@ """Tests for the container start attributes of the v2 simulation workers' calls. Each call of a v2 simulation worker (``SimulationWorkerUS``, -``SimulationWorkerUK`` and ``SimulationSegmentWorkerUS``, and the plain -functions kept for callers on older code) says on its operation how the -container serving it started (``container_start``) and which call it is in -that container (``container_call_number``), as the run_simulation app's -workers do (``tests/test_modal_container_start.py``). +``SimulationWorkerUK`` and ``SimulationSegmentWorkerUS``) says on its +operation how the container serving it started (``container_start``) and +which call it is in that container (``container_call_number``), as the +run_simulation app's workers do (``tests/test_modal_container_start.py``). """ from __future__ import annotations @@ -23,13 +22,12 @@ from policyengine_simulation_executor import stage12_runtime WORKER_RESULT = {"ok": True} -# The Modal task IDs of the container that takes the snapshot, of a container -# restored from it, and of a container that serves a plain function. +# The Modal task IDs of the container that takes the snapshot and of a +# container restored from it. SNAPSHOT_TASK_ID = "ta-01SNAPSHOTTING" RESTORED_TASK_ID = "ta-01RESTORED" -PLAIN_FUNCTION_TASK_ID = "ta-01PLAINFUNCTION" CONTAINER_START_KEYS = {"container_start", "container_call_number"} -# Each worker class's method has the name of the plain function it replaces. +# Each worker class's method has the name the worker had as a plain function. WORKER_METHOD_NAMES = ( "run_single_simulation_us", "run_single_simulation_uk", @@ -106,7 +104,7 @@ def v2_app(monkeypatch): def _call_worker(worker, method_name: str) -> dict: - """Call a worker method or plain function with the arguments its callers pass.""" + """Call a worker method with the arguments its callers pass.""" if method_name == SEGMENT_WORKER_METHOD: return worker( {"simulation": {"role": "baseline"}, "segment_index": 0}, @@ -207,21 +205,6 @@ def test_a_worker_container_started_without_snapshots_reports_no_snapshot( assert call_attributes["container_call_number"] == 1 -@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) -def test_a_plain_function_call_reports_a_container_started_without_a_snapshot( - v2_app, monkeypatch, method_name -): - # The plain functions kept for callers on older code have no snapshot hook. - monkeypatch.setenv("MODAL_TASK_ID", PLAIN_FUNCTION_TASK_ID) - attributes_by_call = _record_call_attributes(v2_app, monkeypatch) - - assert _call_worker(getattr(v2_app, method_name), method_name) == WORKER_RESULT - - [call_attributes] = attributes_by_call - assert call_attributes["container_start"] == "no_snapshot" - assert call_attributes["container_call_number"] == 1 - - @pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_each_call_in_a_container_reports_the_next_call_number( v2_app, monkeypatch, method_name diff --git a/projects/policyengine-simulation-executor/tests/test_v2_worker_app_and_version.py b/projects/policyengine-simulation-executor/tests/test_v2_worker_app_and_version.py index 6db7c0144..0a7cd1fe3 100644 --- a/projects/policyengine-simulation-executor/tests/test_v2_worker_app_and_version.py +++ b/projects/policyengine-simulation-executor/tests/test_v2_worker_app_and_version.py @@ -84,7 +84,7 @@ def v2_app(monkeypatch): def _call_worker(worker, method_name: str) -> dict: - """Call a worker method or plain function with the arguments its callers pass.""" + """Call a worker method with the arguments its callers pass.""" if method_name == SEGMENT_WORKER_METHOD: return worker( {"simulation": {"role": "baseline"}, "segment_index": 0}, @@ -93,17 +93,14 @@ def _call_worker(worker, method_name: str) -> dict: return worker({"role": "baseline"}, {}, observability_context=None) -def _worker(v2_app, method_name: str, worker_kind: str): - """Return the class method or the plain function serving one worker's calls.""" - if worker_kind == "class_method": - return getattr(getattr(v2_app, worker_class_name(method_name))(), method_name) - return getattr(v2_app, method_name) +def _worker(v2_app, method_name: str): + """Return the class method serving one worker's calls.""" + return getattr(getattr(v2_app, worker_class_name(method_name))(), method_name) -@pytest.mark.parametrize("worker_kind", ["class_method", "plain_function"]) @pytest.mark.parametrize("method_name", sorted(WORKER_SERVICE_NAMES)) def test_each_v2_worker_call_names_its_app_and_py_version( - v2_app, monkeypatch, method_name, worker_kind + v2_app, monkeypatch, method_name ): runtime = _OperationRecordingRuntime() runtime_identities: list[dict] = [] @@ -116,7 +113,7 @@ def configure_recording_runtime(**identity): v2_app, "init_process_observability", configure_recording_runtime ) - worker = _worker(v2_app, method_name, worker_kind) + worker = _worker(v2_app, method_name) assert _call_worker(worker, method_name) == WORKER_RESULT [identity] = runtime_identities @@ -135,7 +132,7 @@ def test_the_app_name_reaches_the_operation_record( runtime, written_records = recorded_observability_runtime monkeypatch.setattr(v2_app, "init_process_observability", lambda **_: runtime) - assert _call_worker(getattr(v2_app, method_name), method_name) == WORKER_RESULT + assert _call_worker(_worker(v2_app, method_name), method_name) == WORKER_RESULT [completion] = [ record diff --git a/projects/policyengine-simulation-executor/tests/test_v2_worker_classes.py b/projects/policyengine-simulation-executor/tests/test_v2_worker_classes.py index 4cb4775e4..8a51b91ae 100644 --- a/projects/policyengine-simulation-executor/tests/test_v2_worker_classes.py +++ b/projects/policyengine-simulation-executor/tests/test_v2_worker_classes.py @@ -51,7 +51,7 @@ def _worker_class(v2_app, method_name: str) -> type: def _expected_worker_options(v2_app) -> dict[str, dict]: - """Each worker's Modal options, shared by its class and its plain function.""" + """Each worker's Modal options, set on its class.""" simulation_worker_resources = { "cpu": 8.0, "memory": 32768, @@ -113,17 +113,18 @@ def test_each_v2_worker_class_defines_the_method_its_callers_name( assert getattr(segment_worker, SEGMENT_WORKER_METHOD) in recorded_methods -def test_plain_functions_keep_the_workers_for_callers_on_older_code( +def test_the_v2_app_defines_its_workers_only_as_class_methods( monkeypatch, ) -> None: v2_app = _load_v2_app(monkeypatch) - function_options = dict(v2_app.app.function_calls) + function_names = {name for name, _options in v2_app.app.function_calls} - # An entry service revision from before the classes, and a coordinator or - # US worker call running while the app is redeployed in place, look these - # functions up by name. They keep the workers' options, without snapshots. - for method_name, options in _expected_worker_options(v2_app).items(): - assert function_options[method_name] == options + # Every caller, the entry service revision kept for rollback included, + # starts the workers through their classes, so no plain function of a + # method's name is deployed. + for method_name in WORKER_METHOD_NAMES: + assert method_name not in function_names + assert not hasattr(v2_app, method_name) def test_each_v2_worker_class_has_one_snapshot_hook_and_one_restore_hook( @@ -317,8 +318,29 @@ def model_dump(self, *, mode): } +# What each worker's call hands its calculation and returns, with the +# calculation stubbed (``_call_worker``). +EXPECTED_WORKER_CALLS = { + "run_single_simulation_us": { + "result": {"calculated": "baseline", "context": {}}, + "modal_call_id": "fc-worker-call", + "required_country": "us", + }, + "run_single_simulation_uk": { + "result": {"calculated": "baseline", "context": {}}, + "modal_call_id": "fc-worker-call", + "required_country": "uk", + }, + "run_single_simulation_segment_us": { + "result": {"calculated": "segment"}, + "modal_call_id": None, + "required_country": None, + }, +} + + @pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) -def test_a_v2_worker_class_method_runs_the_same_code_as_its_plain_function( +def test_a_v2_worker_class_method_runs_its_worker_once_per_call( monkeypatch, method_name ) -> None: v2_app = _load_v2_app(monkeypatch) @@ -327,11 +349,11 @@ def test_a_v2_worker_class_method_runs_the_same_code_as_its_plain_function( through_the_class = _call_worker( monkeypatch, v2_app, getattr(worker_instance, method_name) ) - through_the_function = _call_worker( - monkeypatch, v2_app, getattr(v2_app, method_name) - ) - assert through_the_class == through_the_function + expected_call = EXPECTED_WORKER_CALLS[method_name] + assert through_the_class["result"] == expected_call["result"] + assert through_the_class["modal_call_id"] == expected_call["modal_call_id"] + assert through_the_class["required_country"] == expected_call["required_country"] assert through_the_class["shutdown_calls"] == 1 assert len(through_the_class["operations"]) == 1 (operation_keywords,) = through_the_class["operation_keywords"] @@ -341,8 +363,6 @@ def test_a_v2_worker_class_method_runs_the_same_code_as_its_plain_function( ) if method_name == SEGMENT_WORKER_METHOD: assert operation_keywords["attributes"]["database_row_id"] == SIMULATION_RUN_ID - if method_name != SEGMENT_WORKER_METHOD: - assert through_the_class["modal_call_id"] == "fc-worker-call" @pytest.mark.parametrize( diff --git a/projects/policyengine-simulation-executor/tests/test_v2_worker_interrupted_inputs.py b/projects/policyengine-simulation-executor/tests/test_v2_worker_interrupted_inputs.py index 52212865e..77f6ba21f 100644 --- a/projects/policyengine-simulation-executor/tests/test_v2_worker_interrupted_inputs.py +++ b/projects/policyengine-simulation-executor/tests/test_v2_worker_interrupted_inputs.py @@ -36,17 +36,6 @@ "run_single_simulation_uk", "run_single_simulation_segment_us", ) -# Each worker runs as a method of its Modal class, and as a plain function of -# the same name for callers on code from before the classes. -WORKER_ENTRY_POINTS = [ - pytest.param( - method_name, - called_through, - id=f"{method_name}-{called_through.replace(' ', '-')}", - ) - for method_name in WORKER_METHOD_NAMES - for called_through in ("class method", "plain function") -] class InputCancelledForTest(BaseException): @@ -80,9 +69,8 @@ def _load_v2_app(monkeypatch): return importlib.import_module("src.modal.v2_app") -def _worker(v2_app, method_name: str, called_through: str) -> Callable[..., dict]: - if called_through == "plain function": - return getattr(v2_app, method_name) +def _worker(v2_app, method_name: str) -> Callable[..., dict]: + """Return the method of the worker's Modal class that serves its calls.""" class_name = worker_class_name(method_name) assert class_name is not None, method_name return getattr(getattr(v2_app, class_name)(), method_name) @@ -144,7 +132,7 @@ def calculation() -> dict: return calculation -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) @pytest.mark.parametrize( "interruption", [ @@ -155,7 +143,7 @@ def calculation() -> dict: ], ) def test_an_interrupted_input_stops_its_container_fetching_inputs( - monkeypatch, method_name, called_through, interruption + monkeypatch, method_name, interruption ) -> None: v2_app = _load_v2_app(monkeypatch) input_fetching = v2_app.modal.experimental.input_fetching @@ -164,7 +152,7 @@ def test_an_interrupted_input_stops_its_container_fetching_inputs( _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, called_through), + _worker(v2_app, method_name), _calculation_raising(interruption), ) @@ -174,9 +162,9 @@ def test_an_interrupted_input_stops_its_container_fetching_inputs( assert input_fetching.stop_requests == 1 -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_an_input_failing_with_an_error_keeps_its_container_fetching_inputs( - monkeypatch, method_name, called_through + monkeypatch, method_name ) -> None: v2_app = _load_v2_app(monkeypatch) input_fetching = v2_app.modal.experimental.input_fetching @@ -186,7 +174,7 @@ def test_an_input_failing_with_an_error_keeps_its_container_fetching_inputs( _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, called_through), + _worker(v2_app, method_name), _calculation_raising(error), ) @@ -194,9 +182,9 @@ def test_an_input_failing_with_an_error_keeps_its_container_fetching_inputs( assert input_fetching.stop_requests == 0 -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_a_successful_input_keeps_its_container_fetching_inputs( - monkeypatch, method_name, called_through + monkeypatch, method_name ) -> None: v2_app = _load_v2_app(monkeypatch) input_fetching = v2_app.modal.experimental.input_fetching @@ -204,7 +192,7 @@ def test_a_successful_input_keeps_its_container_fetching_inputs( result, runtime = _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, called_through), + _worker(v2_app, method_name), lambda: {"calculated": True}, ) @@ -213,9 +201,9 @@ def test_a_successful_input_keeps_its_container_fetching_inputs( assert input_fetching.stop_requests == 0 -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_a_preempted_input_keeps_its_container_fetching_inputs( - monkeypatch, method_name, called_through + monkeypatch, method_name ) -> None: # Modal preempts a container by raising KeyboardInterrupt in the running # input, shuts the container down, and runs the input again in another. @@ -227,7 +215,7 @@ def test_a_preempted_input_keeps_its_container_fetching_inputs( _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, called_through), + _worker(v2_app, method_name), _calculation_raising(preemption), ) @@ -249,7 +237,7 @@ def test_a_failed_stop_request_still_ends_the_input_in_its_cancellation( _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, "class method"), + _worker(v2_app, method_name), _calculation_raising(cancellation), ) @@ -268,9 +256,9 @@ def test_a_failed_stop_request_still_ends_the_input_in_its_cancellation( assert warning.exc_info[1] is input_fetching.stop_error -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_an_input_interrupted_while_its_telemetry_starts_stops_its_container( - monkeypatch, method_name, called_through + monkeypatch, method_name ) -> None: # The first input of a container restored from the memory snapshot # imports the telemetry libraries while it sets up its call's @@ -285,15 +273,15 @@ def start_telemetry(**_options) -> RecordingRuntime: monkeypatch.setattr(v2_app, "init_process_observability", start_telemetry) with pytest.raises(InputCancelledForTest) as raised: - _call_worker(_worker(v2_app, method_name, called_through)) + _call_worker(_worker(v2_app, method_name)) assert raised.value is cancellation assert input_fetching.stop_requests == 1 -@pytest.mark.parametrize(("method_name", "called_through"), WORKER_ENTRY_POINTS) +@pytest.mark.parametrize("method_name", WORKER_METHOD_NAMES) def test_an_input_interrupted_while_its_telemetry_shuts_down_stops_its_container( - monkeypatch, method_name, called_through + monkeypatch, method_name ) -> None: v2_app = _load_v2_app(monkeypatch) input_fetching = v2_app.modal.experimental.input_fetching @@ -308,7 +296,7 @@ def shutdown(self) -> None: _run_one_input( monkeypatch, v2_app, - _worker(v2_app, method_name, called_through), + _worker(v2_app, method_name), lambda: {"calculated": True}, runtime=RuntimeCancelledWhileShuttingDown(), )