aboutsummaryrefslogtreecommitdiff
path: root/util/src/testFixtures/java/io/grpc/util/AbstractTestHelper.java
blob: 2afb133877b511f61603419ad34f40b30cd38fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright 2023 The gRPC Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.grpc.util;

import static org.mockito.AdditionalAnswers.delegatesTo;
import static org.mockito.Mockito.mock;

import com.google.common.collect.Maps;
import io.grpc.Attributes;
import io.grpc.Channel;
import io.grpc.ChannelLogger;
import io.grpc.ConnectivityState;
import io.grpc.ConnectivityStateInfo;
import io.grpc.EquivalentAddressGroup;
import io.grpc.LoadBalancer.CreateSubchannelArgs;
import io.grpc.LoadBalancer.Helper;
import io.grpc.LoadBalancer.Subchannel;
import io.grpc.LoadBalancer.SubchannelPicker;
import io.grpc.LoadBalancer.SubchannelStateListener;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A real class that can be used as a delegate of a mock Helper to provide more real representation
 * and track the subchannels as is needed with petiole policies where the subchannels are no
 * longer direct children of the loadbalancer.
 * <br>
 * To use it replace <br>
 * \@mock Helper mockHelper<br>
 * with<br>
 * <p>Helper mockHelper = mock(Helper.class, delegatesTo(new TestHelper()));</p>
 * <br>
 * TestHelper will need to define accessors for the maps that information is store within as
 * those maps need to be defined in the Test class.
 */
public abstract class AbstractTestHelper extends ForwardingLoadBalancerHelper {

  private final Map<Subchannel, Subchannel> mockToRealSubChannelMap = new HashMap<>();
  private final Map<Subchannel, SubchannelStateListener> subchannelStateListeners =
      Maps.newLinkedHashMap();

  public abstract Map<List<EquivalentAddressGroup>, Subchannel> getSubchannelMap();

  public Map<Subchannel, Subchannel> getMockToRealSubChannelMap() {
    return mockToRealSubChannelMap;
  }

  public Subchannel getRealForMockSubChannel(Subchannel mock) {
    Subchannel realSc = getMockToRealSubChannelMap().get(mock);
    if (realSc == null) {
      realSc = mock;
    }
    return realSc;
  }

  public Map<Subchannel, SubchannelStateListener> getSubchannelStateListeners() {
    return subchannelStateListeners;
  }

  public void deliverSubchannelState(Subchannel subchannel, ConnectivityStateInfo newState) {
    Subchannel realSc = getMockToRealSubChannelMap().get(subchannel);
    if (realSc == null) {
      realSc = subchannel;
    }
    SubchannelStateListener listener = getSubchannelStateListeners().get(realSc);
    if (listener == null) {
      throw new IllegalArgumentException("subchannel does not have a matching listener");
    }
    listener.onSubchannelState(newState);
  }

  @Override
  public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
    // do nothing, should have been done in the wrapper helpers
  }

  @Override
  protected Helper delegate() {
    throw new UnsupportedOperationException("This helper class is only for use in this test");
  }

  @Override
  public Subchannel createSubchannel(CreateSubchannelArgs args) {
    Subchannel subchannel = getSubchannelMap().get(args.getAddresses());
    if (subchannel == null) {
      TestSubchannel delegate = new TestSubchannel(args);
      subchannel = mock(Subchannel.class, delegatesTo(delegate));
      getSubchannelMap().put(args.getAddresses(), subchannel);
      getMockToRealSubChannelMap().put(subchannel, delegate);
    }

    return subchannel;
  }

  @Override
  public void refreshNameResolution() {
    // no-op
  }

  public void setChannel(Subchannel subchannel, Channel channel) {
    ((TestSubchannel)subchannel).channel = channel;
  }

  @Override
  public String toString() {
    return "Test Helper";
  }

  private class TestSubchannel extends ForwardingSubchannel {
    CreateSubchannelArgs args;
    Channel channel;

    public TestSubchannel(CreateSubchannelArgs args) {
      this.args = args;
    }

    @Override
    protected Subchannel delegate() {
      throw new UnsupportedOperationException("Only to be used in tests");
    }

    @Override
    public List<EquivalentAddressGroup> getAllAddresses() {
      return args.getAddresses();
    }

    @Override
    public Attributes getAttributes() {
      return args.getAttributes();
    }

    @Override
    public void requestConnection() {
      // Ignore, we will manually update state
    }

    @Override
    public void updateAddresses(List<EquivalentAddressGroup> addrs) {
      if (args.getAddresses().equals(addrs)) {
        return; // no changes so it's a no-op
      }

      List<EquivalentAddressGroup> oldAddrs = args.getAddresses();
      Subchannel oldTarget = getSubchannelMap().get(oldAddrs);

      this.args = args.toBuilder().setAddresses(addrs).build();
      getSubchannelMap().put(addrs, oldTarget);
      getSubchannelMap().remove(oldAddrs);
    }

    @Override
    public void start(SubchannelStateListener listener) {
      getSubchannelStateListeners().put(this, listener);
    }

    @Override
    public void shutdown() {
      getSubchannelStateListeners().remove(this);
      for (EquivalentAddressGroup eag : getAllAddresses()) {
        getSubchannelMap().remove(Collections.singletonList(eag));
      }
    }

    @Override
    public Channel asChannel() {
      return channel;
    }

    @Override
    public ChannelLogger getChannelLogger() {
      return mock(ChannelLogger.class);
    }

    @Override
    public String toString() {
      return "Mock Subchannel" + args.toString();
    }
  }
}