Lee-W commented on code in PR #76827:
URL: https://github.com/apache/airflow/pull/76827#discussion_r4637935591
##########
airflow-core/tests/unit/models/test_taskinstance.py:
##########
@@ -4513,6 +4509,50 @@ def
test_register_asset_changes_in_db_no_outlets_is_a_noop(dag_maker, session):
)
[email protected]("postgres", "mysql")
+def
test_register_asset_changes_in_db_alias_asset_created_concurrently(dag_maker,
session):
+ """Another writer creates the alias-emitted asset after our lookup missed
it."""
+ alias = AssetAlias("race_alias")
+ with dag_maker(dag_id="alias_race", schedule=None, session=session):
+ EmptyOperator(task_id="hi", outlets=[alias])
+ dr = dag_maker.create_dagrun(session=session)
+ [ti] = dr.get_task_instances(session=session)
+ session.commit()
+
+ real_register = asset_manager.register_asset_change
+ calls = 0
+
+ def _lookup_misses_once(**kwargs):
Review Comment:
```suggestion
def _register_after_asset_appears(**kwargs):
```
##########
airflow-core/tests/unit/models/test_taskinstance.py:
##########
@@ -4513,6 +4509,50 @@ def
test_register_asset_changes_in_db_no_outlets_is_a_noop(dag_maker, session):
)
[email protected]("postgres", "mysql")
+def
test_register_asset_changes_in_db_alias_asset_created_concurrently(dag_maker,
session):
+ """Another writer creates the alias-emitted asset after our lookup missed
it."""
+ alias = AssetAlias("race_alias")
+ with dag_maker(dag_id="alias_race", schedule=None, session=session):
+ EmptyOperator(task_id="hi", outlets=[alias])
+ dr = dag_maker.create_dagrun(session=session)
+ [ti] = dr.get_task_instances(session=session)
+ session.commit()
+
+ real_register = asset_manager.register_asset_change
+ calls = 0
+
+ def _lookup_misses_once(**kwargs):
+ nonlocal calls
+ calls += 1
+ if calls == 1:
+ with create_session() as other:
+ other.add(AssetModel(name="race_asset",
uri="s3://bucket/race_asset", group="asset"))
+ return None
+ return real_register(**kwargs)
+
+ with mock.patch.object(asset_manager, "register_asset_change",
side_effect=_lookup_misses_once):
Review Comment:
```suggestion
with mock.patch.object(asset_manager, "register_asset_change",
side_effect=_register_after_asset_appears, autospec=True):
```
##########
airflow-core/tests/unit/assets/test_manager.py:
##########
@@ -188,6 +189,74 @@ def test_register_asset_change_with_alias(
)
assert
session.scalar(select(func.count()).select_from(AssetDagRunQueue)) == 2
+ @pytest.mark.backend("postgres", "mysql")
+ @pytest.mark.usefixtures("testing_dag_bundle")
+ def test_add_asset_alias_association_tolerates_concurrent_link(self,
session):
+ """Another writer links the same alias to the asset after we read its
aliases."""
+ session.add(AssetModel(uri="test://race/", name="race_asset",
group="asset"))
+ session.commit()
+
+ def _load(s):
+ return s.scalar(
+ select(AssetModel)
+ .where(AssetModel.name == "race_asset")
+ .options(joinedload(AssetModel.aliases))
+ )
+
+ stale = _load(session)
+ assert stale.aliases == []
+
+ if TYPE_CHECKING:
+ assert settings.Session
+ assert settings.Session.session_factory
+ other = settings.Session.session_factory()
+ try:
+ AssetManager._add_asset_alias_association(["race_alias"],
_load(other), session=other)
+ other.commit()
+ finally:
+ other.close()
+
+ AssetManager._add_asset_alias_association(["race_alias"], stale,
session=session)
+ session.flush()
+
+ assert [a.name for a in stale.aliases] == ["race_alias"]
+ assert
session.scalar(select(func.count()).select_from(alias_association_table)) == 1
+
+ @pytest.mark.backend("postgres", "mysql")
+ @pytest.mark.usefixtures("testing_dag_bundle")
+ def
test_add_asset_alias_association_tolerates_concurrent_alias_create(self,
session):
+ """Another writer creates the same alias after our lookup missed it."""
+ asset = AssetModel(uri="test://race/", name="race_asset",
group="asset")
+ session.add(asset)
+ session.commit()
+
+ if TYPE_CHECKING:
+ assert settings.Session
+ assert settings.Session.session_factory
+ real_scalars = session.scalars
+ calls = 0
+
+ def _lookup_then_create_alias(*args, **kwargs):
+ nonlocal calls
+ calls += 1
+ result = list(real_scalars(*args, **kwargs))
+ if calls == 1:
+ other = settings.Session.session_factory()
+ try:
+ other.add(AssetAliasModel(name="race_alias"))
+ other.commit()
+ finally:
+ other.close()
+ return result
+
+ with mock.patch.object(session, "scalars",
side_effect=_lookup_then_create_alias):
Review Comment:
```suggestion
with mock.patch.object(session, "scalars",
side_effect=_lookup_then_create_alias, autospec=True):
```
--
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]