pierrejeambrun commented on code in PR #48239:
URL: https://github.com/apache/airflow/pull/48239#discussion_r2014751344


##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -589,3 +594,97 @@ def test_failed_trigger(session, dag_maker, 
supervisor_builder):
     assert task_instance.next_method == "__fail__"
     assert task_instance.next_kwargs["error"] == "Trigger failure"
     assert task_instance.next_kwargs["traceback"][-1] == "ModuleNotFoundError: 
No module named 'fake'\n"
+
+
+class CustomTrigger(BaseTrigger):
+    """Custom Trigger that will access one Variable and one Connection."""
+
+    async def run(self) -> AsyncIterator[TriggerEvent]:
+        import attrs
+
+        from airflow.sdk import Variable
+
+        conn = await sync_to_async(BaseHook.get_connection)("test_connection")
+
+        self.log.info("Loaded conn %s", conn.conn_id)
+
+        variable = await sync_to_async(Variable.get)("test_variable")
+
+        self.log.info("Loaded variable %s", variable)
+
+        yield TriggerEvent({"connection": attrs.asdict(conn), "variable": 
variable})
+
+    def serialize(self) -> tuple[str, dict[str, Any]]:
+        return (
+            f"{type(self).__module__}.{type(self).__qualname__}",
+            {},
+        )
+
+
+class TestTriggerRunnerSupervisor(TriggerRunnerSupervisor):
+    """
+    Make sure that the Supervisor stops after handling the events and do not 
keep running forever so the
+    test can continue.
+    """
+
+    def handle_events(self):
+        self.stop = bool(self.events)
+        super().handle_events()
+
+
[email protected]
+# @patch("airflow.providers.standard.triggers.temporal.CustomTrigger", 
return_value=CustomTrigger)
+async def test_trigger_can_access_variables_and_connections(session, 
dag_maker, supervisor_builder):
+    """
+    Checks that the trigger will successfully access Variables and Connections.
+
+    This is the Supervisor side of the error reported in 
TestTriggerRunner::test_invalid_trigger
+    """
+    # Create a Trigger
+    trigger = CustomTrigger()
+    trigger_orm = Trigger(classpath=trigger.serialize()[0], kwargs={})
+    trigger_orm.id = 1
+    session.add(trigger_orm)
+    session.commit()
+
+    # Create the appropriate Connection and Variable
+    connection = Connection(conn_id="test_connection", conn_type="http")
+    variable = Variable(key="test_variable", val="some_value")
+    session.add(connection)
+    session.add(variable)
+
+    # Create the test DAG and task
+    with dag_maker(dag_id="trigger_accessing_variable_and_connection", 
session=session):
+        EmptyOperator(task_id="dummy1")
+    dr = dag_maker.create_dagrun()
+    task_instance = dr.task_instances[0]
+    # Make a task instance based on that and tie it to the trigger
+    task_instance.state = TaskInstanceState.DEFERRED
+    task_instance.trigger_id = trigger_orm.id
+
+    job = Job()
+    session.add(job)
+    session.commit()
+
+    supervisor = TestTriggerRunnerSupervisor.start(job=job, capacity=1, 
logger=None)

Review Comment:
   ```suggestion
       supervisor = DummyTriggerRunnerSupervisor.start(job=job, capacity=1, 
logger=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