dabla commented on code in PR #75423:
URL: https://github.com/apache/airflow/pull/75423#discussion_r4676726568


##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -6195,3 +6204,136 @@ def test_caller_finished_ids_not_mutated(self):
         runner.process_trigger_events(finished_ids=finished_ids)
 
         assert finished_ids == [10]
+
+
[email protected]
[email protected]("testing_dag_bundle")
+async def test_start_from_trigger_task_records_its_rendered_fields(session, 
supervisor_builder, mocker):
+    """
+    A task that runs entirely in the triggerer still has its rendered fields 
recorded.
+
+    Driven through the supervisor and the serialized Dag, so the operator is 
the
+    ``SerializedBaseOperator`` production builds rather than an SDK one, and 
only the operator
+    fields carried in ``start_trigger_args.trigger_kwargs`` are recorded.
+    """
+
+    target_time = "{{ data_interval_end }}"
+    sensor = DateTimeSensorAsync(
+        task_id="test_ti",
+        target_time=target_time,
+        start_from_trigger=True,
+        end_from_trigger=True,
+    )
+    _, _, trigger_orm, ti = create_trigger_in_db(
+        session,
+        DateTimeTrigger(target_time=target_time, end_from_trigger=True),
+        operator=sensor,
+    )
+
+    supervisor = supervisor_builder()
+    runner = TriggerRunner()
+    runner.comms_decoder = InProcessTriggerComms(supervisor)
+    set_rtif = mocker.patch.object(
+        TaskInstanceOperations, "set_rtif", autospec=True, 
return_value=OKResponse(ok=True)
+    )
+
+    supervisor.load_triggers()
+    await runner.sync_state_to_supervisor(finished_ids=[])
+    await runner.create_triggers()
+    await asyncio.wait([runner.triggers[trigger_orm.id]["task"]], timeout=10)
+
+    # Asserted on the API call, not the send, so the TI the supervisor 
resolves is covered too.
+    set_rtif.assert_called_once_with(mocker.ANY, ti.id, {"target_time": 
"2023-01-01 00:00:00+00:00"})
+
+
[email protected]
[email protected]("testing_dag_bundle")
+async def test_a_rejected_rendered_fields_write_does_not_end_the_trigger(
+    session, supervisor_builder, mocker, cap_structlog
+):
+    """A write the supervisor rejects is logged, and the trigger still fires 
its event.
+
+    The task instance can be cleared between the trigger starting and the 
write landing, which
+    answers that write with a 410. The fields are only ever displayed, so 
losing them must not
+    cost the task its run.
+    """
+
+    target_time = "{{ data_interval_end }}"
+    sensor = DateTimeSensorAsync(
+        task_id="test_ti",
+        target_time=target_time,
+        start_from_trigger=True,
+        end_from_trigger=True,
+    )
+    _, _, trigger_orm, _ = create_trigger_in_db(
+        session,
+        DateTimeTrigger(target_time=target_time, end_from_trigger=True),
+        operator=sensor,
+    )
+
+    supervisor = supervisor_builder()
+    runner = TriggerRunner()
+    runner.comms_decoder = InProcessTriggerComms(supervisor)
+    mocker.patch.object(
+        TaskInstanceOperations,
+        "set_rtif",
+        autospec=True,
+        side_effect=RuntimeError("task instance is gone"),

Review Comment:
   **[nit]** This test catches the harness's own `assert`, not the error the 
real decoder raises.
   
   The `RuntimeError` from `set_rtif` is caught by `handle_requests`, which 
answers with an `ErrorResponse`. `InProcessTriggerComms.asend` then fails on 
its `assert response.error is None`, and that `AssertionError` is what 
`record_rendered_fields` logs. I printed the caught exception in the test to 
check: `AssertionError("{'error': 'API_SERVER_ERROR', ... 'exception_type': 
'RuntimeError'}")`. In production the decoder raises `AirflowRuntimeError` for 
the same response.
   
   The test still shows what it claims, since both are `Exception`s and the 
trigger fires its event. It would only stop matching production if someone 
narrowed the `except` later. Having the harness raise 
`AirflowRuntimeError(error=...)` on an error response, as `CommsDecoder` does, 
would close that gap. Not blocking.



##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -6195,3 +6204,136 @@ def test_caller_finished_ids_not_mutated(self):
         runner.process_trigger_events(finished_ids=finished_ids)
 
         assert finished_ids == [10]
+
+
[email protected]
[email protected]("testing_dag_bundle")
+async def test_start_from_trigger_task_records_its_rendered_fields(session, 
supervisor_builder, mocker):
+    """
+    A task that runs entirely in the triggerer still has its rendered fields 
recorded.
+
+    Driven through the supervisor and the serialized Dag, so the operator is 
the
+    ``SerializedBaseOperator`` production builds rather than an SDK one, and 
only the operator
+    fields carried in ``start_trigger_args.trigger_kwargs`` are recorded.
+    """
+
+    target_time = "{{ data_interval_end }}"
+    sensor = DateTimeSensorAsync(
+        task_id="test_ti",
+        target_time=target_time,
+        start_from_trigger=True,
+        end_from_trigger=True,
+    )
+    _, _, trigger_orm, ti = create_trigger_in_db(
+        session,
+        DateTimeTrigger(target_time=target_time, end_from_trigger=True),
+        operator=sensor,
+    )
+
+    supervisor = supervisor_builder()
+    runner = TriggerRunner()
+    runner.comms_decoder = InProcessTriggerComms(supervisor)
+    set_rtif = mocker.patch.object(
+        TaskInstanceOperations, "set_rtif", autospec=True, 
return_value=OKResponse(ok=True)
+    )
+
+    supervisor.load_triggers()
+    await runner.sync_state_to_supervisor(finished_ids=[])
+    await runner.create_triggers()
+    await asyncio.wait([runner.triggers[trigger_orm.id]["task"]], timeout=10)
+
+    # Asserted on the API call, not the send, so the TI the supervisor 
resolves is covered too.
+    set_rtif.assert_called_once_with(mocker.ANY, ti.id, {"target_time": 
"2023-01-01 00:00:00+00:00"})
+
+
[email protected]
[email protected]("testing_dag_bundle")
+async def test_a_rejected_rendered_fields_write_does_not_end_the_trigger(
+    session, supervisor_builder, mocker, cap_structlog
+):
+    """A write the supervisor rejects is logged, and the trigger still fires 
its event.
+
+    The task instance can be cleared between the trigger starting and the 
write landing, which
+    answers that write with a 410. The fields are only ever displayed, so 
losing them must not
+    cost the task its run.
+    """
+
+    target_time = "{{ data_interval_end }}"
+    sensor = DateTimeSensorAsync(
+        task_id="test_ti",
+        target_time=target_time,
+        start_from_trigger=True,
+        end_from_trigger=True,
+    )
+    _, _, trigger_orm, _ = create_trigger_in_db(
+        session,
+        DateTimeTrigger(target_time=target_time, end_from_trigger=True),
+        operator=sensor,
+    )
+
+    supervisor = supervisor_builder()
+    runner = TriggerRunner()
+    runner.comms_decoder = InProcessTriggerComms(supervisor)
+    mocker.patch.object(
+        TaskInstanceOperations,
+        "set_rtif",
+        autospec=True,
+        side_effect=RuntimeError("task instance is gone"),
+    )
+
+    supervisor.load_triggers()
+    await runner.sync_state_to_supervisor(finished_ids=[])
+    await runner.create_triggers()
+    await asyncio.wait([runner.triggers[trigger_orm.id]["task"]], timeout=10)
+
+    assert not runner.failed_triggers
+    assert [entry.trigger_id for entry in runner.events] == [trigger_orm.id]
+    assert "Failed to record rendered template fields" in cap_structlog
+
+
+def 
test_supervisor_drops_rendered_fields_sent_outside_a_trigger_request(jobless_supervisor,
 mocker):
+    """Fields that arrive with no task instance in scope are dropped, and the 
drop is logged."""
+    jobless_supervisor.client = mocker.MagicMock(spec=Client)
+    mocker.patch.object(TriggerRunnerSupervisor, "send_msg")

Review Comment:
   **[nit]** `send_msg` is patched without `autospec` here and in 
`test_supervisor_records_rendered_fields_for_the_handled_trigger`.
   
   The other tests in this file that patch `TriggerRunnerSupervisor.send_msg` 
use `autospec=True` (for example the ones around lines 4738 and 5647), and 
`AGENTS.md` asks for `spec`/`autospec` on mocks. With `autospec` the assertion 
takes the supervisor as its first argument: 
`send_msg.assert_called_once_with(jobless_supervisor, OKResponse(ok=True), 
request_id=7, error=None)`.



-- 
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]

Reply via email to