Lee-W commented on code in PR #66160:
URL: https://github.com/apache/airflow/pull/66160#discussion_r3199406766
##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -4868,3 +4887,218 @@ def test_dag_add_result(create_runtime_ti,
mock_supervisor_comms):
dag_result=True,
)
)
+
+
+class TestTaskInstanceStateOperations:
+ """Tests to verify that tasks can perform state operations (task / asset)
via the supervisor."""
+
+ def test_task_can_set_and_get_state(self, create_runtime_ti,
mock_supervisor_comms):
+ class MyOperator(BaseOperator):
+ def execute(self, context):
+ ts = context["task_state"]
+ ts.set("job_id", "spark_app_001")
+ return ts.get("job_id")
+
+ task = MyOperator(task_id="t")
+ runtime_ti = create_runtime_ti(task=task)
+
+ run(runtime_ti, context=runtime_ti.get_template_context(),
log=mock.MagicMock())
+
+ mock_supervisor_comms.send.assert_any_call(
+ SetTaskState(ti_id=runtime_ti.id, key="job_id",
value="spark_app_001")
+ )
+
mock_supervisor_comms.send.assert_any_call(GetTaskState(ti_id=runtime_ti.id,
key="job_id"))
+
+ def test_task_can_delete_state(self, create_runtime_ti,
mock_supervisor_comms):
+ class MyOperator(BaseOperator):
+ def execute(self, context):
+ context["task_state"].delete("job_id")
+
+ task = MyOperator(task_id="t")
+ runtime_ti = create_runtime_ti(task=task)
+
+ run(runtime_ti, context=runtime_ti.get_template_context(),
log=mock.MagicMock())
+
+
mock_supervisor_comms.send.assert_any_call(DeleteTaskState(ti_id=runtime_ti.id,
key="job_id"))
+
+ @pytest.mark.parametrize(
+ ("call_kwargs", "expected_flag"),
+ [
+ pytest.param({}, False, id="default"),
+ pytest.param({"all_map_indices": True}, True, id="fleet-wipe"),
+ ],
+ )
+ def test_task_can_clear_state(self, call_kwargs, expected_flag,
create_runtime_ti, mock_supervisor_comms):
+ class MyOperator(BaseOperator):
+ def execute(self, context):
+ context["task_state"].clear(**call_kwargs)
+
+ task = MyOperator(task_id="t")
+ runtime_ti = create_runtime_ti(task=task)
+ run(runtime_ti, context=runtime_ti.get_template_context(),
log=mock.MagicMock())
+ mock_supervisor_comms.send.assert_any_call(
+ ClearTaskState(ti_id=runtime_ti.id, all_map_indices=expected_flag)
+ )
+
+ @staticmethod
+ def _watcher_side_effect(msg=None, *args, **kwargs):
+ actual = msg or (args[0] if args else None)
+ if isinstance(actual, ValidateInletsAndOutlets):
+ return InactiveAssetsResult(inactive_assets=[])
+ if isinstance(actual, GetAssetByUri):
+ return AssetResult(name=actual.uri, uri=actual.uri, group="asset")
Review Comment:
ah ok, yep, I misunderstood what `actaul` is...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]