2016-05-04 00:06:19 +03:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
2020-04-09 14:05:46 +03:00
|
|
|
"os"
|
2016-05-04 00:06:19 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2016-05-04 00:06:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStrategies(t *testing.T) {
|
|
|
|
testData := []*registry.Service{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Name: "test1",
|
|
|
|
Version: "latest",
|
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Id: "test1-1",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.1:1001",
|
2016-05-04 00:06:19 +03:00
|
|
|
},
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Id: "test1-2",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.2:1002",
|
2016-05-04 00:06:19 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Name: "test1",
|
|
|
|
Version: "default",
|
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Id: "test1-3",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.3:1003",
|
2016-05-04 00:06:19 +03:00
|
|
|
},
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-04 00:06:19 +03:00
|
|
|
Id: "test1-4",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.4:1004",
|
2016-05-04 00:06:19 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, strategy := range map[string]Strategy{"random": Random, "roundrobin": RoundRobin} {
|
|
|
|
next := strategy(testData)
|
|
|
|
counts := make(map[string]int)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
node, err := next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
counts[node.Id]++
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("%s: %+v\n", name, counts)
|
|
|
|
}
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|
|
|
|
}
|