TestSubConnEmpty tests that removing all addresses from a SubConn and then re-adding them does not cause a panic and properly reconnects.
(t *testing.T)
| 46 | // TestSubConnEmpty tests that removing all addresses from a SubConn and then |
| 47 | // re-adding them does not cause a panic and properly reconnects. |
| 48 | func (s) TestSubConnEmpty(t *testing.T) { |
| 49 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 50 | defer cancel() |
| 51 | |
| 52 | // sc is the one SubConn used throughout the test. Created on demand and |
| 53 | // re-used on every update. |
| 54 | var sc balancer.SubConn |
| 55 | |
| 56 | // Simple custom balancer that sets the address list to empty if the |
| 57 | // resolver produces no addresses. Pickfirst, by default, will remove the |
| 58 | // SubConn in this case instead. |
| 59 | bal := stub.BalancerFuncs{ |
| 60 | UpdateClientConnState: func(d *stub.BalancerData, ccs balancer.ClientConnState) error { |
| 61 | if sc == nil { |
| 62 | var err error |
| 63 | sc, err = d.ClientConn.NewSubConn(ccs.ResolverState.Addresses, balancer.NewSubConnOptions{ |
| 64 | StateListener: func(state balancer.SubConnState) { |
| 65 | switch state.ConnectivityState { |
| 66 | case connectivity.Ready: |
| 67 | d.ClientConn.UpdateState(balancer.State{ |
| 68 | ConnectivityState: connectivity.Ready, |
| 69 | Picker: &tsccPicker{sc: sc}, |
| 70 | }) |
| 71 | case connectivity.TransientFailure: |
| 72 | d.ClientConn.UpdateState(balancer.State{ |
| 73 | ConnectivityState: connectivity.TransientFailure, |
| 74 | Picker: base.NewErrPicker(fmt.Errorf("error connecting: %v", state.ConnectionError)), |
| 75 | }) |
| 76 | } |
| 77 | }, |
| 78 | }) |
| 79 | if err != nil { |
| 80 | t.Errorf("error creating initial subconn: %v", err) |
| 81 | } |
| 82 | } else { |
| 83 | d.ClientConn.UpdateAddresses(sc, ccs.ResolverState.Addresses) |
| 84 | } |
| 85 | sc.Connect() |
| 86 | |
| 87 | if len(ccs.ResolverState.Addresses) == 0 { |
| 88 | d.ClientConn.UpdateState(balancer.State{ |
| 89 | ConnectivityState: connectivity.TransientFailure, |
| 90 | Picker: base.NewErrPicker(errors.New("no addresses")), |
| 91 | }) |
| 92 | } else { |
| 93 | d.ClientConn.UpdateState(balancer.State{ |
| 94 | ConnectivityState: connectivity.Connecting, |
| 95 | Picker: &tsccPicker{sc: sc}, |
| 96 | }) |
| 97 | } |
| 98 | return nil |
| 99 | }, |
| 100 | } |
| 101 | stub.Register("tscc", bal) |
| 102 | |
| 103 | // Start the stub server with our stub balancer. |
| 104 | ss := &stubserver.StubServer{ |
| 105 | EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
nothing calls this directly
no test coverage detected