gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/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  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"gitee.com/ks-custle/core-gm/grpc/balancer"
    29  	"gitee.com/ks-custle/core-gm/grpc/codes"
    30  	"gitee.com/ks-custle/core-gm/grpc/internal/balancer/stub"
    31  	"gitee.com/ks-custle/core-gm/grpc/resolver"
    32  	"gitee.com/ks-custle/core-gm/grpc/resolver/manual"
    33  	"gitee.com/ks-custle/core-gm/grpc/serviceconfig"
    34  	"gitee.com/ks-custle/core-gm/grpc/status"
    35  )
    36  
    37  const happyBalancerName = "happy balancer"
    38  
    39  func init() {
    40  	// Register a balancer that never returns an error from
    41  	// UpdateClientConnState, and doesn't do anything else either.
    42  	bf := stub.BalancerFuncs{
    43  		UpdateClientConnState: func(*stub.BalancerData, balancer.ClientConnState) error {
    44  			return nil
    45  		},
    46  	}
    47  	stub.Register(happyBalancerName, bf)
    48  }
    49  
    50  // TestResolverErrorInBuild makes the resolver.Builder call into the ClientConn
    51  // during the Build call. We use two separate mutexes in the code which make
    52  // sure there is no data race in this code path, and also that there is no
    53  // deadlock.
    54  func (s) TestResolverErrorInBuild(t *testing.T) {
    55  	r := manual.NewBuilderWithScheme("whatever")
    56  	r.InitialState(resolver.State{ServiceConfig: &serviceconfig.ParseResult{Err: errors.New("resolver build err")}})
    57  
    58  	cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
    59  	if err != nil {
    60  		t.Fatalf("Dial(_, _) = _, %v; want _, nil", err)
    61  	}
    62  	defer cc.Close()
    63  
    64  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    65  	defer cancel()
    66  	var dummy int
    67  	const wantMsg = "error parsing service config"
    68  	const wantCode = codes.Unavailable
    69  	if err := cc.Invoke(ctx, "/foo/bar", &dummy, &dummy); status.Code(err) != wantCode || !strings.Contains(status.Convert(err).Message(), wantMsg) {
    70  		t.Fatalf("cc.Invoke(_, _, _, _) = %v; want status.Code()==%v, status.Message() contains %q", err, wantCode, wantMsg)
    71  	}
    72  }
    73  
    74  func (s) TestServiceConfigErrorRPC(t *testing.T) {
    75  	r := manual.NewBuilderWithScheme("whatever")
    76  
    77  	cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
    78  	if err != nil {
    79  		t.Fatalf("Dial(_, _) = _, %v; want _, nil", err)
    80  	}
    81  	defer cc.Close()
    82  	badsc := r.CC.ParseServiceConfig("bad config")
    83  	r.UpdateState(resolver.State{ServiceConfig: badsc})
    84  
    85  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    86  	defer cancel()
    87  	var dummy int
    88  	const wantMsg = "error parsing service config"
    89  	const wantCode = codes.Unavailable
    90  	if err := cc.Invoke(ctx, "/foo/bar", &dummy, &dummy); status.Code(err) != wantCode || !strings.Contains(status.Convert(err).Message(), wantMsg) {
    91  		t.Fatalf("cc.Invoke(_, _, _, _) = %v; want status.Code()==%v, status.Message() contains %q", err, wantCode, wantMsg)
    92  	}
    93  }