You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
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
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.
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.
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.
Closesopenfga#346
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
✅ 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.
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.
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
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.
Description
jitter()inopenfga_sdk/oauth2_common.pyreturned 0 wheneverpytestwas insys.modules, so token retry backoff depended on the test runner being imported, and the suite never exercised the real values.import sys.asyncio.sleep/time.sleep, liketest/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 formin_wait_in_ms=1stops_retrying_after_max_retry: 2 waitsretries_5xx_responses: 3 waitstest_jitter_stays_in_rangeintest/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).ruff checkandruff formatare clean.Summary by CodeRabbit