Skip to content

tests: stabilize affinity handler suite#10983

Merged
ti-chi-bot[bot] merged 4 commits into
tikv:masterfrom
rleungx:fix-affinity-handler-suite-flake
Jul 8, 2026
Merged

tests: stabilize affinity handler suite#10983
ti-chi-bot[bot] merged 4 commits into
tikv:masterfrom
rleungx:fix-affinity-handler-suite-flake

Conversation

@rleungx

@rleungx rleungx commented Jul 6, 2026

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: Fixes #10385, Fixes #10565, Fixes #10857

Stabilizes TestAffinityHandlerTestSuite by covering the currently open flaky-test issues from the affinity handler suite:

  • TestAffinityListWithIDs could immediately list only a subset of newly created groups.
  • TestAffinityListWithEmptyID could immediately list no matching group after creation.
  • TearDownTest could lazily touch an environment that was not already started and panic through a nil test server.

What is changed and how does it work?

  • Run the affinity handler matrix through the non-microservice scheduling path by default.
  • Keep TestAffinityForwardedHeader as the targeted microservice forwarding coverage.
  • Add a SchedulingTestEnvironment helper that only runs cleanup over already-started environments.
  • Keep affinity group visibility checks retry-based for read-after-write assertions.

Check List

Tests

  • Unit test

Release note

None.

Summary by CodeRabbit

  • Bug Fixes

    • Improved affinity-group validations by polling until expected results are available, reducing intermittent failures.
    • Updated test cleanup behavior to reliably run cleanup across already-started environments.
  • Tests

    • Enhanced affinity-group list and create scenarios to wait for both existing and newly-created groups to appear.
    • Strengthened forwarded-header verification with repeated requests and confirmation the created group is present.
  • Chores

    • Added a test-environment helper to execute functions across any already-started clusters.

rleungx added 2 commits July 6, 2026 18:09
Signed-off-by: Ryan Leung <rleungx@gmail.com>
Signed-off-by: Ryan Leung <rleungx@gmail.com>
@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has signed the dco. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6c72baab-28d5-43ef-9f1b-9cb6d9d4d2b9

📥 Commits

Reviewing files that changed from the base of the PR and between 4b8cc5d and 3949f48.

📒 Files selected for processing (1)
  • tests/server/apiv2/handlers/affinity_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/server/apiv2/handlers/affinity_test.go

📝 Walkthrough

Walkthrough

This PR updates affinity handler tests to poll for eventually consistent affinity-group responses and adds a helper that runs cleanup only on already-started test clusters.

Changes

Affinity test reliability improvements

Layer / File(s) Summary
Test environment helper and polling support
tests/testutil.go, tests/server/apiv2/handlers/affinity_test.go
Adds RunFuncInStartedEnvs, switches the suite to tests.NonMicroserviceEnv, updates teardown cleanup to use started clusters only, and introduces polling helpers for affinity-group GET responses.
Polling-based affinity handler assertions
tests/server/apiv2/handlers/affinity_test.go
Replaces direct affinity-group assertions in the create, list-by-IDs, and forwarded-header tests with polling loops that wait for the expected group membership and header values.

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

Possibly related PRs

  • tikv/pd#10853: Also replaces brittle single-shot affinity-group assertions with repeated polling until the expected state is observable.
  • tikv/pd#10862: Also updates affinity-group list assertions to wait for expected IDs to appear.

Suggested labels: lgtm, approved

Suggested reviewers: okJiang, JmPotato

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: stabilizing the affinity handler test suite.
Description check ✅ Passed The description follows the template with problem statement, changes, checklist, and release note.
Linked Issues check ✅ Passed The changes address the flaky affinity tests and teardown panic by adding polling and limiting cleanup to started environments.
Out of Scope Changes check ✅ Passed The diff stays within test stabilization work and the helper needed to support it.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with 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.

@rleungx

rleungx commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

/retest

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.14%. Comparing base (8abdf30) to head (3949f48).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10983      +/-   ##
==========================================
- Coverage   79.26%   79.14%   -0.13%     
==========================================
  Files         541      541              
  Lines       75674    75677       +3     
==========================================
- Hits        59985    59893      -92     
- Misses      11467    11542      +75     
- Partials     4222     4242      +20     
Flag Coverage Δ
unittests 79.14% <ø> (-0.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rleungx

rleungx commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

/test pull-unit-test-next-gen-3

})
// Exercise the full affinity handler matrix through the local scheduling
// path. TestAffinityForwardedHeader covers the microservice forwarding path.
suite.env.Env = tests.NonMicroserviceEnv

@lhy1024 lhy1024 Jul 6, 2026

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.

By forcing the affinity handler matrix to run only in the non-microservice env, the microservice-path coverage now mostly depends on TestAffinityForwardedHeader. However, that test only checks status 200 and X-Forwarded-To-Microservice; it does not verify that the forwarded GET response actually contains the affinity group created by the test.

I verified this with a local mutation: I temporarily changed the scheduling microservice affinity list endpoint to always return an empty affinity_groups map, and the current test still passed. After adding a response-body assertion, the test failed as expected. So this creates a real coverage gap. Please enhance TestAffinityForwardedHeader to decode the response body and use Eventually to assert that header-check is visible in the returned AffinityGroupsResponse.

Signed-off-by: Ryan Leung <rleungx@gmail.com>
@rleungx rleungx requested a review from lhy1024 July 7, 2026 02:26

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/server/apiv2/handlers/affinity_test.go`:
- Around line 1308-1319: The polling block in the affinity test is using a
require-based check option through ReadGetJSON, which can stop Eventually
retries on transient failures. Update the Eventually callback around
handlers.AffinityGroupsResponse to use only non-fatal checks inside the loop
when calling ReadGetJSON, then keep the final assertion on
result.AffinityGroups["header-check"] after the loop so retries can continue
until the condition is met.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b1574028-8bbf-4895-82a8-f5bded67b7f0

📥 Commits

Reviewing files that changed from the base of the PR and between c009a19 and 4b8cc5d.

📒 Files selected for processing (1)
  • tests/server/apiv2/handlers/affinity_test.go

Comment thread tests/server/apiv2/handlers/affinity_test.go
Signed-off-by: Ryan Leung <rleungx@gmail.com>
@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jul 7, 2026
@rleungx rleungx requested a review from bufferflies July 8, 2026 09:46
@ti-chi-bot

ti-chi-bot Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bufferflies, lhy1024

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [bufferflies,lhy1024]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 8, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

[LGTM Timeline notifier]

Timeline:

  • 2026-07-07 06:22:06.91952658 +0000 UTC m=+89912.955621626: ☑️ agreed by lhy1024.
  • 2026-07-08 10:03:26.892993413 +0000 UTC m=+189592.929088470: ☑️ agreed by bufferflies.

@ti-chi-bot ti-chi-bot Bot merged commit 0de53f8 into tikv:master Jul 8, 2026
50 of 54 checks passed
@rleungx rleungx deleted the fix-affinity-handler-suite-flake branch July 8, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: TestAffinityListWithEmptyID TestAffinityListWithIDs is flaky TestAffinityHandlerTestSuite/TestAffinityListWithEmptyID is flaky

3 participants