Skip to content

fix: stop jitter() returning 0 whenever pytest is imported - #348

Merged
SoulPancake merged 1 commit into
openfga:mainfrom
Hung1510:fix/jitter-under-pytest
Nov 28, 2026
Merged

SoulPancake merged 1 commit into
openfga:mainfrom
Hung1510:fix/jitter-under-pytest

Conversation

@Hung1510

@Hung1510 Hung1510 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

jitter() in openfga_sdk/oauth2_common.py returned 0 whenever pytest was in sys.modules, so token retry backoff depended on the test runner being imported, and the suite never exercised the real values.

  • Removed the pytest branch and the now unused import sys.
  • The oauth2 retry tests (async and sync) now patch asyncio.sleep / time.sleep, like test/api/open_fga_api_test.py, and assert the waits:
    • retries_raised_429_and_5xx_errors: 3 waits, inside [1, 2), [2, 4), [4, 8) ms for min_wait_in_ms=1
    • stops_retrying_after_max_retry: 2 waits
    • retries_5xx_responses: 3 waits
  • Added test_jitter_stays_in_range in test/backoff_test.py.

References

Closes #346

Testing

  • grep -rn pytest openfga_sdk/ prints nothing.
  • pytest test/oauth2_test.py test/sync/oauth2_test.py test/backoff_test.py: 40 passed in 2.7 s (4.9 s before; the slowest oauth2 test went from 1.3 s to 0.05 s).
  • Full suite: 600 passed, 12 skipped.
  • ruff check and ruff format are clean.

Summary by CodeRabbit

  • Bug Fixes
    • Retry delays now use exponential backoff with a randomized delay in the expected range, including during test runs.
    • Retry behavior is verified for temporary server errors and rate limits, including when the retry limit is reached.

@Hung1510
Hung1510 requested a review from a team as a code owner November 27, 2026 10:14
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Hung1510 / name: Pham Tran Gia Hung (fda313f)

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 085295d0-849f-44dc-977b-fc212402f95c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The change removes pytest detection from jitter, so it uses the calculated backoff range during pytest runs. Tests check jitter bounds and verify sleep delays or call counts in asynchronous and synchronous OAuth2 retry scenarios.

Changes

OAuth2 retry jitter

Layer / File(s) Summary
Jitter calculation and bounds test
openfga_sdk/oauth2_common.py, test/backoff_test.py
jitter returns the minimum delay when the calculated range is empty. Otherwise, it returns a random delay in the range. A test checks the lower-inclusive and upper-exclusive bounds.
OAuth2 retry delay checks
test/oauth2_test.py, test/sync/oauth2_test.py
Async and sync retry tests mock sleep and check delay ranges or sleep counts for raised errors, exhausted retries, and response-based retries.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

  • openfga/python-sdk#344: Both changes affect token retry wait values and related assertions in the async and sync OAuth2 tests.
  • openfga/python-sdk#331: That PR introduced the token retry loops that call jitter; this change removes pytest-specific behavior from that function and adds checks to the associated retry tests.

Suggested reviewers: soulpancake, rhamzeh

Merge Risk: Minimal · previous review fda31

The change makes OAuth2 token retry backoff behave the same whether or not pytest is imported, and it adds tests for the delays. No merge-blocking risk was found. The CLA authorization status still needs to be resolved.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: stop jitter() returning 0 whenever pytest is imported" matches the main change. The raw summary and objectives show that jitter() no longer checks sys.modules for pytest, so it no long…
Linked Issues check Passed Issue #346 has three coding requirements. (1) Remove the pytest-specific zero-jitter branch and unused import sys: met. The head version of openfga_sdk/oauth2_common.py has no sys import and no …
Out of Scope Changes check Passed All changes map to #346. The oauth2_common.py edit removes the pytest branch and the unused sys import. The oauth2 test edits add sleep patching and wait assertions to the retry tests named in the…
Full details: Title check

Explanation

The title "fix: stop jitter() returning 0 whenever pytest is imported" matches the main change. The raw summary and objectives show that jitter() no longer checks sys.modules for pytest, so it no longer forces zero delay during pytest runs. The title names the fixed behavior and the affected function. The related test updates (patching asyncio.sleep and time.sleep, plus test_jitter_stays_in_range) are secondary and need not appear in the title.

Full details: Linked Issues check

Explanation

Issue #346 has three coding requirements. (1) Remove the pytest-specific zero-jitter branch and unused import sys: met. The head version of openfga_sdk/oauth2_common.py has no sys import and no sys.modules check. jitter() returns the minimum delay when maximum <= minimum, and otherwise a random.randrange value. (2) Patch sleep in the oauth2 retry tests: met for the tests read. test/oauth2_test.py at head applies @patch("asyncio.sleep") to test_get_authentication_retries_raised_429_and_5xx_errors, test_get_authentication_stops_retrying_after_max_retry, and test_get_authentication_retries_5xx_responses. Each test asserts the sleep calls. The retry test checks 3 waits in [1, 2), [2, 4), [4, 8) ms. The max-retry test checks 2 sleep calls. The sync file test/sync/oauth2_test.py was not read directly; its time.sleep patching rests on the PR summary. (3) Cover real jitter values: met by the new test_jitter_stays_in_range in test/backoff_test.py (per the summary) and by the range assertions above. The completion criteria (grep -rn pytest openfga_sdk/ empty; test files finishing well under a second) rest on the author's reported output. The grep result and timings were not re-run in this review.

Full details: Out of Scope Changes check

Explanation

All changes map to #346. The oauth2_common.py edit removes the pytest branch and the unused sys import. The oauth2 test edits add sleep patching and wait assertions to the retry tests named in the issue. The test/backoff_test.py addition covers the jitter range behavior that the issue asks to exercise. The PR summary lists no other files. The test/oauth2_test.py retry test test_get_authentication_retries_5xx_responses sends 429 responses, but this naming mismatch is pre-existing test content and does not add out-of-scope behavior.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c24f62d6-7271-4759-a1ca-10bc6ed5489d
Commits

Reviewing files that changed from the base of the PR and between 214ec02 and fda313f.

Files selected for processing: 4

  • openfga_sdk/oauth2_common.py
  • test/backoff_test.py
  • test/oauth2_test.py
  • test/sync/oauth2_test.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@SoulPancake SoulPancake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Hung1510

It does look right. Please rebase on main: #344 added four oauth2 retry tests that still rely on the old hook, so they need the same sleep patch too.

@Hung1510
Hung1510 force-pushed the fix/jitter-under-pytest branch from fda313f to 5f51800 Compare November 27, 2026 19:31
jitter() zeroed the token retry backoff whenever pytest was in
sys.modules, so the wait depended on the test runner being imported and
the real values were never tested. Remove that branch and the unused
sys import, and patch sleep in the oauth2 retry tests instead (including
the network-error retry tests from openfga#344), asserting the waits stay
within the backoff range.

Closes openfga#346
@Hung1510

Hung1510 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, it now rebased on main. The two retry tests #344 added in each of test/oauth2_test.py and test/sync/oauth2_test.py (retries_network_errors and raises_network_error_when_retries_exhausted) now patch sleep too and assert 2 waits each. does_not_retry_tls_errors never sleeps and caps_retry_wait already patched sleep, so those two are unchanged. Full suite: 645 passed, 12 skipped; grep -rn pytest openfga_sdk/ prints nothing

@SoulPancake
SoulPancake requested a max review from Copilot November 28, 2026 08:02
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.82%. Comparing base (ab1a5c8) to head (5f51800).

❌ Your project status has failed because the head coverage (70.82%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #348      +/-   ##
==========================================
- Coverage   70.83%   70.82%   -0.02%     
==========================================
  Files         143      143              
  Lines       10867    10863       -4     
==========================================
- Hits         7698     7694       -4     
  Misses       3169     3169              

☔ 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change removing pytest-dependent OAuth2 retry jitter.

No open issues 🎉

What changed in this PR

Fixes #346 by making OAuth2 retry jitter independent of whether pytest is imported, keeping the SDK’s backoff behavior consistent during normal use and testing.

Changes:

  • Removes the pytest-specific zero-delay branch and unused sys import.
  • Mocks sleep in async and sync OAuth2 retry tests, with assertions for delay ranges and retry counts.
  • Adds a direct test of jitter’s exponential backoff bounds.
File Description
test/​sync/​oauth2_test.py Mocks synchronous retry waits and verifies delays and counts.
test/​oauth2_test.py Mocks asynchronous retry waits and verifies delays and counts.
test/​backoff_test.py Adds coverage for jitter’s expected range.
openfga_sdk/​oauth2_common.py Removes pytest-dependent jitter behavior.

Review effort: Max

@SoulPancake
SoulPancake added this pull request to the merge queue Nov 28, 2026
Merged via the queue into openfga:main with commit 60caaa5 Nov 28, 2026
25 checks passed
@Hung1510
Hung1510 deleted the fix/jitter-under-pytest branch November 28, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jitter() returns 0 whenever pytest is imported

4 participants