test: Fix flaky test_autoscales by replacing fixed sleeps with poll-based assertions#1835
Open
Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Open
test: Fix flaky test_autoscales by replacing fixed sleeps with poll-based assertions#1835Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Conversation
…ased assertions Replace time-dependent assertions with a polling helper that waits for conditions to become true within a generous timeout. This eliminates flakiness caused by event loop scheduling jitter on Windows and macOS, where the autoscaler may not complete enough cycles within the original hardcoded sleep intervals. Changes: - Add _wait_for() helper that polls a condition with configurable timeout - Replace all asyncio.sleep() + assert patterns with _wait_for() calls - Use an explicit flag for CPU overload instead of wall-clock comparison - Remove @pytest.mark.flaky decorator since the test is now deterministic - Remove unused datetime/timezone imports Fixes apify#1655
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1655 —
test_autoscalesis flaky on Windows and macOS.The root cause is that the test relies on fixed
asyncio.sleep()durations to assert autoscaling behavior, but event loop scheduling jitter on Windows/macOS can cause the autoscaler to not complete enough cycles within the expected time window (e.g.,desired_concurrencyonly reaches 3 instead of 4).Changes:
_wait_for()helper that polls a condition until it becomes true (with configurable timeout)asyncio.sleep() + assertpatterns with_wait_for()callsoverload_activeflag instead of wall-clockdatetimecomparison for the CPU overload simulation@pytest.mark.flakydecorator since the test is now deterministicdatetime/timezoneimportsBefore (original test — flaky on Windows/macOS)
The original test uses fixed sleep durations which are too tight for slower event loop schedulers:
After (fixed test — 5/5 passes, all 8 tests pass)
Stability verification (5 consecutive runs):
Test plan
test_autoscalespasses consistently (5/5 runs)test_autoscaled_pool.pypass with no regressionsruff checkpasses