You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
2 years ago
|
package selector_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.diulo.com/mogfee/kit/registry"
|
||
|
"git.diulo.com/mogfee/kit/selector"
|
||
|
"git.diulo.com/mogfee/kit/selector/filter"
|
||
|
"git.diulo.com/mogfee/kit/selector/random"
|
||
|
"gopkg.in/go-playground/assert.v1"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestSelect(t *testing.T) {
|
||
|
t.Run("没有可用服务", func(t *testing.T) {
|
||
|
s := random.New()
|
||
|
_, _, err := s.Select(context.Background())
|
||
|
assert.Equal(t, err, selector.ErrNoAvailable)
|
||
|
})
|
||
|
t.Run("正常访问", func(t *testing.T) {
|
||
|
s := random.New()
|
||
|
s.Apply(nodes)
|
||
|
selected, _, err := s.Select(context.Background())
|
||
|
assert.Equal(t, err, nil)
|
||
|
assert.Equal(t, selected.Address(), nodes[0].Address())
|
||
|
|
||
|
selected, _, err = s.Select(context.Background())
|
||
|
assert.Equal(t, err, nil)
|
||
|
assert.Equal(t, selected.Address(), nodes[1].Address())
|
||
|
})
|
||
|
|
||
|
t.Run("获取v2版本", func(t *testing.T) {
|
||
|
s := random.New()
|
||
|
s.Apply(nodes)
|
||
|
selected, _, err := s.Select(context.Background(), selector.WithNodeFilter(filter.Version("v2.0.0")))
|
||
|
assert.Equal(t, err, nil)
|
||
|
assert.Equal(t, selected.Address(), nodes[1].Address())
|
||
|
})
|
||
|
}
|
||
|
|
||
|
var nodes = []selector.Node{
|
||
|
selector.NewNode("http", "127.0.0.1:8080", ®istry.ServiceInstance{
|
||
|
ID: "127.0.0.1:8080",
|
||
|
Name: "hello",
|
||
|
Version: "v1.0.0",
|
||
|
Metadata: map[string]string{"weight": "10"},
|
||
|
Endpoints: []string{"127.0.0.1:8080"},
|
||
|
}),
|
||
|
selector.NewNode("http", "127.0.0.1:9090", ®istry.ServiceInstance{
|
||
|
ID: "127.0.0.1:9090",
|
||
|
Name: "hello",
|
||
|
Version: "v2.0.0",
|
||
|
Metadata: map[string]string{"weight": "10"},
|
||
|
Endpoints: []string{"127.0.0.1:9090"},
|
||
|
}),
|
||
|
}
|