1818
1919import static org .mockito .AdditionalAnswers .delegatesTo ;
2020import static org .mockito .ArgumentMatchers .eq ;
21+ import static org .mockito .Mockito .atLeast ;
2122import static org .mockito .Mockito .mock ;
23+ import static org .mockito .Mockito .verify ;
24+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
2225
2326import com .google .common .collect .Maps ;
2427import io .grpc .Attributes ;
3235import io .grpc .LoadBalancer .Subchannel ;
3336import io .grpc .LoadBalancer .SubchannelPicker ;
3437import io .grpc .LoadBalancer .SubchannelStateListener ;
38+ import io .grpc .SynchronizationContext ;
39+ import io .grpc .internal .FakeClock ;
3540import io .grpc .internal .PickFirstLoadBalancerProvider ;
3641import java .net .SocketAddress ;
3742import java .util .Collections ;
3843import java .util .HashMap ;
3944import java .util .List ;
4045import java .util .Map ;
46+ import java .util .concurrent .ScheduledExecutorService ;
4147import org .mockito .ArgumentCaptor ;
4248import org .mockito .InOrder ;
4349
@@ -60,9 +66,26 @@ public abstract class AbstractTestHelper extends ForwardingLoadBalancerHelper {
6066 protected final Map <Subchannel , Subchannel > realToMockSubChannelMap = new HashMap <>();
6167 private final Map <Subchannel , SubchannelStateListener > subchannelStateListeners =
6268 Maps .newLinkedHashMap ();
69+ private final FakeClock fakeClock ;
70+ private final SynchronizationContext syncContext ;
6371
6472 public abstract Map <List <EquivalentAddressGroup >, Subchannel > getSubchannelMap ();
6573
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+
6689 public Map <Subchannel , Subchannel > getMockToRealSubChannelMap () {
6790 return mockToRealSubChannelMap ;
6891 }
@@ -79,6 +102,18 @@ public Map<Subchannel, SubchannelStateListener> getSubchannelStateListeners() {
79102 return subchannelStateListeners ;
80103 }
81104
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+
82117 public void deliverSubchannelState (Subchannel subchannel , ConnectivityStateInfo newState ) {
83118 Subchannel realSc = getMockToRealSubChannelMap ().get (subchannel );
84119 if (realSc == null ) {
@@ -128,6 +163,16 @@ public void setChannel(Subchannel subchannel, Channel channel) {
128163 ((TestSubchannel )subchannel ).channel = channel ;
129164 }
130165
166+ @ Override
167+ public SynchronizationContext getSynchronizationContext () {
168+ return syncContext ;
169+ }
170+
171+ @ Override
172+ public ScheduledExecutorService getScheduledExecutorService () {
173+ return fakeClock .getScheduledExecutorService ();
174+ }
175+
131176 @ Override
132177 public String toString () {
133178 return "Test Helper" ;
@@ -148,6 +193,17 @@ public static void refreshInvokedAndUpdateBS(InOrder inOrder, ConnectivityState
148193 }
149194 }
150195
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+ }
151207
152208 protected class TestSubchannel extends ForwardingSubchannel {
153209 CreateSubchannelArgs args ;
0 commit comments