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" "fmt"
"reflect" "reflect"
"sort" "sort"
"strings"
"time" "time"
"github.com/hashicorp/go-bexpr" "github.com/hashicorp/go-bexpr"
@ -208,18 +207,17 @@ func (noopFilterEvaluator) Evaluate(_ interface{}) (bool, error) {
return true, nil 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 // 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 // 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 { sort.SliceStable(serviceNodes.Nodes, func(i, j int) bool {
left := serviceNodes.Nodes[i] left := serviceNodes.Nodes[i]
right := serviceNodes.Nodes[j] right := serviceNodes.Nodes[j]
res := strings.Compare(left.Node.Node, right.Node.Node) if left.Node.Node == right.Node.Node {
if res != 0 { return left.Service.ID < right.Service.ID
return res < 0
} }
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 { for _, node := range s.state {
result.Nodes = append(result.Nodes, node) result.Nodes = append(result.Nodes, node)
} }
cachedHealResultSorter(&result) sortCheckServiceNodes(&result)
return &result, nil return &result, nil
} }

View File

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