Skip to content

Commit a40fa40

Browse files
authored
test: add more tests (zeromicro#1149)
1 parent eab77e2 commit a40fa40

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

zrpc/client.go

-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"log"
55
"time"
66

7-
"github.com/tal-tech/go-zero/core/discov"
87
"github.com/tal-tech/go-zero/zrpc/internal"
98
"github.com/tal-tech/go-zero/zrpc/internal/auth"
109
"google.golang.org/grpc"
@@ -79,18 +78,6 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
7978
}, nil
8079
}
8180

82-
// NewClientNoAuth returns a Client without authentication.
83-
func NewClientNoAuth(c discov.EtcdConf, opts ...ClientOption) (Client, error) {
84-
client, err := internal.NewClient(internal.BuildDiscovTarget(c.Hosts, c.Key), opts...)
85-
if err != nil {
86-
return nil, err
87-
}
88-
89-
return &RpcClient{
90-
client: client,
91-
}, nil
92-
}
93-
9481
// NewClientWithTarget returns a Client with connecting to given target.
9582
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
9683
return internal.NewClient(target, opts...)

zrpc/client_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/stretchr/testify/assert"
12+
"github.com/tal-tech/go-zero/core/discov"
1213
"github.com/tal-tech/go-zero/core/logx"
1314
"github.com/tal-tech/go-zero/zrpc/internal/mock"
1415
"google.golang.org/grpc"
@@ -82,6 +83,20 @@ func TestDepositServer_Deposit(t *testing.T) {
8283
return invoker(ctx, method, req, reply, cc, opts...)
8384
}),
8485
)
86+
tarConfClient := MustNewClient(
87+
RpcClientConf{
88+
Target: "foo",
89+
App: "foo",
90+
Token: "bar",
91+
Timeout: 1000,
92+
},
93+
WithDialOption(grpc.WithInsecure()),
94+
WithDialOption(grpc.WithContextDialer(dialer())),
95+
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
96+
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
97+
return invoker(ctx, method, req, reply, cc, opts...)
98+
}),
99+
)
85100
targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
86101
WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
87102
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
@@ -91,11 +106,15 @@ func TestDepositServer_Deposit(t *testing.T) {
91106
assert.Nil(t, err)
92107
clients := []Client{
93108
directClient,
109+
tarConfClient,
94110
targetClient,
95111
}
96112
for _, tt := range tests {
113+
tt := tt
97114
for _, client := range clients {
115+
client := client
98116
t.Run(tt.name, func(t *testing.T) {
117+
t.Parallel()
99118
cli := mock.NewDepositServiceClient(client.Conn())
100119
request := &mock.DepositRequest{Amount: tt.amount}
101120
response, err := cli.Deposit(context.Background(), request)
@@ -119,3 +138,39 @@ func TestDepositServer_Deposit(t *testing.T) {
119138
}
120139
}
121140
}
141+
142+
func TestNewClientWithError(t *testing.T) {
143+
_, err := NewClient(
144+
RpcClientConf{
145+
App: "foo",
146+
Token: "bar",
147+
Timeout: 1000,
148+
},
149+
WithDialOption(grpc.WithInsecure()),
150+
WithDialOption(grpc.WithContextDialer(dialer())),
151+
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
152+
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
153+
return invoker(ctx, method, req, reply, cc, opts...)
154+
}),
155+
)
156+
assert.NotNil(t, err)
157+
158+
_, err = NewClient(
159+
RpcClientConf{
160+
Etcd: discov.EtcdConf{
161+
Hosts: []string{"localhost:2379"},
162+
Key: "mock",
163+
},
164+
App: "foo",
165+
Token: "bar",
166+
Timeout: 1,
167+
},
168+
WithDialOption(grpc.WithInsecure()),
169+
WithDialOption(grpc.WithContextDialer(dialer())),
170+
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
171+
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
172+
return invoker(ctx, method, req, reply, cc, opts...)
173+
}),
174+
)
175+
assert.NotNil(t, err)
176+
}

0 commit comments

Comments
 (0)