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

     1  /*
     2   *
     3   * Copyright 2021 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 resolver
    20  
    21  import (
    22  	"testing"
    23  
    24  	"google.golang.org/grpc/internal/grpctest"
    25  )
    26  
    27  type s struct {
    28  	grpctest.Tester
    29  }
    30  
    31  func Test(t *testing.T) {
    32  	grpctest.RunSubTests(t, s{})
    33  }
    34  
    35  // TestValidateEndpoints tests different scenarios of resolver addresses being
    36  // validated by the ValidateEndpoint helper.
    37  func (s) TestValidateEndpoints(t *testing.T) {
    38  	addr1 := Address{Addr: "addr1"}
    39  	addr2 := Address{Addr: "addr2"}
    40  	addr3 := Address{Addr: "addr3"}
    41  	addr4 := Address{Addr: "addr4"}
    42  	tests := []struct {
    43  		name      string
    44  		endpoints []Endpoint
    45  		wantErr   bool
    46  	}{
    47  		{
    48  			name: "duplicate-address-across-endpoints",
    49  			endpoints: []Endpoint{
    50  				{Addresses: []Address{addr1}},
    51  				{Addresses: []Address{addr1}},
    52  			},
    53  			wantErr: false,
    54  		},
    55  		{
    56  			name: "duplicate-address-same-endpoint",
    57  			endpoints: []Endpoint{
    58  				{Addresses: []Address{addr1, addr1}},
    59  			},
    60  			wantErr: false,
    61  		},
    62  		{
    63  			name: "duplicate-address-across-endpoints-plural-addresses",
    64  			endpoints: []Endpoint{
    65  				{Addresses: []Address{addr1, addr2, addr3}},
    66  				{Addresses: []Address{addr3, addr4}},
    67  			},
    68  			wantErr: false,
    69  		},
    70  		{
    71  			name: "no-shared-addresses",
    72  			endpoints: []Endpoint{
    73  				{Addresses: []Address{addr1, addr2}},
    74  				{Addresses: []Address{addr3, addr4}},
    75  			},
    76  			wantErr: false,
    77  		},
    78  		{
    79  			name: "endpoint-with-no-addresses",
    80  			endpoints: []Endpoint{
    81  				{Addresses: []Address{addr1, addr2}},
    82  				{Addresses: []Address{}},
    83  			},
    84  			wantErr: false,
    85  		},
    86  		{
    87  			name:      "empty-endpoints-list",
    88  			endpoints: []Endpoint{},
    89  			wantErr:   true,
    90  		},
    91  		{
    92  			name:      "endpoint-list-with-no-addresses",
    93  			endpoints: []Endpoint{{}, {}},
    94  			wantErr:   true,
    95  		},
    96  	}
    97  	for _, test := range tests {
    98  		t.Run(test.name, func(t *testing.T) {
    99  			err := ValidateEndpoints(test.endpoints)
   100  			if (err != nil) != test.wantErr {
   101  				t.Fatalf("ValidateEndpoints() wantErr: %v, got: %v", test.wantErr, err)
   102  			}
   103  		})
   104  	}
   105  }