Skip to content

Conversation

evansims
Copy link
Member

@evansims evansims commented Oct 3, 2025

This PR addresses flaky unit tests resulting from race conditions in async batch check operations and timing precision issues in retry logic.

  • Fixed race conditions when running BatchCheck tests with parallelization
    Replaced sequential mock responses with conditional responses based on request body content. I only noticed this after the package modernization and adoption of uv, where pytest is run with parallelization enabled by default, unlike previously.

  • Improved retry timing stability
    I adjusted the retry delay from exact matches to tolerance ranges of 1 or 2 seconds, and increased test timeouts to account for variance. I haven't encountered this locally, but have seen it pop up a few times on our CI during PR status checks.

References

openfga/sdk-generator#629

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

Summary by CodeRabbit

  • Tests
    • Stabilized batch check tests by making mock responses depend on request content, reducing flakiness with concurrent requests and accurately simulating per-request successes and failures.
    • Updated rate-limit retry tests to use longer Retry-After intervals and more tolerant timing assertions to align with current retry behavior.
    • Applied improvements across both async and sync client test suites.
    • No changes to public APIs or runtime behavior.

@evansims evansims requested a review from a team as a code owner October 3, 2025 22:37
@evansims evansims added the enhancement New feature or request label Oct 3, 2025
Copy link
Contributor

coderabbitai bot commented Oct 3, 2025

Walkthrough

Tests updated to adjust 429 Retry-After timings and tolerances, and to replace sequential mock responses with request-body-driven side_effect dispatchers in batch check client tests across async and sync variants.

Changes

Cohort / File(s) Summary
Retry timing adjustments (429 handling)
test/api/open_fga_api_test.py, test/sync/open_fga_api_test.py
Increased Retry-After values to 10s (HTTP-date), raised max_wait_in_sec to 15, and relaxed sleep duration assertions to a range. No API changes.
Request-body-driven mock dispatch for batch checks
test/client/client_test.py, test/sync/client/client_test.py
Replaced ordered mock side effects with a single side_effect function selecting responses based on request body (tuple_key.user); includes per-user branching and error raising in failure paths.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant T as Test
  participant C as Client
  participant H as HTTP Layer (mocked)
  participant D as side_effect Dispatcher

  T->>C: batch_check(requests[])
  C->>H: POST /checks (per request)
  H->>D: Evaluate side_effect(request body)
  alt user-specific match
    D-->>H: Return mock response (allowed/denied)
  else failure case
    D-->>H: Raise ValidationException
  end
  H-->>C: Response or Exception
  C-->>T: Aggregated results/errors
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • ttrzeng

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely describes the primary change of the pull request, namely addressing flaky unit tests in the CI, and uses an appropriate conventional prefix without extraneous detail.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ci/fix-flaky-unit-tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.73%. Comparing base (4f3eeb4) to head (463efcc).

❌ Your project status has failed because the head coverage (70.73%) 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     #231   +/-   ##
=======================================
  Coverage   70.73%   70.73%           
=======================================
  Files         134      134           
  Lines       10882    10882           
=======================================
  Hits         7697     7697           
  Misses       3185     3185           

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

@evansims evansims added this pull request to the merge queue Oct 3, 2025
Merged via the queue into main with commit 8e38d75 Oct 3, 2025
28 checks passed
@evansims evansims deleted the ci/fix-flaky-unit-tests branch October 3, 2025 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants