Skip to content

test: Deflake throttled-request deferral tests with a stopped throttler clock - #2275

Merged
vdusek merged 1 commit into
masterfrom
test/deflake-static-throttled-deferral
Oct 5, 2026
Merged

vdusek merged 1 commit into
masterfrom
test/deflake-static-throttled-deferral

Conversation

@vdusek

@vdusek vdusek commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

test_static_handler_raised_throttled_error_is_deferred failed on macOS (run). The test put the domain into a 50 ms backoff and raised RequestThrottledError. The crawler defers the request only if is_throttled() still holds when it checks, and on a loaded runner the backoff ran out first. The request then went down the ordinary-error path and fell back to the browser.

A new helper, open_throttler_held_until_reclaim() in tests/unit/utils.py, opens the throttler on a stopped Mock clock with a 1-minute backoff. The domain is always still held back when the crawler checks. When the crawler reclaims the deferred request, the clock moves past the backoff, so the retry is dispatched right away.

The same race also failed test_handler_raised_throttled_error_is_deferred in the basic crawler suite (run, on #2274).

Six deferral tests in the basic, HTTP, Playwright and adaptive crawler suites now use the helper. Their assertions are unchanged, and all of them fail when _is_held_back is forced to False. The stall tests still use the 50 ms backoff, since they need real time to pass through max_domain_stall.

Closes #2271

✍️ Drafted by Claude Code

@vdusek vdusek added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels Oct 5, 2026
@vdusek vdusek self-assigned this Oct 5, 2026
@vdusek vdusek added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels Oct 5, 2026
@codecov

codecov Bot commented Oct 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.91%. Comparing base (87561ee) to head (e2ad405).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2275   +/-   ##
=======================================
  Coverage   93.90%   93.91%           
=======================================
  Files         182      182           
  Lines       13144    13144           
=======================================
+ Hits        12343    12344    +1     
+ Misses        801      800    -1     
Flag Coverage Δ
unit 93.91% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek vdusek removed the adhoc Ad-hoc unplanned task added during the sprint. label Oct 5, 2026
@vdusek
vdusek requested a review from Mantisus October 5, 2026 14:54
@vdusek
vdusek marked this pull request as ready for review October 5, 2026 14:54
@apify-service-account apify-service-account added the tested Temporary label used only programatically for some analytics. label Oct 5, 2026

@Mantisus Mantisus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vdusek
vdusek requested a review from Mantisus October 5, 2026 16:57
@vdusek

vdusek commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

@Mantisus could you please try to approve it again?

@apify-approval-mirror apify-approval-mirror Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mirroring approval from @Mantisus.

@vdusek

vdusek commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

amazing, thanks @B4nan

@vdusek
vdusek merged commit d22ef78 into master Oct 5, 2026
40 of 42 checks passed
@vdusek
vdusek deleted the test/deflake-static-throttled-deferral branch October 5, 2026 16:59
vdusek added a commit that referenced this pull request Oct 15, 2026
…clock (#2331)

`test_persistent_429_raises_after_stall[retry_on_blocked]` failed on
ubuntu / Python 3.10
([run](https://github.com/apify/crawlee-python/actions/runs/38694178175/job/116286777865))
with `DID NOT RAISE PersistentRateLimitError`. The other 17 legs of the
same run passed.

The test put the domain into a 50 to 100 ms backoff and let real time
pass through a 1.2 s `max_domain_stall`. The crawler defers a 429 only
if `is_throttled()` still holds when it handles the error, and on a
loaded runner the backoff ran out first. With `max_request_retries=0`
the request then took the ordinary-error path, got marked as handled,
and the run ended without an error. A 120 ms delay injected into that
check reproduces the failure on every run.

The throttler now runs on a stopped `Mock` clock with a 1-minute
backoff, as in #2275. The clock moves one backoff forward each time the
idle crawler asks for a stall reason, and `max_domain_stall` sits
between one and two backoffs, so the domain stalls on the check after
its second 429. The assertions are unchanged, and the test still fails
when `_is_held_back` is forced to `False`. It also finishes in well
under a second.

The stall tests in the basic crawler suite keep the 50 ms backoff. They
run with the default three retries, so a lost race there only spends one
of the retries.

*✍️ Drafted by Claude Code*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test_static_handler_raised_throttled_error_is_deferred

3 participants