9
9
"time"
10
10
11
11
"github.com/stretchr/testify/assert"
12
+ "github.com/tal-tech/go-zero/core/discov"
12
13
"github.com/tal-tech/go-zero/core/logx"
13
14
"github.com/tal-tech/go-zero/zrpc/internal/mock"
14
15
"google.golang.org/grpc"
@@ -82,6 +83,20 @@ func TestDepositServer_Deposit(t *testing.T) {
82
83
return invoker (ctx , method , req , reply , cc , opts ... )
83
84
}),
84
85
)
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
+ )
85
100
targetClient , err := NewClientWithTarget ("foo" , WithDialOption (grpc .WithInsecure ()),
86
101
WithDialOption (grpc .WithContextDialer (dialer ())), WithUnaryClientInterceptor (
87
102
func (ctx context.Context , method string , req , reply interface {}, cc * grpc.ClientConn ,
@@ -91,11 +106,15 @@ func TestDepositServer_Deposit(t *testing.T) {
91
106
assert .Nil (t , err )
92
107
clients := []Client {
93
108
directClient ,
109
+ tarConfClient ,
94
110
targetClient ,
95
111
}
96
112
for _ , tt := range tests {
113
+ tt := tt
97
114
for _ , client := range clients {
115
+ client := client
98
116
t .Run (tt .name , func (t * testing.T ) {
117
+ t .Parallel ()
99
118
cli := mock .NewDepositServiceClient (client .Conn ())
100
119
request := & mock.DepositRequest {Amount : tt .amount }
101
120
response , err := cli .Deposit (context .Background (), request )
@@ -119,3 +138,39 @@ func TestDepositServer_Deposit(t *testing.T) {
119
138
}
120
139
}
121
140
}
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