github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/resolver/map_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  	"fmt"
    23  	"sort"
    24  	"testing"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"github.com/hxx258456/ccgo/grpc/attributes"
    28  )
    29  
    30  // Note: each address is different from addr1 by one value.  addr7 matches
    31  // addr1, since the only difference is BalancerAttributes, which are not
    32  // compared.
    33  var (
    34  	addr1 = Address{Addr: "a1", Attributes: attributes.New("a1", 3), ServerName: "s1"}
    35  	addr2 = Address{Addr: "a2", Attributes: attributes.New("a1", 3), ServerName: "s1"}
    36  	addr3 = Address{Addr: "a1", Attributes: attributes.New("a2", 3), ServerName: "s1"}
    37  	addr4 = Address{Addr: "a1", Attributes: attributes.New("a1", 2), ServerName: "s1"}
    38  	addr5 = Address{Addr: "a1", Attributes: attributes.New("a1", "3"), ServerName: "s1"}
    39  	addr6 = Address{Addr: "a1", Attributes: attributes.New("a1", 3), ServerName: "s2"}
    40  	addr7 = Address{Addr: "a1", Attributes: attributes.New("a1", 3), ServerName: "s1", BalancerAttributes: attributes.New("xx", 3)}
    41  )
    42  
    43  func (s) TestAddressMap_Length(t *testing.T) {
    44  	addrMap := NewAddressMap()
    45  	if got := addrMap.Len(); got != 0 {
    46  		t.Fatalf("addrMap.Len() = %v; want 0", got)
    47  	}
    48  	for i := 0; i < 10; i++ {
    49  		addrMap.Set(addr1, nil)
    50  		if got, want := addrMap.Len(), 1; got != want {
    51  			t.Fatalf("addrMap.Len() = %v; want %v", got, want)
    52  		}
    53  		addrMap.Set(addr7, nil) // aliases addr1
    54  	}
    55  	for i := 0; i < 10; i++ {
    56  		addrMap.Set(addr2, nil)
    57  		if got, want := addrMap.Len(), 2; got != want {
    58  			t.Fatalf("addrMap.Len() = %v; want %v", got, want)
    59  		}
    60  	}
    61  }
    62  
    63  func (s) TestAddressMap_Get(t *testing.T) {
    64  	addrMap := NewAddressMap()
    65  	addrMap.Set(addr1, 1)
    66  
    67  	if got, ok := addrMap.Get(addr2); ok || got != nil {
    68  		t.Fatalf("addrMap.Get(addr1) = %v, %v; want nil, false", got, ok)
    69  	}
    70  
    71  	addrMap.Set(addr2, 2)
    72  	addrMap.Set(addr3, 3)
    73  	addrMap.Set(addr4, 4)
    74  	addrMap.Set(addr5, 5)
    75  	addrMap.Set(addr6, 6)
    76  	addrMap.Set(addr7, 7) // aliases addr1
    77  	if got, ok := addrMap.Get(addr1); !ok || got.(int) != 7 {
    78  		t.Fatalf("addrMap.Get(addr1) = %v, %v; want %v, true", got, ok, 7)
    79  	}
    80  	if got, ok := addrMap.Get(addr2); !ok || got.(int) != 2 {
    81  		t.Fatalf("addrMap.Get(addr2) = %v, %v; want %v, true", got, ok, 2)
    82  	}
    83  	if got, ok := addrMap.Get(addr3); !ok || got.(int) != 3 {
    84  		t.Fatalf("addrMap.Get(addr3) = %v, %v; want %v, true", got, ok, 3)
    85  	}
    86  	if got, ok := addrMap.Get(addr4); !ok || got.(int) != 4 {
    87  		t.Fatalf("addrMap.Get(addr4) = %v, %v; want %v, true", got, ok, 4)
    88  	}
    89  	if got, ok := addrMap.Get(addr5); !ok || got.(int) != 5 {
    90  		t.Fatalf("addrMap.Get(addr5) = %v, %v; want %v, true", got, ok, 5)
    91  	}
    92  	if got, ok := addrMap.Get(addr6); !ok || got.(int) != 6 {
    93  		t.Fatalf("addrMap.Get(addr6) = %v, %v; want %v, true", got, ok, 6)
    94  	}
    95  	if got, ok := addrMap.Get(addr7); !ok || got.(int) != 7 {
    96  		t.Fatalf("addrMap.Get(addr7) = %v, %v; want %v, true", got, ok, 7)
    97  	}
    98  }
    99  
   100  func (s) TestAddressMap_Delete(t *testing.T) {
   101  	addrMap := NewAddressMap()
   102  	addrMap.Set(addr1, 1)
   103  	addrMap.Set(addr2, 2)
   104  	if got, want := addrMap.Len(), 2; got != want {
   105  		t.Fatalf("addrMap.Len() = %v; want %v", got, want)
   106  	}
   107  	addrMap.Delete(addr3)
   108  	addrMap.Delete(addr4)
   109  	addrMap.Delete(addr5)
   110  	addrMap.Delete(addr6)
   111  	addrMap.Delete(addr7) // aliases addr1
   112  	if got, ok := addrMap.Get(addr1); ok || got != nil {
   113  		t.Fatalf("addrMap.Get(addr1) = %v, %v; want nil, false", got, ok)
   114  	}
   115  	if got, ok := addrMap.Get(addr7); ok || got != nil {
   116  		t.Fatalf("addrMap.Get(addr7) = %v, %v; want nil, false", got, ok)
   117  	}
   118  	if got, ok := addrMap.Get(addr2); !ok || got.(int) != 2 {
   119  		t.Fatalf("addrMap.Get(addr2) = %v, %v; want %v, true", got, ok, 2)
   120  	}
   121  }
   122  
   123  func (s) TestAddressMap_Keys(t *testing.T) {
   124  	addrMap := NewAddressMap()
   125  	addrMap.Set(addr1, 1)
   126  	addrMap.Set(addr2, 2)
   127  	addrMap.Set(addr3, 3)
   128  	addrMap.Set(addr4, 4)
   129  	addrMap.Set(addr5, 5)
   130  	addrMap.Set(addr6, 6)
   131  	addrMap.Set(addr7, 7) // aliases addr1
   132  
   133  	want := []Address{addr1, addr2, addr3, addr4, addr5, addr6}
   134  	got := addrMap.Keys()
   135  	if d := cmp.Diff(want, got, cmp.Transformer("sort", func(in []Address) []Address {
   136  		out := append([]Address(nil), in...)
   137  		sort.Slice(out, func(i, j int) bool { return fmt.Sprint(out[i]) < fmt.Sprint(out[j]) })
   138  		return out
   139  	})); d != "" {
   140  		t.Fatalf("addrMap.Keys returned unexpected elements (-want, +got):\n%v", d)
   141  	}
   142  }