mirror of
https://github.com/status-im/consul.git
synced 2025-02-18 08:36:46 +00:00
Adds a better shuffle test (similar to DNS).
This commit is contained in:
parent
989619cb6b
commit
1f87480e54
@ -1,7 +1,9 @@
|
|||||||
package structs
|
package structs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -210,45 +212,36 @@ func TestStructs_HealthCheck_IsSame(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) {
|
func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) {
|
||||||
nodes := CheckServiceNodes{
|
// Make a huge list of nodes.
|
||||||
CheckServiceNode{
|
var nodes CheckServiceNodes
|
||||||
Node: &Node{
|
|
||||||
Node: "node1",
|
|
||||||
Address: "127.0.0.1",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
CheckServiceNode{
|
|
||||||
Node: &Node{
|
|
||||||
Node: "node2",
|
|
||||||
Address: "127.0.0.2",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
CheckServiceNode{
|
|
||||||
Node: &Node{
|
|
||||||
Node: "node3",
|
|
||||||
Address: "127.0.0.3",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make a copy to shuffle and make sure it matches initially.
|
|
||||||
twiddle := make(CheckServiceNodes, len(nodes))
|
|
||||||
if n := copy(twiddle, nodes); n != len(nodes) {
|
|
||||||
t.Fatalf("bad: %d", n)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(twiddle, nodes) {
|
|
||||||
t.Fatalf("bad: %v", twiddle)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Give this lots of tries to randomize. If we find a case that's
|
|
||||||
// not equal we can end the test, otherwise we will call shenanigans.
|
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
twiddle.Shuffle()
|
nodes = append(nodes, CheckServiceNode{
|
||||||
if !reflect.DeepEqual(twiddle, nodes) {
|
Node: &Node{
|
||||||
return
|
Node: fmt.Sprintf("node%d", i),
|
||||||
}
|
Address: fmt.Sprintf("127.0.0.%d", i+1),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep track of how many unique shuffles we get.
|
||||||
|
uniques := make(map[string]struct{})
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
nodes.Shuffle()
|
||||||
|
|
||||||
|
var names []string
|
||||||
|
for _, node := range nodes {
|
||||||
|
names = append(names, node.Node.Node)
|
||||||
|
}
|
||||||
|
key := strings.Join(names, "|")
|
||||||
|
uniques[key] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have to allow for the fact that there won't always be a unique
|
||||||
|
// shuffle each pass, so we just look for smell here without the test
|
||||||
|
// being flaky.
|
||||||
|
if len(uniques) < 50 {
|
||||||
|
t.Fatalf("unique shuffle ratio too low: %d/100", len(uniques))
|
||||||
}
|
}
|
||||||
t.Fatalf("shuffle is not working")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStructs_CheckServiceNodes_Filter(t *testing.T) {
|
func TestStructs_CheckServiceNodes_Filter(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user