You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/balancer/p2c/p2c_test.go

153 lines
3.1 KiB
Go

package p2c
4 years ago
import (
"context"
"fmt"
4 years ago
"runtime"
4 years ago
"strconv"
4 years ago
"sync"
4 years ago
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/mathx"
"github.com/zeromicro/go-zero/core/stringx"
4 years ago
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
4 years ago
"google.golang.org/grpc/codes"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/status"
)
func init() {
logx.Disable()
}
func TestP2cPicker_PickNil(t *testing.T) {
builder := new(p2cPickerBuilder)
picker := builder.Build(base.PickerBuildInfo{})
_, err := picker.Pick(balancer.PickInfo{
4 years ago
FullMethodName: "/",
Ctx: context.Background(),
})
assert.NotNil(t, err)
}
func TestP2cPicker_Pick(t *testing.T) {
4 years ago
tests := []struct {
name string
candidates int
err error
4 years ago
threshold float64
4 years ago
}{
{
name: "empty",
candidates: 0,
err: balancer.ErrNoSubConnAvailable,
},
4 years ago
{
name: "single",
candidates: 1,
4 years ago
threshold: 0.9,
4 years ago
},
4 years ago
{
name: "two",
candidates: 2,
4 years ago
threshold: 0.5,
4 years ago
},
4 years ago
{
name: "multiple",
candidates: 100,
4 years ago
threshold: 0.95,
4 years ago
},
}
for _, test := range tests {
4 years ago
test := test
4 years ago
t.Run(test.name, func(t *testing.T) {
4 years ago
t.Parallel()
4 years ago
const total = 10000
4 years ago
builder := new(p2cPickerBuilder)
ready := make(map[balancer.SubConn]base.SubConnInfo)
4 years ago
for i := 0; i < test.candidates; i++ {
ready[mockClientConn{
id: stringx.Rand(),
}] = base.SubConnInfo{
Address: resolver.Address{
Addr: strconv.Itoa(i),
},
}
4 years ago
}
picker := builder.Build(base.PickerBuildInfo{
ReadySCs: ready,
})
4 years ago
var wg sync.WaitGroup
wg.Add(total)
for i := 0; i < total; i++ {
result, err := picker.Pick(balancer.PickInfo{
4 years ago
FullMethodName: "/",
Ctx: context.Background(),
})
assert.Equal(t, test.err, err)
if test.err != nil {
return
}
4 years ago
if i%100 == 0 {
err = status.Error(codes.DeadlineExceeded, "deadline")
}
4 years ago
go func() {
4 years ago
runtime.Gosched()
result.Done(balancer.DoneInfo{
4 years ago
Err: err,
})
wg.Done()
}()
4 years ago
}
4 years ago
wg.Wait()
dist := make(map[any]int)
4 years ago
conns := picker.(*p2cPicker).conns
for _, conn := range conns {
dist[conn.addr.Addr] = int(conn.requests)
}
entropy := mathx.CalcEntropy(dist)
4 years ago
assert.True(t, entropy > test.threshold, fmt.Sprintf("entropy is %f, less than %f",
entropy, test.threshold))
4 years ago
})
}
}
func TestPickerWithEmptyConns(t *testing.T) {
var picker p2cPicker
_, err := picker.Pick(balancer.PickInfo{
FullMethodName: "/",
Ctx: context.Background(),
})
assert.ErrorIs(t, err, balancer.ErrNoSubConnAvailable)
}
type mockClientConn struct {
// add random string member to avoid map key equality.
id string
}
4 years ago
func (m mockClientConn) GetOrBuildProducer(builder balancer.ProducerBuilder) (
p balancer.Producer, close func()) {
return builder.Build(m)
}
4 years ago
func (m mockClientConn) UpdateAddresses(addresses []resolver.Address) {
}
4 years ago
func (m mockClientConn) Connect() {
}
func (m mockClientConn) Shutdown() {
}