Skip to content

[ServiceBus] Fix flaky test_backoff_fixed_retry by mocking time.sleep#46987

Merged
j7nw4r merged 1 commit into
mainfrom
fix/sb-backoff-test-flake
May 20, 2026
Merged

[ServiceBus] Fix flaky test_backoff_fixed_retry by mocking time.sleep#46987
j7nw4r merged 1 commit into
mainfrom
fix/sb-backoff-test-flake

Conversation

@j7nw4r
Copy link
Copy Markdown
Member

@j7nw4r j7nw4r commented May 19, 2026

What

Make ests/test_sb_client.py::TestServiceBusClient::test_backoff_fixed_retry deterministic by mocking ime.sleep instead of measuring wall-clock duration around real sleeps.

Why

The test previously called _backoff(retried_times=1) (which sleeps 0.8s in fixed mode) and asserted ime.time() - start_time < backoff * 2 (i.e. < 1.6s). On slow CI hosts, OS scheduling jitter can push the elapsed time over the budget. Observed in PR #46953 CI run (macos311 agent):

FAILED tests/test_sb_client.py::TestServiceBusClient::test_backoff_fixed_retry[pyamqp]
assert 1.6399269104003906 < (0.8 * (2 ** 1))

How

  • Patch azure.servicebus._base_handler.time.sleep and assert the value passed (== retry_backoff_factor for fixed mode).
  • Collapse the duplicated blocks for queue/topic/subscription sender+receiver and for "fixed"/RetryMode.Fixed into a single helper.
  • Net: -50/+26 lines, ~80x faster, no longer flaky.

The test previously measured wall-clock time around real time.sleep(0.8) calls and asserted the duration was strictly less than 1.6 seconds. On slow CI agents (notably macOS hosts) the elapsed time can exceed the budget by a few ms (e.g., 1.6399s observed in PR #46953), causing intermittent failures.

Mock time.sleep in azure.servicebus._base_handler and assert the value passed to sleep instead. This is deterministic and ~80x faster.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@j7nw4r j7nw4r marked this pull request as ready for review May 19, 2026 15:15
@j7nw4r j7nw4r requested a review from EldertGrootenboer as a code owner May 19, 2026 15:15
Copilot AI review requested due to automatic review settings May 19, 2026 15:15
@j7nw4r j7nw4r requested a review from skarri-microsoft as a code owner May 19, 2026 15:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes the Service Bus fixed retry backoff test deterministic by mocking time.sleep instead of measuring elapsed wall-clock time, reducing flakiness on slow CI agents.

Changes:

  • Replaces real sleep timing assertions with a mocked _base_handler.time.sleep assertion.
  • Consolidates duplicated fixed retry checks across string and enum retry modes.
  • Verifies queue/topic senders and receivers all use the fixed backoff value.

@j7nw4r j7nw4r merged commit 3b5edc6 into main May 20, 2026
26 checks passed
@j7nw4r j7nw4r deleted the fix/sb-backoff-test-flake branch May 20, 2026 16:08
@j7nw4r j7nw4r mentioned this pull request May 20, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants