Skip to content

test: consolidate polling into a single shared helper and deflake tests#920

Open
vdusek wants to merge 4 commits into
masterfrom
test/unify-polling-helpers
Open

test: consolidate polling into a single shared helper and deflake tests#920
vdusek wants to merge 4 commits into
masterfrom
test/unify-polling-helpers

Conversation

@vdusek
Copy link
Copy Markdown
Contributor

@vdusek vdusek commented May 29, 2026

Summary

Started as a generalization of the two integration polling helpers, and grew into the SDK counterpart of apify/apify-client-python#844: a single shared polling helper used across the whole test suite.

Changes:

  • Add a shared tests/_utils.py with a single poll_until_condition helper — identical to the one in apify-client-python (chore(deps): update codecov/codecov-action action to v6 #844): polls a sync-or-async callable at a constant interval until a condition holds or a wall-clock timeout expires, with an optional backoff_factor that multiplies the interval after each poll. This subsumes the former call_with_exp_backoff (backoff_factor=2) and the "call once" single-RQ-mode case (timeout=0). The duplicated generate_unique_resource_name (integration + e2e copies) moves there too; tests/integration/_utils.py and tests/e2e/_utils.py are removed.
  • Migrate all ~50 call_with_exp_backoff call sites in test_request_queue.py to poll_until_condition (timeout=30, backoff_factor=2 for shared mode ≈ the former exponential backoff; timeout=0 for single mode). Pre-existing poll_until_condition call sites get their former defaults (timeout=60, poll_interval=5) passed explicitly.
  • Fix the flaky test_actor_adds_webhook_and_receives_event e2e test (failing CI run): the client Actor exited ~170 ms after registering the ad-hoc webhook, so the platform could process the run-succeeded event before the webhook propagated, leaving the server Actor blocked until its run timeout. The client now stays alive 5 s after add_webhook, and the unbounded INITIALIZED startup polling loop is replaced with poll_until_condition (timeout=300, growing interval). Replaces test: fix flaky webhook E2E test #930.
  • Replace the hand-rolled retry_counter loops in test_actor_charge.py (4×) with poll_until_condition.
  • Replace fixed sleeps in unit tests (test_actor_lifecycle, test_actor_key_value_store, test_apify_event_manager) with condition polling where the test waits for something to happen — faster on the happy path and robust under xdist load.

Fixed sleeps that are semantically required are intentionally left as-is: negative checks ("event must NOT fire"), mtime-granularity checks in test_apify_storages, simulated latency, and sleeps inside Actor main_func bodies (which are serialized and deployed to the platform, where the helper is not available).

Verification: lint, type-check, and the full unit suite pass; the three touched unit test files pass 10/10 repeated runs. Integration/e2e tests require a platform token and will be verified by CI.

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels May 29, 2026
@vdusek vdusek self-assigned this May 29, 2026
@github-actions github-actions Bot added this to the 141st sprint - Tooling team milestone May 29, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label May 29, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.94%. Comparing base (695500e) to head (fffc7eb).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #920   +/-   ##
=======================================
  Coverage   86.94%   86.94%           
=======================================
  Files          48       48           
  Lines        2942     2942           
=======================================
  Hits         2558     2558           
  Misses        384      384           
Flag Coverage Δ
e2e 37.55% <ø> (ø)
integration 58.90% <ø> (ø)
unit 75.69% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 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 marked this pull request as draft May 29, 2026 17:04
vdusek added 2 commits June 3, 2026 12:48
Make call_with_exp_backoff and poll_until_condition generic and consistent
so the same two helpers can be shared across apify-sdk-python,
crawlee-python and apify-client-python:

- call_with_exp_backoff no longer takes rq_access_mode. It now takes an
  optional condition (default: truthy) and retries fn until the condition
  holds, returning the last result. The Apify RQ single/shared distinction
  moves to the call sites via max_retries (0 = call once / single mode,
  5 = retry / shared mode).
- poll_until_condition gains the same condition default and now accepts
  both sync and async callables (so it can poll plain values too).
- Both helpers accept sync or async fn via a small _maybe_await helper,
  typed with overloads so the return type is preserved.

All ~36 call sites in test_request_queue.py are refactored accordingly.
Replace the two integration polling helpers with a single poll_until_condition
in a shared tests/_utils.py, matching the helper in apify-client-python (with
backoff_factor covering the exponential-backoff use case). Use it across
integration, e2e, and unit tests instead of hand-rolled retry loops and fixed
sleeps, and fix the flaky webhook e2e test by keeping the client run alive
briefly after add_webhook so the run-succeeded event cannot outrun the webhook
registration.
@vdusek vdusek force-pushed the test/unify-polling-helpers branch from 3052131 to fffc7eb Compare June 3, 2026 11:01
@vdusek vdusek changed the title test: generalize integration polling helpers test: consolidate polling into a single shared helper and deflake tests Jun 3, 2026
@vdusek vdusek marked this pull request as ready for review June 3, 2026 11:18
vdusek added 2 commits June 3, 2026 13:32
…run, bound webhook wait)

- Add an rq_poll_timeout fixture deriving the single/shared polling timeout
  from the request_queue_apify param, replacing the derivation block that was
  copy-pasted into all 20 request queue tests.
- Extract the byte-identical get_run closure in test_actor_charge.py into a
  module-level _get_run helper used via functools.partial.
- Bound the webhook server Actor's wait loop (5s server timeout, 300s
  deadline) so a webhook that never propagates fails the test quickly with an
  empty WEBHOOK_BODY and a clear message instead of blocking until the run
  timeout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. 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.

2 participants