weightedroundrobin_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. *
  3. * Copyright 2020 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 weightedroundrobin
  19. import (
  20. "testing"
  21. "github.com/google/go-cmp/cmp"
  22. "google.golang.org/grpc/attributes"
  23. "google.golang.org/grpc/resolver"
  24. )
  25. func TestAddrInfoToAndFromAttributes(t *testing.T) {
  26. tests := []struct {
  27. desc string
  28. inputAddrInfo AddrInfo
  29. inputAttributes *attributes.Attributes
  30. wantAddrInfo AddrInfo
  31. }{
  32. {
  33. desc: "empty attributes",
  34. inputAddrInfo: AddrInfo{Weight: 100},
  35. inputAttributes: nil,
  36. wantAddrInfo: AddrInfo{Weight: 100},
  37. },
  38. {
  39. desc: "non-empty attributes",
  40. inputAddrInfo: AddrInfo{Weight: 100},
  41. inputAttributes: attributes.New("foo", "bar"),
  42. wantAddrInfo: AddrInfo{Weight: 100},
  43. },
  44. {
  45. desc: "addrInfo not present in empty attributes",
  46. inputAddrInfo: AddrInfo{},
  47. inputAttributes: nil,
  48. wantAddrInfo: AddrInfo{},
  49. },
  50. {
  51. desc: "addrInfo not present in non-empty attributes",
  52. inputAddrInfo: AddrInfo{},
  53. inputAttributes: attributes.New("foo", "bar"),
  54. wantAddrInfo: AddrInfo{},
  55. },
  56. }
  57. for _, test := range tests {
  58. t.Run(test.desc, func(t *testing.T) {
  59. addr := resolver.Address{Attributes: test.inputAttributes}
  60. addr = SetAddrInfo(addr, test.inputAddrInfo)
  61. gotAddrInfo := GetAddrInfo(addr)
  62. if !cmp.Equal(gotAddrInfo, test.wantAddrInfo) {
  63. t.Errorf("gotAddrInfo: %v, wantAddrInfo: %v", gotAddrInfo, test.wantAddrInfo)
  64. }
  65. })
  66. }
  67. }
  68. func TestGetAddInfoEmpty(t *testing.T) {
  69. addr := resolver.Address{}
  70. gotAddrInfo := GetAddrInfo(addr)
  71. wantAddrInfo := AddrInfo{}
  72. if !cmp.Equal(gotAddrInfo, wantAddrInfo) {
  73. t.Errorf("gotAddrInfo: %v, wantAddrInfo: %v", gotAddrInfo, wantAddrInfo)
  74. }
  75. }