Skip to content

Commit

Permalink
fix filter unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
weilaaa committed Jan 16, 2024
1 parent f44edd2 commit c5a3586
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/utils/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var _ = ginkgo.Describe("Filter", func() {
annotations["kubecube.test.io/app"] = "a-name" + strconv.Itoa(i)
l2.SetAnnotations(annotations)
list.Items = append(list.Items, l2)
// must sleep 1 second here to make create timestamp different
time.Sleep(1 * time.Second)
}
})

Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/filter/sort_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func SortHandler(items []unstructured.Unstructured, param *SortParam) ([]unstruc
if len(param.sortFunc) == 0 || len(param.sortName) == 0 {
return items, nil
}
sort.Slice(items, func(i, j int) bool {
// add prev sort here to avoid of random return if bellow conditions are same
sort.SliceStable(items, func(i, j int) bool {
iv, jv := items[i], items[j]
return (iv.GetName() + iv.GetNamespace()) < (jv.GetName() + jv.GetNamespace())
})
sort.SliceStable(items, func(i, j int) bool {
getStringFunc := func(items []unstructured.Unstructured, i int, j int) (string, string, error) {
si, err := GetDeepValue(items[i], param.sortName)
if err != nil {
Expand Down Expand Up @@ -116,10 +121,5 @@ func SortHandler(items []unstructured.Unstructured, param *SortParam) ([]unstruc
}
})

// add final sort here to avoid of random return if above conditions are same
sort.SliceStable(items, func(i, j int) bool {
iv, jv := items[i], items[j]
return (iv.GetName() + iv.GetNamespace()) < (jv.GetName() + jv.GetNamespace())
})
return items, nil
}

0 comments on commit c5a3586

Please sign in to comment.