Skip to content

Commit 19bf6ec

Browse files
s35560claude
andcommitted
test(api): fix flaky GetEvaluations tests caused by CreatedAt
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>
1 parent e1f410e commit 19bf6ec

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

pkg/api/api/api_grpc_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,9 @@ func TestGrpcGetEvaluationsValidation(t *testing.T) {
23592359
"authorization": []string{"test-key"},
23602360
})
23612361
actual, err := gs.GetEvaluations(ctx, p.input)
2362+
if p.expected != nil && actual != nil {
2363+
normalizeUserEvaluationsCreatedAt(t, p.expected.Evaluations, actual.Evaluations, "%s", p.desc)
2364+
}
23622365
assert.Equal(t, p.expected, actual, "%s", p.desc)
23632366
assert.Equal(t, p.expectedErr, err, "%s", p.desc)
23642367
})
@@ -2410,6 +2413,7 @@ func TestGrpcGetEvaluationsZeroFeature(t *testing.T) {
24102413
"authorization": []string{"test-key"},
24112414
})
24122415
actual, err := gs.GetEvaluations(ctx, p.input)
2416+
normalizeUserEvaluationsCreatedAt(t, p.expected.Evaluations, actual.Evaluations, "%s", p.desc)
24132417
assert.Equal(t, p.expected, actual, "%s", p.desc)
24142418
assert.Equal(t, p.expected.State, actual.State, "%s", p.desc)
24152419
assert.Equal(t, p.expectedErr, err, "%s", p.desc)
@@ -4959,6 +4963,20 @@ func emptyUserEvaluations(t *testing.T) *featureproto.UserEvaluations {
49594963
}
49604964
}
49614965

4966+
// normalizeUserEvaluationsCreatedAt asserts CreatedAt is roughly now, then aligns expected so Equal stays stable.
4967+
func normalizeUserEvaluationsCreatedAt(
4968+
t *testing.T,
4969+
expected, actual *featureproto.UserEvaluations,
4970+
msgAndArgs ...interface{},
4971+
) {
4972+
t.Helper()
4973+
if expected == nil || actual == nil {
4974+
return
4975+
}
4976+
assert.InDelta(t, time.Now().Unix(), actual.CreatedAt, 5, msgAndArgs...)
4977+
expected.CreatedAt = actual.CreatedAt
4978+
}
4979+
49624980
func TestGrpcListFeatures(t *testing.T) {
49634981
t.Parallel()
49644982
mockController := gomock.NewController(t)

pkg/api/api/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ func TestGetEvaluationsValidation(t *testing.T) {
716716
decoded := decodeSuccessResponse(t, actual.Body)
717717
err := json.Unmarshal(decoded, &respBody)
718718
assert.NoError(t, err)
719-
// FIXME: This is a flaky test. CreateAt may not be equal ocasionally.
719+
normalizeUserEvaluationsCreatedAt(t, p.expected.Evaluations, respBody.Evaluations, "%s", p.desc)
720720
assert.Equal(t, p.expected, &respBody, "%s", p.desc)
721721
}
722722
}
@@ -779,6 +779,7 @@ func TestGetEvaluationsZeroFeature(t *testing.T) {
779779
decoded := decodeSuccessResponse(t, actual.Body)
780780
err := json.Unmarshal(decoded, &respBody)
781781
assert.NoError(t, err)
782+
normalizeUserEvaluationsCreatedAt(t, p.expected.Evaluations, respBody.Evaluations, "%s", p.desc)
782783
assert.Equal(t, p.expected, &respBody, "%s", p.desc)
783784
}
784785
}

0 commit comments

Comments
 (0)