kaxil commented on code in PR #77762:
URL: https://github.com/apache/airflow/pull/77762#discussion_r4740684438
##########
AGENTS.md:
##########
@@ -166,6 +166,7 @@ reported as such are described in "What is NOT considered a
security vulnerabili
- Use `spec`/`autospec` when mocking.
- Prefer `@mock.patch` decorators over `with mock.patch(...)` context managers
for patching. Use `conf_vars` (from `tests_common.test_utils.config`) for
Airflow config overrides — as a decorator when the value is fixed, as a context
manager when it varies via `@pytest.mark.parametrize` or is only known at run
time. In `async def` tests always use it as a context manager inside the body:
as a decorator it hides the coroutine from pytest-asyncio and the test fails.
- Use `time_machine` for time-dependent tests. Do not use `datetime.now()`
+- Do not assert on elapsed wall-clock time (`time.monotonic() - start < N`) or
release threads with `threading.Timer`: both flake on loaded CI runners.
Synchronise with `threading.Event` or `threading.Barrier` and assert order
instead, e.g. that an event is still unset when the call returns. Patch time
functions where the code under test looks them up
(`mock.patch("pkg.module.time.monotonic")`), never `time.monotonic` or
`time.sleep` globally, which also changes the clock for boto, moto and other
libraries.
Review Comment:
I don't think `mock.patch("pkg.module.time.monotonic")` is local. If the
module does `import time`, `pkg.module.time` is just the `time` module, so this
patches `time.monotonic` for everyone, same as the form it bans. I tried it on
a scratch module: `time.monotonic()` from outside returned the mock too, and in
an async test the event loop ate the `side_effect` values (StopIteration).
`mock.patch("pkg.module.time", autospec=True)` is what stays local
(`test_manager.py` does this), though it also mocks the module's other `time.*`
calls. Does the Copilot line need the same fix?
##########
.github/instructions/code-review.instructions.md:
##########
@@ -40,6 +40,7 @@ Use these rules when reviewing pull requests to the Apache
Airflow repository.
- **Flag any `unittest.TestCase` subclass.** Use pytest patterns instead.
- **Flag any `mock.Mock()` or `mock.MagicMock()` without `spec` or
`autospec`.** Unspec'd mocks silently accept any attribute access, hiding real
bugs.
- **Flag any `time.sleep` or `datetime.now()` in tests.** Use `time_machine`
for time-dependent tests.
+- **Flag elapsed-time assertions and `threading.Timer` in tests.** Checks like
`time.monotonic() - start < 1` and threads released by timers flake on loaded
CI runners. Use `threading.Event` and assert order instead. Also flag global
`time.*` patches such as `mock.patch("time.monotonic")`; patch the name in the
module under test.
Review Comment:
Would this flag the watchdog in `test_scheduler_job.py`
(`threading.Timer(10, other_session.rollback)`, cancelled in `finally` when the
test passes)? Same for `test_sbx.py`, which checks `time.monotonic() - start <
10` against a 30 s fake stall. There's no event to wait on there, and
`time_machine` doesn't touch `monotonic`. Could the rule (here and in
AGENTS.md) cover only timers the test races against, as the title says, so a
watchdog or a coarse bound isn't flagged? Fine as a follow-up.
--
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]