Skip to content

Commit 30995c2

Browse files
authored
fix test flakiness (#636)
1 parent e4a0431 commit 30995c2

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

internal/scheduling/reservations/commitments/api_change_commitments_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,16 @@ func (tfg TestFlavorGroup) ToFlavorGroupsKnowledge() FlavorGroupsKnowledge {
615615
groupMap[groupName] = append(groupMap[groupName], flavor)
616616
}
617617

618+
// Sort group names for deterministic iteration
619+
sortedGroupNames := make([]string, 0, len(groupMap))
620+
for groupName := range groupMap {
621+
sortedGroupNames = append(sortedGroupNames, groupName)
622+
}
623+
sort.Strings(sortedGroupNames)
624+
618625
var groups []compute.FlavorGroupFeature
619-
for groupName, groupFlavors := range groupMap {
626+
for _, groupName := range sortedGroupNames {
627+
groupFlavors := groupMap[groupName]
620628
if len(groupFlavors) == 0 {
621629
continue
622630
}
@@ -1765,6 +1773,7 @@ func newAPIResponse(rejectReasonSubstrings ...string) APIResponseExpectation {
17651773

17661774
// buildRequestJSON converts a test CommitmentChangeRequest to JSON string.
17671775
// Builds the nested JSON structure directly for simplicity.
1776+
// Uses sorted iteration to ensure deterministic JSON output.
17681777
func buildRequestJSON(req CommitmentChangeRequest) string {
17691778
// Group commitments by project and resource for nested structure
17701779
type projectResources map[liquid.ResourceName][]TestCommitment
@@ -1780,11 +1789,30 @@ func buildRequestJSON(req CommitmentChangeRequest) string {
17801789
)
17811790
}
17821791

1783-
// Build nested JSON structure
1792+
// Sort projects for deterministic iteration
1793+
sortedProjects := make([]string, 0, len(byProject))
1794+
for projectID := range byProject {
1795+
sortedProjects = append(sortedProjects, projectID)
1796+
}
1797+
sort.Strings(sortedProjects)
1798+
1799+
// Build nested JSON structure with sorted iteration
17841800
var projectParts []string
1785-
for projectID, resources := range byProject {
1801+
for _, projectID := range sortedProjects {
1802+
resources := byProject[projectID]
1803+
1804+
// Sort resource names for deterministic iteration
1805+
sortedResources := make([]liquid.ResourceName, 0, len(resources))
1806+
for resourceName := range resources {
1807+
sortedResources = append(sortedResources, resourceName)
1808+
}
1809+
sort.Slice(sortedResources, func(i, j int) bool {
1810+
return string(sortedResources[i]) < string(sortedResources[j])
1811+
})
1812+
17861813
var resourceParts []string
1787-
for resourceName, commits := range resources {
1814+
for _, resourceName := range sortedResources {
1815+
commits := resources[resourceName]
17881816
var commitParts []string
17891817
for _, c := range commits {
17901818
expiryTime := time.Now().Add(time.Duration(defaultCommitmentExpiryYears) * 365 * 24 * time.Hour)

internal/scheduling/reservations/commitments/syncer_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package commitments
55

66
import (
77
"context"
8+
"sort"
89
"testing"
910

1011
"github.com/cobaltcore-dev/cortex/api/v1alpha1"
@@ -32,9 +33,17 @@ type FlavorGroupData struct {
3233
func createFlavorGroupKnowledge(t *testing.T, groups map[string]FlavorGroupData) *v1alpha1.Knowledge {
3334
t.Helper()
3435

35-
// Build flavor group features
36+
// Sort group names for deterministic iteration
37+
sortedGroupNames := make([]string, 0, len(groups))
38+
for groupName := range groups {
39+
sortedGroupNames = append(sortedGroupNames, groupName)
40+
}
41+
sort.Strings(sortedGroupNames)
42+
43+
// Build flavor group features with sorted iteration
3644
features := make([]compute.FlavorGroupFeature, 0, len(groups))
37-
for groupName, data := range groups {
45+
for _, groupName := range sortedGroupNames {
46+
data := groups[groupName]
3847
features = append(features, compute.FlavorGroupFeature{
3948
Name: groupName,
4049
Flavors: []compute.FlavorInGroup{

0 commit comments

Comments
 (0)