add unit test (#921)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/core/mathx"
|
"github.com/tal-tech/go-zero/core/mathx"
|
||||||
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
"google.golang.org/grpc/balancer"
|
"google.golang.org/grpc/balancer"
|
||||||
"google.golang.org/grpc/balancer/base"
|
"google.golang.org/grpc/balancer/base"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
@@ -36,8 +37,14 @@ func TestP2cPicker_Pick(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
candidates int
|
candidates int
|
||||||
|
err error
|
||||||
threshold float64
|
threshold float64
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
candidates: 0,
|
||||||
|
err: balancer.ErrNoSubConnAvailable,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "single",
|
name: "single",
|
||||||
candidates: 1,
|
candidates: 1,
|
||||||
@@ -64,7 +71,9 @@ func TestP2cPicker_Pick(t *testing.T) {
|
|||||||
builder := new(p2cPickerBuilder)
|
builder := new(p2cPickerBuilder)
|
||||||
ready := make(map[balancer.SubConn]base.SubConnInfo)
|
ready := make(map[balancer.SubConn]base.SubConnInfo)
|
||||||
for i := 0; i < test.candidates; i++ {
|
for i := 0; i < test.candidates; i++ {
|
||||||
ready[new(mockClientConn)] = base.SubConnInfo{
|
ready[mockClientConn{
|
||||||
|
id: stringx.Rand(),
|
||||||
|
}] = base.SubConnInfo{
|
||||||
Address: resolver.Address{
|
Address: resolver.Address{
|
||||||
Addr: strconv.Itoa(i),
|
Addr: strconv.Itoa(i),
|
||||||
},
|
},
|
||||||
@@ -81,10 +90,16 @@ func TestP2cPicker_Pick(t *testing.T) {
|
|||||||
FullMethodName: "/",
|
FullMethodName: "/",
|
||||||
Ctx: context.Background(),
|
Ctx: context.Background(),
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Equal(t, test.err, err)
|
||||||
|
|
||||||
|
if test.err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if i%100 == 0 {
|
if i%100 == 0 {
|
||||||
err = status.Error(codes.DeadlineExceeded, "deadline")
|
err = status.Error(codes.DeadlineExceeded, "deadline")
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
runtime.Gosched()
|
runtime.Gosched()
|
||||||
result.Done(balancer.DoneInfo{
|
result.Done(balancer.DoneInfo{
|
||||||
@@ -108,7 +123,10 @@ func TestP2cPicker_Pick(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockClientConn struct{}
|
type mockClientConn struct {
|
||||||
|
// add random string member to avoid map key equality.
|
||||||
|
id string
|
||||||
|
}
|
||||||
|
|
||||||
func (m mockClientConn) UpdateAddresses(addresses []resolver.Address) {
|
func (m mockClientConn) UpdateAddresses(addresses []resolver.Address) {
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user