map_test.go 5.2 KB

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