balancer_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 base
  19. import (
  20. "testing"
  21. "google.golang.org/grpc/attributes"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/connectivity"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. type testClientConn struct {
  27. balancer.ClientConn
  28. newSubConn func([]resolver.Address, balancer.NewSubConnOptions) (balancer.SubConn, error)
  29. }
  30. func (c *testClientConn) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  31. return c.newSubConn(addrs, opts)
  32. }
  33. func (c *testClientConn) UpdateState(balancer.State) {}
  34. type testSubConn struct{}
  35. func (sc *testSubConn) UpdateAddresses(addresses []resolver.Address) {}
  36. func (sc *testSubConn) Connect() {}
  37. func (sc *testSubConn) GetOrBuildProducer(balancer.ProducerBuilder) (balancer.Producer, func()) {
  38. return nil, nil
  39. }
  40. // testPickBuilder creates balancer.Picker for test.
  41. type testPickBuilder struct {
  42. validate func(info PickerBuildInfo)
  43. }
  44. func (p *testPickBuilder) Build(info PickerBuildInfo) balancer.Picker {
  45. p.validate(info)
  46. return nil
  47. }
  48. func TestBaseBalancerReserveAttributes(t *testing.T) {
  49. var v = func(info PickerBuildInfo) {
  50. for _, sc := range info.ReadySCs {
  51. if sc.Address.Addr == "1.1.1.1" {
  52. if sc.Address.Attributes == nil {
  53. t.Errorf("in picker.validate, got address %+v with nil attributes, want not nil", sc.Address)
  54. }
  55. foo, ok := sc.Address.Attributes.Value("foo").(string)
  56. if !ok || foo != "2233niang" {
  57. t.Errorf("in picker.validate, got address[1.1.1.1] with invalid attributes value %v, want 2233niang", sc.Address.Attributes.Value("foo"))
  58. }
  59. } else if sc.Address.Addr == "2.2.2.2" {
  60. if sc.Address.Attributes != nil {
  61. t.Error("in b.subConns, got address[2.2.2.2] with not nil attributes, want nil")
  62. }
  63. }
  64. }
  65. }
  66. pickBuilder := &testPickBuilder{validate: v}
  67. b := (&baseBuilder{pickerBuilder: pickBuilder}).Build(&testClientConn{
  68. newSubConn: func(addrs []resolver.Address, _ balancer.NewSubConnOptions) (balancer.SubConn, error) {
  69. return &testSubConn{}, nil
  70. },
  71. }, balancer.BuildOptions{}).(*baseBalancer)
  72. b.UpdateClientConnState(balancer.ClientConnState{
  73. ResolverState: resolver.State{
  74. Addresses: []resolver.Address{
  75. {Addr: "1.1.1.1", Attributes: attributes.New("foo", "2233niang")},
  76. {Addr: "2.2.2.2", Attributes: nil},
  77. },
  78. },
  79. })
  80. for sc := range b.scStates {
  81. b.UpdateSubConnState(sc, balancer.SubConnState{ConnectivityState: connectivity.Ready, ConnectionError: nil})
  82. }
  83. }