balancer_conn_wrappers_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Copyright 2019 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 grpc
  19. import (
  20. "fmt"
  21. "net"
  22. "testing"
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/balancer/roundrobin"
  25. "google.golang.org/grpc/internal/balancer/stub"
  26. "google.golang.org/grpc/resolver"
  27. "google.golang.org/grpc/resolver/manual"
  28. )
  29. // TestBalancerErrorResolverPolling injects balancer errors and verifies
  30. // ResolveNow is called on the resolver with the appropriate backoff strategy
  31. // being consulted between ResolveNow calls.
  32. func (s) TestBalancerErrorResolverPolling(t *testing.T) {
  33. // The test balancer will return ErrBadResolverState iff the
  34. // ClientConnState contains no addresses.
  35. bf := stub.BalancerFuncs{
  36. UpdateClientConnState: func(_ *stub.BalancerData, s balancer.ClientConnState) error {
  37. if len(s.ResolverState.Addresses) == 0 {
  38. return balancer.ErrBadResolverState
  39. }
  40. return nil
  41. },
  42. }
  43. const balName = "BalancerErrorResolverPolling"
  44. stub.Register(balName, bf)
  45. testResolverErrorPolling(t,
  46. func(r *manual.Resolver) {
  47. // No addresses so the balancer will fail.
  48. r.CC.UpdateState(resolver.State{})
  49. }, func(r *manual.Resolver) {
  50. // UpdateState will block if ResolveNow is being called (which blocks on
  51. // rn), so call it in a goroutine. Include some address so the balancer
  52. // will be happy.
  53. go r.CC.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "x"}}})
  54. },
  55. WithDefaultServiceConfig(fmt.Sprintf(`{ "loadBalancingConfig": [{"%v": {}}] }`, balName)))
  56. }
  57. // TestRoundRobinZeroAddressesResolverPolling reports no addresses to the round
  58. // robin balancer and verifies ResolveNow is called on the resolver with the
  59. // appropriate backoff strategy being consulted between ResolveNow calls.
  60. func (s) TestRoundRobinZeroAddressesResolverPolling(t *testing.T) {
  61. // We need to start a real server or else the connecting loop will call
  62. // ResolveNow after every iteration, even after a valid resolver result is
  63. // returned.
  64. lis, err := net.Listen("tcp", "localhost:0")
  65. if err != nil {
  66. t.Fatalf("Error while listening. Err: %v", err)
  67. }
  68. defer lis.Close()
  69. s := NewServer()
  70. defer s.Stop()
  71. go s.Serve(lis)
  72. testResolverErrorPolling(t,
  73. func(r *manual.Resolver) {
  74. // No addresses so the balancer will fail.
  75. r.CC.UpdateState(resolver.State{})
  76. }, func(r *manual.Resolver) {
  77. // UpdateState will block if ResolveNow is being called (which
  78. // blocks on rn), so call it in a goroutine. Include a valid
  79. // address so the balancer will be happy.
  80. go r.CC.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})
  81. },
  82. WithDefaultServiceConfig(fmt.Sprintf(`{ "loadBalancingConfig": [{"%v": {}}] }`, roundrobin.Name)))
  83. }