gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/grpc/resolver_conn_wrapper_test.go (about) 1 /* 2 * 3 * Copyright 2017 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package grpc 20 21 import ( 22 "context" 23 "errors" 24 "gitee.com/zhaochuninhefei/gmgo/grpc/credentials/insecure" 25 "strings" 26 "testing" 27 "time" 28 29 "gitee.com/zhaochuninhefei/gmgo/grpc/balancer" 30 "gitee.com/zhaochuninhefei/gmgo/grpc/codes" 31 "gitee.com/zhaochuninhefei/gmgo/grpc/internal/balancer/stub" 32 "gitee.com/zhaochuninhefei/gmgo/grpc/resolver" 33 "gitee.com/zhaochuninhefei/gmgo/grpc/resolver/manual" 34 "gitee.com/zhaochuninhefei/gmgo/grpc/serviceconfig" 35 "gitee.com/zhaochuninhefei/gmgo/grpc/status" 36 ) 37 38 const happyBalancerName = "happy balancer" 39 40 func init() { 41 // Register a balancer that never returns an error from 42 // UpdateClientConnState, and doesn't do anything else either. 43 bf := stub.BalancerFuncs{ 44 UpdateClientConnState: func(*stub.BalancerData, balancer.ClientConnState) error { 45 return nil 46 }, 47 } 48 stub.Register(happyBalancerName, bf) 49 } 50 51 // TestResolverErrorInBuild makes the resolver.Builder call into the ClientConn 52 // during the Build call. We use two separate mutexes in the code which make 53 // sure there is no data race in this code path, and also that there is no 54 // deadlock. 55 func (s) TestResolverErrorInBuild(t *testing.T) { 56 r := manual.NewBuilderWithScheme("whatever") 57 r.InitialState(resolver.State{ServiceConfig: &serviceconfig.ParseResult{Err: errors.New("resolver build err")}}) 58 59 // WithInsecure() is deprecated, use WithTransportCredentials and insecure.NewCredentials() instead. 60 //cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r)) 61 cc, err := Dial(r.Scheme()+":///test.server", WithTransportCredentials(insecure.NewCredentials()), WithResolvers(r)) 62 if err != nil { 63 t.Fatalf("Dial(_, _) = _, %v; want _, nil", err) 64 } 65 defer func(cc *ClientConn) { 66 _ = cc.Close() 67 }(cc) 68 69 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 70 defer cancel() 71 var dummy int 72 const wantMsg = "error parsing service config" 73 const wantCode = codes.Unavailable 74 if err := cc.Invoke(ctx, "/foo/bar", &dummy, &dummy); status.Code(err) != wantCode || !strings.Contains(status.Convert(err).Message(), wantMsg) { 75 t.Fatalf("cc.Invoke(_, _, _, _) = %v; want status.Code()==%v, status.Message() contains %q", err, wantCode, wantMsg) 76 } 77 } 78 79 func (s) TestServiceConfigErrorRPC(t *testing.T) { 80 r := manual.NewBuilderWithScheme("whatever") 81 82 // WithInsecure() is deprecated, use WithTransportCredentials and insecure.NewCredentials() instead. 83 //cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r)) 84 cc, err := Dial(r.Scheme()+":///test.server", WithTransportCredentials(insecure.NewCredentials()), WithResolvers(r)) 85 if err != nil { 86 t.Fatalf("Dial(_, _) = _, %v; want _, nil", err) 87 } 88 defer func(cc *ClientConn) { 89 _ = cc.Close() 90 }(cc) 91 badsc := r.CC.ParseServiceConfig("bad config") 92 r.UpdateState(resolver.State{ServiceConfig: badsc}) 93 94 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 95 defer cancel() 96 var dummy int 97 const wantMsg = "error parsing service config" 98 const wantCode = codes.Unavailable 99 if err := cc.Invoke(ctx, "/foo/bar", &dummy, &dummy); status.Code(err) != wantCode || !strings.Contains(status.Convert(err).Message(), wantMsg) { 100 t.Fatalf("cc.Invoke(_, _, _, _) = %v; want status.Code()==%v, status.Message() contains %q", err, wantCode, wantMsg) 101 } 102 }