test: add more tests (#1604)

This commit is contained in:
Kevin Wan
2022-03-02 21:19:04 +08:00
committed by GitHub
parent be277a7376
commit 900bc96420
6 changed files with 40 additions and 19 deletions

View File

@@ -12,20 +12,20 @@ import (
// A RpcProxy is a rpc proxy.
type RpcProxy struct {
backend string
clients map[string]Client
options []internal.ClientOption
sharedCalls syncx.SingleFlight
lock sync.Mutex
backend string
clients map[string]Client
options []internal.ClientOption
singleFlight syncx.SingleFlight
lock sync.Mutex
}
// NewProxy returns a RpcProxy.
func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
return &RpcProxy{
backend: backend,
clients: make(map[string]Client),
options: opts,
sharedCalls: syncx.NewSingleFlight(),
backend: backend,
clients: make(map[string]Client),
options: opts,
singleFlight: syncx.NewSingleFlight(),
}
}
@@ -33,7 +33,7 @@ func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
func (p *RpcProxy) TakeConn(ctx context.Context) (*grpc.ClientConn, error) {
cred := auth.ParseCredential(ctx)
key := cred.App + "/" + cred.Token
val, err := p.sharedCalls.Do(key, func() (interface{}, error) {
val, err := p.singleFlight.Do(key, func() (interface{}, error) {
p.lock.Lock()
client, ok := p.clients[key]
p.lock.Unlock()

View File

@@ -64,3 +64,9 @@ func TestProxy(t *testing.T) {
})
}
}
func TestRpcProxy_TakeConnNewClientFailed(t *testing.T) {
proxy := NewProxy("foo", WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithBlock()))
_, err := proxy.TakeConn(context.Background())
assert.NotNil(t, err)
}

View File

@@ -101,6 +101,21 @@ func TestServer_HasEtcd(t *testing.T) {
srv.Stop()
}
func TestServer_StartFailed(t *testing.T) {
srv := MustNewServer(RpcServerConf{
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
ServiceName: "foo",
Mode: "console",
},
},
ListenOn: "localhost:aaa",
}, func(server *grpc.Server) {
})
assert.Panics(t, srv.Start)
}
type mockedServer struct {
unaryInterceptors []grpc.UnaryServerInterceptor
streamInterceptors []grpc.StreamServerInterceptor