Applied suggestions from @dnephin

* Renamed `cachedHealResultSorter` into `sortCheckServiceNodes`
* Use `<` instead of `strings.Compare`
* Single line comparison in unit test
This commit is contained in:
Pierre Souchay 2020-11-25 21:01:07 +01:00
parent c22f249a99
commit 09673426e3
2 changed files with 9 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt"
"reflect"
"sort"
"strings"
"time"
"github.com/hashicorp/go-bexpr"
@ -208,18 +207,17 @@ func (noopFilterEvaluator) Evaluate(_ interface{}) (bool, error) {
return true, nil
}
// cachedHealResultSorter sorts the results to match memdb semantics
// sortCheckServiceNodes sorts the results to match memdb semantics
// Sort results by Node.Node, if 2 instances match, order by Service.ID
// Will allow result to be stable sorted and match queries without cache
func cachedHealResultSorter(serviceNodes *structs.IndexedCheckServiceNodes) {
func sortCheckServiceNodes(serviceNodes *structs.IndexedCheckServiceNodes) {
sort.SliceStable(serviceNodes.Nodes, func(i, j int) bool {
left := serviceNodes.Nodes[i]
right := serviceNodes.Nodes[j]
res := strings.Compare(left.Node.Node, right.Node.Node)
if res != 0 {
return res < 0
if left.Node.Node == right.Node.Node {
return left.Service.ID < right.Service.ID
}
return strings.Compare(left.Service.ID, right.Service.ID) < 0
return left.Node.Node < right.Node.Node
})
}
@ -234,7 +232,7 @@ func (s *healthView) Result(index uint64) (interface{}, error) {
for _, node := range s.state {
result.Nodes = append(result.Nodes, node)
}
cachedHealResultSorter(&result)
sortCheckServiceNodes(&result)
return &result, nil
}

View File

@ -273,11 +273,9 @@ func TestOrderingConsistentWithMemDb(t *testing.T) {
Index: index,
},
}
cachedHealResultSorter(&result)
require.Equal(t, result.Nodes[0], zero)
require.Equal(t, result.Nodes[1], one)
require.Equal(t, result.Nodes[2], two)
require.Equal(t, result.Nodes[3], three)
sortCheckServiceNodes(&result)
expected := structs.CheckServiceNodes{zero, one, two, three}
require.Equal(t, expected, result.Nodes)
}
func TestStreamingHealthServices_FullSnapshot(t *testing.T) {