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.
kratos/selector/node/direct/direct_test.go

54 lines
1.3 KiB

package direct
import (
"context"
"testing"
"time"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/selector"
"github.com/stretchr/testify/assert"
)
func TestDirect(t *testing.T) {
b := &Builder{}
wn := b.Build(selector.NewNode(
"127.0.0.1:9090",
&registry.ServiceInstance{
ID: "127.0.0.1:9090",
Name: "helloworld",
Version: "v1.0.0",
Endpoints: []string{"http://127.0.0.1:9090"},
Metadata: map[string]string{"weight": "10"},
}))
done := wn.Pick()
assert.NotNil(t, done)
time.Sleep(time.Millisecond * 10)
done(context.Background(), selector.DoneInfo{})
assert.Equal(t, float64(10), wn.Weight())
assert.Greater(t, time.Millisecond*15, wn.PickElapsed())
assert.Less(t, time.Millisecond*5, wn.PickElapsed())
}
func TestDirectDefaultWeight(t *testing.T) {
b := &Builder{}
wn := b.Build(selector.NewNode(
"127.0.0.1:9090",
&registry.ServiceInstance{
ID: "127.0.0.1:9090",
Name: "helloworld",
Version: "v1.0.0",
Endpoints: []string{"http://127.0.0.1:9090"},
}))
done := wn.Pick()
assert.NotNil(t, done)
time.Sleep(time.Millisecond * 10)
done(context.Background(), selector.DoneInfo{})
assert.Equal(t, float64(100), wn.Weight())
assert.Greater(t, time.Millisecond*15, wn.PickElapsed())
assert.Less(t, time.Millisecond*5, wn.PickElapsed())
}