Skip to content

test(api): fix flaky GetEvaluations tests caused by CreatedAt - #2792

Merged
t-kikuc merged 2 commits into
mainfrom
fix/flaky-get-evaluations-created-at
Sep 3, 2026
Merged

test(api): fix flaky GetEvaluations tests caused by CreatedAt#2792
t-kikuc merged 2 commits into
mainfrom
fix/flaky-get-evaluations-created-at

Conversation

@kimurakazuhiro-c

@kimurakazuhiro-c kimurakazuhiro-c commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1023

What this PR does

Removes the flakiness in the GetEvaluations tests by asserting UserEvaluations.CreatedAt with a time tolerance instead of an exact equality check.

Background / Why this PR is needed

UserEvaluations.CreatedAt is stamped at request time inside the handler, so the hard-coded expectation and the actual value differ whenever the second boundary is crossed between building the expectation and running the request. The tests compared the whole response with assert.Equal, so this occasionally failed. api_test.go carried a FIXME: This is a flaky test. CreateAt may not be equal ocasionally. comment acknowledging the problem without fixing it; that comment is now removed.

Points

  • normalizeUserEvaluationsCreatedAt asserts the actual CreatedAt is within 5 seconds of time.Now().Unix(), then copies it into the expected value so the subsequent assert.Equal still verifies every other field. This keeps the field covered rather than ignoring it.
  • The 5-second delta is a deliberately loose bound: these are in-process handler calls that finish in milliseconds, so the tolerance only needs to absorb clock-boundary skew and CI scheduling stalls, not real latency.
  • Applied to all four affected tests: TestGrpcGetEvaluationsValidation, TestGrpcGetEvaluationsZeroFeature (gRPC) and TestGetEvaluationsValidation, TestGetEvaluationsZeroFeature (HTTP).
  • The helper is a no-op when either side is nil, so the error-path cases that expect a nil response are unaffected.
  • Test-only change: no production code is touched.
  • TestGrpcGetEvaluationsValidation is flaky #1023 reports the failure at pkg/gateway/api/api_grpc_test.go; that package has since moved to pkg/api/api/, and the failing test is the same TestGrpcGetEvaluationsValidation/success case fixed here.

The GetEvaluations tests compared the whole response including CreatedAt,
which is generated at request time, so the assertion failed occasionally.

Add normalizeUserEvaluationsCreatedAt to assert CreatedAt is roughly the
current time, then align the expected value so the equality check stays
stable.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

Fixes flaky GetEvaluations tests by tolerating small CreatedAt timing differences.

Changes:

  • Adds a helper to validate and normalize CreatedAt.
  • Applies it across affected HTTP and gRPC tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/api/api/api_test.go Stabilizes HTTP evaluation assertions.
pkg/api/api/api_grpc_test.go Adds the helper and stabilizes gRPC assertions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pkg/api/api/api_grpc_test.go Outdated
Comment on lines +4966 to +4978
// normalizeUserEvaluationsCreatedAt asserts CreatedAt is roughly now, then aligns expected so Equal stays stable.
func normalizeUserEvaluationsCreatedAt(
t *testing.T,
expected, actual *featureproto.UserEvaluations,
msgAndArgs ...interface{},
) {
t.Helper()
if expected == nil || actual == nil {
return
}
assert.InDelta(t, time.Now().Unix(), actual.CreatedAt, 5, msgAndArgs...)
expected.CreatedAt = actual.CreatedAt
}

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.

The comment describes what the function does (readable from the signature + body). Replace with why — the flaky CreatedAt timing that motivates this helper.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@t-kikuc
I have updated the description to address the instability (flaky behavior) regarding the timing of CreatedAt.

Comment thread pkg/api/api/api_grpc_test.go Outdated
func normalizeUserEvaluationsCreatedAt(
t *testing.T,
expected, actual *featureproto.UserEvaluations,
msgAndArgs ...interface{},

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.

msgAndArgs can be dropped — t.Helper() already attributes the failure to the caller line, and the call sites run inside t.Run(p.desc) which names the case in the output.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@t-kikuc
Modified to omit msgAndArgs.

Explain why the helper exists (the service stamps CreatedAt mid-test, so a
plain Equal is flaky) instead of restating what it does, and drop the
msgAndArgs parameter: t.Helper() already points at the caller line.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@kimurakazuhiro-c
kimurakazuhiro-c marked this pull request as ready for review September 3, 2026 02:14
@t-kikuc
t-kikuc merged commit f5df5d1 into main Sep 3, 2026
11 checks passed
@t-kikuc
t-kikuc deleted the fix/flaky-get-evaluations-created-at branch September 3, 2026 03:02
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.

TestGrpcGetEvaluationsValidation is flaky

3 participants