ashb commented on a change in pull request #20443:
URL: https://github.com/apache/airflow/pull/20443#discussion_r782855132
##########
File path: tests/listeners/test_empty_listener.py
##########
@@ -0,0 +1,24 @@
+#
Review comment:
Let's change the name of these files that don't contain tests to just
`empty_listener` etc -- the `test_` prefix is confusing here as they don't
contain any tests themselves, but are used by them.
##########
File path: tests/listeners/test_listeners.py
##########
@@ -0,0 +1,108 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import pytest as pytest
+
+from airflow import AirflowException
+from airflow.listeners import hookimpl
+from airflow.listeners.events import register_task_instance_state_events
+from airflow.listeners.listener import get_listener_manager
+from airflow.operators.bash import BashOperator
+from airflow.utils import timezone
+from airflow.utils.session import provide_session
+from airflow.utils.state import State
+from tests.listeners import test_full_listener, test_partial_listener,
test_throwing_listener
+
+DAG_ID = "test_listener_dag"
+TASK_ID = "test_listener_task"
+EXECUTION_DATE = timezone.utcnow()
+
+
[email protected](scope="module", autouse=True)
+def register_events():
+ register_task_instance_state_events()
Review comment:
Do we need to remove this setting/config when the test finishes?
##########
File path: tests/listeners/test_listeners.py
##########
@@ -0,0 +1,108 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import pytest as pytest
+
+from airflow import AirflowException
+from airflow.listeners import hookimpl
+from airflow.listeners.events import register_task_instance_state_events
+from airflow.listeners.listener import get_listener_manager
+from airflow.operators.bash import BashOperator
+from airflow.utils import timezone
+from airflow.utils.session import provide_session
+from airflow.utils.state import State
+from tests.listeners import test_full_listener, test_partial_listener,
test_throwing_listener
+
+DAG_ID = "test_listener_dag"
+TASK_ID = "test_listener_task"
+EXECUTION_DATE = timezone.utcnow()
+
+
[email protected](scope="module", autouse=True)
+def register_events():
+ register_task_instance_state_events()
+
+
[email protected](autouse=True)
+def clean_listener_manager():
+ lm = get_listener_manager()
+ lm.clear()
+ yield
+ lm = get_listener_manager()
+ lm.clear()
+ test_full_listener.state = []
+
+
+@provide_session
+def test_listener_gets_calls(create_task_instance, session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_full_listener)
+
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti.run()
Review comment:
Is this one using `ti.run()` and not `ti._run_raw_task()` for a reason?
If so please add a short comment
##########
File path: airflow/plugins_manager.py
##########
@@ -458,6 +463,20 @@ def integrate_macros_plugins() -> None:
setattr(macros, plugin.name, macros_module)
+def integrate_listener_plugins(listener_manager: "ListenerManager") -> None:
+ global plugins
+ global macros_modules
Review comment:
```suggestion
```
Not used?
##########
File path: tests/listeners/test_listeners.py
##########
@@ -0,0 +1,108 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import pytest as pytest
+
+from airflow import AirflowException
+from airflow.listeners import hookimpl
+from airflow.listeners.events import register_task_instance_state_events
+from airflow.listeners.listener import get_listener_manager
+from airflow.operators.bash import BashOperator
+from airflow.utils import timezone
+from airflow.utils.session import provide_session
+from airflow.utils.state import State
+from tests.listeners import test_full_listener, test_partial_listener,
test_throwing_listener
+
+DAG_ID = "test_listener_dag"
+TASK_ID = "test_listener_task"
+EXECUTION_DATE = timezone.utcnow()
+
+
[email protected](scope="module", autouse=True)
+def register_events():
+ register_task_instance_state_events()
+
+
[email protected](autouse=True)
+def clean_listener_manager():
+ lm = get_listener_manager()
+ lm.clear()
+ yield
+ lm = get_listener_manager()
+ lm.clear()
+ test_full_listener.state = []
+
+
+@provide_session
+def test_listener_gets_calls(create_task_instance, session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_full_listener)
+
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti.run()
+
+ assert len(test_full_listener.state) == 2
+ assert test_full_listener.state == [State.RUNNING, State.SUCCESS]
+
+
+@provide_session
+def test_listener_gets_only_subscribed_calls(create_task_instance,
session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_partial_listener)
+
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti.run()
+
+ assert len(test_partial_listener.state) == 1
+ assert test_partial_listener.state == [State.RUNNING]
+
+
+@provide_session
+def test_listener_throws_exceptions(create_task_instance, session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_throwing_listener)
+
+ with pytest.raises(RuntimeError):
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti._run_raw_task()
+
+
+@provide_session
+def
test_listener_captures_failed_taskinstances(create_task_instance_of_operator,
session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_full_listener)
+
+ with pytest.raises(AirflowException):
+ ti = create_task_instance_of_operator(
+ BashOperator, dag_id=DAG_ID, execution_date=EXECUTION_DATE,
task_id=TASK_ID, bash_command="exit 1"
+ )
+ ti._run_raw_task()
+
+ assert test_full_listener.state == [State.FAILED]
+ assert len(test_full_listener.state) == 1
+
+
+def test_non_module_listener_is_not_registered():
+ class NotAListener:
+ @hookimpl
+ def on_task_instance_running(self, previous_state, task_instance,
session):
+ pass
+
+ lm = get_listener_manager()
+ lm.add_listener(NotAListener())
Review comment:
Shouldn't this throw a TypeError instead?
##########
File path: tests/listeners/test_listeners.py
##########
@@ -0,0 +1,108 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import pytest as pytest
+
+from airflow import AirflowException
+from airflow.listeners import hookimpl
+from airflow.listeners.events import register_task_instance_state_events
+from airflow.listeners.listener import get_listener_manager
+from airflow.operators.bash import BashOperator
+from airflow.utils import timezone
+from airflow.utils.session import provide_session
+from airflow.utils.state import State
+from tests.listeners import test_full_listener, test_partial_listener,
test_throwing_listener
+
+DAG_ID = "test_listener_dag"
+TASK_ID = "test_listener_task"
+EXECUTION_DATE = timezone.utcnow()
+
+
[email protected](scope="module", autouse=True)
+def register_events():
+ register_task_instance_state_events()
+
+
[email protected](autouse=True)
+def clean_listener_manager():
+ lm = get_listener_manager()
+ lm.clear()
+ yield
+ lm = get_listener_manager()
+ lm.clear()
+ test_full_listener.state = []
+
+
+@provide_session
+def test_listener_gets_calls(create_task_instance, session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_full_listener)
+
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti.run()
+
+ assert len(test_full_listener.state) == 2
+ assert test_full_listener.state == [State.RUNNING, State.SUCCESS]
+
+
+@provide_session
+def test_listener_gets_only_subscribed_calls(create_task_instance,
session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_partial_listener)
+
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti.run()
+
+ assert len(test_partial_listener.state) == 1
+ assert test_partial_listener.state == [State.RUNNING]
+
+
+@provide_session
+def test_listener_throws_exceptions(create_task_instance, session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_throwing_listener)
+
+ with pytest.raises(RuntimeError):
+ ti = create_task_instance(session=session, state=State.QUEUED)
+ ti._run_raw_task()
+
+
+@provide_session
+def
test_listener_captures_failed_taskinstances(create_task_instance_of_operator,
session=None):
+ lm = get_listener_manager()
+ lm.add_listener(test_full_listener)
+
+ with pytest.raises(AirflowException):
+ ti = create_task_instance_of_operator(
+ BashOperator, dag_id=DAG_ID, execution_date=EXECUTION_DATE,
task_id=TASK_ID, bash_command="exit 1"
+ )
+ ti._run_raw_task()
Review comment:
Please scope the `raises` tighter (in a few places)
```suggestion
ti = create_task_instance_of_operator(
BashOperator, dag_id=DAG_ID, execution_date=EXECUTION_DATE,
task_id=TASK_ID, bash_command="exit 1"
)
with pytest.raises(AirflowException):
ti._run_raw_task()
```
--
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]