test: add more tests (#1149)
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/discov"
|
|
||||||
"github.com/tal-tech/go-zero/zrpc/internal"
|
"github.com/tal-tech/go-zero/zrpc/internal"
|
||||||
"github.com/tal-tech/go-zero/zrpc/internal/auth"
|
"github.com/tal-tech/go-zero/zrpc/internal/auth"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@@ -79,18 +78,6 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClientNoAuth returns a Client without authentication.
|
|
||||||
func NewClientNoAuth(c discov.EtcdConf, opts ...ClientOption) (Client, error) {
|
|
||||||
client, err := internal.NewClient(internal.BuildDiscovTarget(c.Hosts, c.Key), opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &RpcClient{
|
|
||||||
client: client,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewClientWithTarget returns a Client with connecting to given target.
|
// NewClientWithTarget returns a Client with connecting to given target.
|
||||||
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
|
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
|
||||||
return internal.NewClient(target, opts...)
|
return internal.NewClient(target, opts...)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/core/discov"
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/zrpc/internal/mock"
|
"github.com/tal-tech/go-zero/zrpc/internal/mock"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@@ -82,6 +83,20 @@ func TestDepositServer_Deposit(t *testing.T) {
|
|||||||
return invoker(ctx, method, req, reply, cc, opts...)
|
return invoker(ctx, method, req, reply, cc, opts...)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
tarConfClient := MustNewClient(
|
||||||
|
RpcClientConf{
|
||||||
|
Target: "foo",
|
||||||
|
App: "foo",
|
||||||
|
Token: "bar",
|
||||||
|
Timeout: 1000,
|
||||||
|
},
|
||||||
|
WithDialOption(grpc.WithInsecure()),
|
||||||
|
WithDialOption(grpc.WithContextDialer(dialer())),
|
||||||
|
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
|
||||||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||||
|
return invoker(ctx, method, req, reply, cc, opts...)
|
||||||
|
}),
|
||||||
|
)
|
||||||
targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
|
targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
|
||||||
WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
|
WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
|
||||||
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
|
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
|
||||||
@@ -91,11 +106,15 @@ func TestDepositServer_Deposit(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
clients := []Client{
|
clients := []Client{
|
||||||
directClient,
|
directClient,
|
||||||
|
tarConfClient,
|
||||||
targetClient,
|
targetClient,
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
tt := tt
|
||||||
for _, client := range clients {
|
for _, client := range clients {
|
||||||
|
client := client
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
cli := mock.NewDepositServiceClient(client.Conn())
|
cli := mock.NewDepositServiceClient(client.Conn())
|
||||||
request := &mock.DepositRequest{Amount: tt.amount}
|
request := &mock.DepositRequest{Amount: tt.amount}
|
||||||
response, err := cli.Deposit(context.Background(), request)
|
response, err := cli.Deposit(context.Background(), request)
|
||||||
@@ -119,3 +138,39 @@ func TestDepositServer_Deposit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewClientWithError(t *testing.T) {
|
||||||
|
_, err := NewClient(
|
||||||
|
RpcClientConf{
|
||||||
|
App: "foo",
|
||||||
|
Token: "bar",
|
||||||
|
Timeout: 1000,
|
||||||
|
},
|
||||||
|
WithDialOption(grpc.WithInsecure()),
|
||||||
|
WithDialOption(grpc.WithContextDialer(dialer())),
|
||||||
|
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
|
||||||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||||
|
return invoker(ctx, method, req, reply, cc, opts...)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
_, err = NewClient(
|
||||||
|
RpcClientConf{
|
||||||
|
Etcd: discov.EtcdConf{
|
||||||
|
Hosts: []string{"localhost:2379"},
|
||||||
|
Key: "mock",
|
||||||
|
},
|
||||||
|
App: "foo",
|
||||||
|
Token: "bar",
|
||||||
|
Timeout: 1,
|
||||||
|
},
|
||||||
|
WithDialOption(grpc.WithInsecure()),
|
||||||
|
WithDialOption(grpc.WithContextDialer(dialer())),
|
||||||
|
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
|
||||||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||||
|
return invoker(ctx, method, req, reply, cc, opts...)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user