google.golang.org/grpc@v1.72.2/resolver/manual/manual_test.go (about)

     1  /*
     2   *
     3   * Copyright 2023 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 manual_test
    20  
    21  import (
    22  	"errors"
    23  	"testing"
    24  
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/credentials/insecure"
    27  	"google.golang.org/grpc/resolver"
    28  	"google.golang.org/grpc/resolver/manual"
    29  )
    30  
    31  func TestResolver(t *testing.T) {
    32  	r := manual.NewBuilderWithScheme("whatever")
    33  	r.InitialState(resolver.State{
    34  		Addresses: []resolver.Address{
    35  			{Addr: "address"},
    36  		},
    37  	})
    38  
    39  	t.Run("cc_panics", func(t *testing.T) {
    40  		defer func() {
    41  			want := "Manual resolver instance has not yet been built."
    42  			if r := recover(); r != want {
    43  				t.Errorf("expected panic %q, got %q", want, r)
    44  			}
    45  		}()
    46  		r.CC()
    47  	})
    48  
    49  	t.Run("happy_path", func(t *testing.T) {
    50  		cc, err := grpc.NewClient("whatever://localhost",
    51  			grpc.WithTransportCredentials(insecure.NewCredentials()),
    52  			grpc.WithResolvers(r))
    53  		if err != nil {
    54  			t.Errorf("grpc.NewClient() failed: %v", err)
    55  		}
    56  		defer cc.Close()
    57  		cc.Connect()
    58  		r.UpdateState(resolver.State{Addresses: []resolver.Address{
    59  			{Addr: "ok"},
    60  		}})
    61  		r.CC().ReportError(errors.New("example"))
    62  	})
    63  }