google.golang.org/grpc@v1.72.2/internal/balancer/weight/weight_test.go (about)

     1  /*
     2   *
     3   * Copyright 2025 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 weight_test
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"google.golang.org/grpc/attributes"
    26  	"google.golang.org/grpc/internal/balancer/weight"
    27  	"google.golang.org/grpc/internal/grpctest"
    28  	"google.golang.org/grpc/resolver"
    29  )
    30  
    31  type s struct {
    32  	grpctest.Tester
    33  }
    34  
    35  func Test(t *testing.T) {
    36  	grpctest.RunSubTests(t, s{})
    37  }
    38  
    39  func (s) TestEndpointInfoToAndFromAttributes(t *testing.T) {
    40  	tests := []struct {
    41  		desc              string
    42  		inputEndpointInfo weight.EndpointInfo
    43  		inputAttributes   *attributes.Attributes
    44  		wantEndpointInfo  weight.EndpointInfo
    45  	}{
    46  		{
    47  			desc:              "empty_attributes",
    48  			inputEndpointInfo: weight.EndpointInfo{Weight: 100},
    49  			inputAttributes:   nil,
    50  			wantEndpointInfo:  weight.EndpointInfo{Weight: 100},
    51  		},
    52  		{
    53  			desc:              "non-empty_attributes",
    54  			inputEndpointInfo: weight.EndpointInfo{Weight: 100},
    55  			inputAttributes:   attributes.New("foo", "bar"),
    56  			wantEndpointInfo:  weight.EndpointInfo{Weight: 100},
    57  		},
    58  		{
    59  			desc:              "endpointInfo_not_present_in_empty_attributes",
    60  			inputEndpointInfo: weight.EndpointInfo{},
    61  			inputAttributes:   nil,
    62  			wantEndpointInfo:  weight.EndpointInfo{},
    63  		},
    64  		{
    65  			desc:              "endpointInfo_not_present_in_non-empty_attributes",
    66  			inputEndpointInfo: weight.EndpointInfo{},
    67  			inputAttributes:   attributes.New("foo", "bar"),
    68  			wantEndpointInfo:  weight.EndpointInfo{},
    69  		},
    70  	}
    71  
    72  	for _, test := range tests {
    73  		t.Run(test.desc, func(t *testing.T) {
    74  			endpoint := resolver.Endpoint{Attributes: test.inputAttributes}
    75  			endpoint = weight.Set(endpoint, test.inputEndpointInfo)
    76  			gotEndpointInfo := weight.FromEndpoint(endpoint)
    77  			if !cmp.Equal(gotEndpointInfo, test.wantEndpointInfo) {
    78  				t.Errorf("gotEndpointInfo: %v, wantEndpointInfo: %v", gotEndpointInfo, test.wantEndpointInfo)
    79  			}
    80  
    81  		})
    82  	}
    83  }