codeant-ai-for-open-source[bot] commented on code in PR #38576:
URL: https://github.com/apache/superset/pull/38576#discussion_r3477271444
##########
tests/unit_tests/utils/test_screenshot_cache_fix.py:
##########
@@ -27,13 +27,15 @@
import pytest
from pytest_mock import MockerFixture
+from superset.exceptions import AcquireDistributedLockFailedException
from superset.utils.screenshots import (
BaseScreenshot,
ScreenshotCachePayload,
StatusValues,
)
BASE_SCREENSHOT_PATH = "superset.utils.screenshots.BaseScreenshot"
+DISTRIBUTED_LOCK_PATH = "superset.distributed_lock.DistributedLock"
Review Comment:
**Suggestion:** The lock mock target is incorrect:
`BaseScreenshot.compute_and_cache` uses `DistributedLock` from the
`superset.utils.screenshots` module namespace, but this constant points to
`superset.distributed_lock.DistributedLock`. As a result, your patches do not
replace the object actually used at runtime, so tests may execute the real
distributed lock path and become flaky/fail unexpectedly. Patch
`superset.utils.screenshots.DistributedLock` instead. [api mismatch]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Thumbnail dedup tests invoke real distributed lock backend.
- ❌ `test_concurrent_task_skips_when_lock_already_held` does not mock lock.
- ⚠️ Flaky tests if Redis/DB coordination backend misconfigured.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Inspect the production implementation of
`BaseScreenshot.compute_and_cache` in
`superset/utils/screenshots.py:9-13` and `:124-131`, where `DistributedLock`
is imported
into the module namespace (`from superset.distributed_lock import
DistributedLock`) and
then used as `with DistributedLock(...):`, meaning
`superset.utils.screenshots.DistributedLock` is the symbol looked up at
runtime.
2. Inspect the test helper constant in
`tests/unit_tests/utils/test_screenshot_cache_fix.py:37-39`, where
`DISTRIBUTED_LOCK_PATH`
is defined as `"superset.distributed_lock.DistributedLock"`, and observe
that all tests
use `mocker.patch(DISTRIBUTED_LOCK_PATH)` (e.g. `_setup_mocks` at lines
79-82,
`test_cache_error_status_when_screenshot_fails` at lines 93-99,
`TestIntegrationCacheBugFix.test_failed_screenshot_does_not_pollute_cache`
at lines
91-100) to patch the lock.
3. Note that `mocker.patch("superset.distributed_lock.DistributedLock")`
only rebinds the
attribute in the `superset.distributed_lock` package, but
`superset.utils.screenshots` has
already imported `DistributedLock` into its own namespace; the
`DistributedLock` name used
in `compute_and_cache` (lines 124-131 of `superset/utils/screenshots.py`)
still refers to
the original context manager function, not the MagicMock created by the
tests, so the real
lock implementation is executed.
4. Run `pytest
tests/unit_tests/utils/test_screenshot_cache_fix.py::TestIntegrationCacheBugFix::test_concurrent_task_skips_when_lock_already_held`
and observe that `mock_lock = mocker.patch(DISTRIBUTED_LOCK_PATH)` and
`mock_lock.return_value.__enter__.side_effect =
AcquireDistributedLockFailedException("lock held")` at lines 159-167 never
affect
`BaseScreenshot.compute_and_cache`; instead the real `DistributedLock`
executes, so
`get_screenshot` at line 168 is called despite
`get_screenshot.assert_not_called()` at
line 173, or the test attempts to acquire a real distributed lock backend,
making the test
fail or become flaky depending on environment configuration.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3c8041df6d4e428d8f252ca6f3eabc4e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=3c8041df6d4e428d8f252ca6f3eabc4e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/utils/test_screenshot_cache_fix.py
**Line:** 38:38
**Comment:**
*Api Mismatch: The lock mock target is incorrect:
`BaseScreenshot.compute_and_cache` uses `DistributedLock` from the
`superset.utils.screenshots` module namespace, but this constant points to
`superset.distributed_lock.DistributedLock`. As a result, your patches do not
replace the object actually used at runtime, so tests may execute the real
distributed lock path and become flaky/fail unexpectedly. Patch
`superset.utils.screenshots.DistributedLock` instead.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38576&comment_hash=11da5aa067ae71be4e0555b7178ebc829d14fba621ffdd018f2e6ea497a8ff61&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38576&comment_hash=11da5aa067ae71be4e0555b7178ebc829d14fba621ffdd018f2e6ea497a8ff61&reaction=dislike'>👎</a>
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]