18
18
19
19
import static org .mockito .AdditionalAnswers .delegatesTo ;
20
20
import static org .mockito .ArgumentMatchers .eq ;
21
+ import static org .mockito .Mockito .atLeast ;
21
22
import static org .mockito .Mockito .mock ;
23
+ import static org .mockito .Mockito .verify ;
24
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
22
25
23
26
import com .google .common .collect .Maps ;
24
27
import io .grpc .Attributes ;
32
35
import io .grpc .LoadBalancer .Subchannel ;
33
36
import io .grpc .LoadBalancer .SubchannelPicker ;
34
37
import io .grpc .LoadBalancer .SubchannelStateListener ;
38
+ import io .grpc .SynchronizationContext ;
39
+ import io .grpc .internal .FakeClock ;
35
40
import io .grpc .internal .PickFirstLoadBalancerProvider ;
36
41
import java .net .SocketAddress ;
37
42
import java .util .Collections ;
38
43
import java .util .HashMap ;
39
44
import java .util .List ;
40
45
import java .util .Map ;
46
+ import java .util .concurrent .ScheduledExecutorService ;
41
47
import org .mockito .ArgumentCaptor ;
42
48
import org .mockito .InOrder ;
43
49
@@ -60,9 +66,26 @@ public abstract class AbstractTestHelper extends ForwardingLoadBalancerHelper {
60
66
protected final Map <Subchannel , Subchannel > realToMockSubChannelMap = new HashMap <>();
61
67
private final Map <Subchannel , SubchannelStateListener > subchannelStateListeners =
62
68
Maps .newLinkedHashMap ();
69
+ private final FakeClock fakeClock ;
70
+ private final SynchronizationContext syncContext ;
63
71
64
72
public abstract Map <List <EquivalentAddressGroup >, Subchannel > getSubchannelMap ();
65
73
74
+ public AbstractTestHelper () {
75
+ this (new FakeClock (), new SynchronizationContext (new Thread .UncaughtExceptionHandler () {
76
+ @ Override
77
+ public void uncaughtException (Thread t , Throwable e ) {
78
+ throw new RuntimeException (e );
79
+ }
80
+ }));
81
+ }
82
+
83
+ public AbstractTestHelper (FakeClock fakeClock , SynchronizationContext syncContext ) {
84
+ super ();
85
+ this .fakeClock = fakeClock ;
86
+ this .syncContext = syncContext ;
87
+ }
88
+
66
89
public Map <Subchannel , Subchannel > getMockToRealSubChannelMap () {
67
90
return mockToRealSubChannelMap ;
68
91
}
@@ -79,6 +102,18 @@ public Map<Subchannel, SubchannelStateListener> getSubchannelStateListeners() {
79
102
return subchannelStateListeners ;
80
103
}
81
104
105
+ public static final FakeClock .TaskFilter NOT_START_NEXT_CONNECTION =
106
+ new FakeClock .TaskFilter () {
107
+ @ Override
108
+ public boolean shouldAccept (Runnable command ) {
109
+ return !command .toString ().contains ("StartNextConnection" );
110
+ }
111
+ };
112
+
113
+ public static int getNumFilteredPendingTasks (FakeClock fakeClock ) {
114
+ return fakeClock .getPendingTasks (NOT_START_NEXT_CONNECTION ).size ();
115
+ }
116
+
82
117
public void deliverSubchannelState (Subchannel subchannel , ConnectivityStateInfo newState ) {
83
118
Subchannel realSc = getMockToRealSubChannelMap ().get (subchannel );
84
119
if (realSc == null ) {
@@ -128,6 +163,16 @@ public void setChannel(Subchannel subchannel, Channel channel) {
128
163
((TestSubchannel )subchannel ).channel = channel ;
129
164
}
130
165
166
+ @ Override
167
+ public SynchronizationContext getSynchronizationContext () {
168
+ return syncContext ;
169
+ }
170
+
171
+ @ Override
172
+ public ScheduledExecutorService getScheduledExecutorService () {
173
+ return fakeClock .getScheduledExecutorService ();
174
+ }
175
+
131
176
@ Override
132
177
public String toString () {
133
178
return "Test Helper" ;
@@ -148,6 +193,17 @@ public static void refreshInvokedAndUpdateBS(InOrder inOrder, ConnectivityState
148
193
}
149
194
}
150
195
196
+ public static void verifyNoMoreMeaningfulInteractions (Helper helper ) {
197
+ verify (helper , atLeast (0 )).getSynchronizationContext ();
198
+ verify (helper , atLeast (0 )).getScheduledExecutorService ();
199
+ verifyNoMoreInteractions (helper );
200
+ }
201
+
202
+ public static void verifyNoMoreMeaningfulInteractions (Helper helper , InOrder inOrder ) {
203
+ inOrder .verify (helper , atLeast (0 )).getSynchronizationContext ();
204
+ inOrder .verify (helper , atLeast (0 )).getScheduledExecutorService ();
205
+ inOrder .verifyNoMoreInteractions ();
206
+ }
151
207
152
208
protected class TestSubchannel extends ForwardingSubchannel {
153
209
CreateSubchannelArgs args ;
0 commit comments