1818import static com .google .common .truth .Truth .assertThat ;
1919import static org .mockito .ArgumentMatchers .any ;
2020import static org .mockito .ArgumentMatchers .anyLong ;
21+ import static org .mockito .Mockito .when ;
2122
2223import com .google .api .gax .core .FixedExecutorProvider ;
2324import com .google .api .gax .grpc .ChannelPoolSettings ;
4647import java .util .List ;
4748import java .util .Map ;
4849import java .util .concurrent .ScheduledExecutorService ;
50+ import java .util .concurrent .ScheduledFuture ;
4951import org .junit .After ;
5052import org .junit .Before ;
5153import org .junit .Test ;
5254import org .junit .runner .RunWith ;
5355import org .junit .runners .JUnit4 ;
5456import org .mockito .ArgumentCaptor ;
5557import org .mockito .Mockito ;
58+ import org .mockito .stubbing .Answer ;
5659
5760@ RunWith (JUnit4 .class )
5861public class ErrorCountPerConnectionTest {
@@ -103,9 +106,8 @@ public void setup() throws Exception {
103106 .setMetricsProvider (CustomOpenTelemetryMetricsProvider .create (otel ));
104107
105108 runnableCaptor = ArgumentCaptor .forClass (Runnable .class );
106- Mockito .when (
107- executors .scheduleAtFixedRate (runnableCaptor .capture (), anyLong (), anyLong (), any ()))
108- .thenReturn (null );
109+ when (executors .scheduleAtFixedRate (runnableCaptor .capture (), anyLong (), anyLong (), any ()))
110+ .then ((Answer <ScheduledFuture <?>>) invocation -> Mockito .mock (ScheduledFuture .class ));
109111 }
110112
111113 @ After
@@ -117,21 +119,22 @@ public void tearDown() throws Exception {
117119
118120 @ Test
119121 public void readWithOneChannel () throws Exception {
120- EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ());
121122 long errorCount = 0 ;
122123
123- for (int i = 0 ; i < 20 ; i ++) {
124- Query query ;
125- if (i % 3 == 0 ) {
126- query = Query .create (ERROR_TABLE_NAME );
127- errorCount += 1 ;
128- } else {
129- query = Query .create (SUCCESS_TABLE_NAME );
130- }
131- try {
132- stub .readRowsCallable ().call (query ).iterator ().hasNext ();
133- } catch (Exception e ) {
134- // noop
124+ try (EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ())) {
125+ for (int i = 0 ; i < 20 ; i ++) {
126+ Query query ;
127+ if (i % 3 == 0 ) {
128+ query = Query .create (ERROR_TABLE_NAME );
129+ errorCount += 1 ;
130+ } else {
131+ query = Query .create (SUCCESS_TABLE_NAME );
132+ }
133+ try {
134+ stub .readRowsCallable ().call (query ).iterator ().hasNext ();
135+ } catch (Exception e ) {
136+ // noop
137+ }
135138 }
136139 }
137140
@@ -158,19 +161,19 @@ public void readWithTwoChannels() throws Exception {
158161 .toBuilder ()
159162 .setChannelPoolSettings (ChannelPoolSettings .staticallySized (2 ))
160163 .build ());
161- EnhancedBigtableStub stub = EnhancedBigtableStub .create (builderWithTwoChannels .build ());
162164 long totalErrorCount = 0 ;
163-
164- for (int i = 0 ; i < 20 ; i ++) {
165- try {
166- if (i < 10 ) {
167- totalErrorCount += 1 ;
168- stub .readRowsCallable ().call (Query .create (ERROR_TABLE_NAME )).iterator ().hasNext ();
169- } else {
170- stub .readRowsCallable ().call (Query .create (SUCCESS_TABLE_NAME )).iterator ().hasNext ();
165+ try (EnhancedBigtableStub stub = EnhancedBigtableStub .create (builderWithTwoChannels .build ())) {
166+ for (int i = 0 ; i < 20 ; i ++) {
167+ try {
168+ if (i < 10 ) {
169+ totalErrorCount += 1 ;
170+ stub .readRowsCallable ().call (Query .create (ERROR_TABLE_NAME )).iterator ().hasNext ();
171+ } else {
172+ stub .readRowsCallable ().call (Query .create (SUCCESS_TABLE_NAME )).iterator ().hasNext ();
173+ }
174+ } catch (Exception e ) {
175+ // noop
171176 }
172- } catch (Exception e ) {
173- // noop
174177 }
175178 }
176179 runInterceptorTasksAndAssertCount ();
@@ -193,39 +196,40 @@ public void readWithTwoChannels() throws Exception {
193196
194197 @ Test
195198 public void readOverTwoPeriods () throws Exception {
196- EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ());
197199 long errorCount1 = 0 ;
200+ long errorCount2 = 0 ;
201+ try (EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ())) {
198202
199- for (int i = 0 ; i < 20 ; i ++) {
200- Query query ;
201- if (i % 3 == 0 ) {
202- query = Query .create (ERROR_TABLE_NAME );
203- errorCount1 += 1 ;
204- } else {
205- query = Query .create (SUCCESS_TABLE_NAME );
206- }
207- try {
208- stub .readRowsCallable ().call (query ).iterator ().hasNext ();
209- } catch (Exception e ) {
210- // noop
203+ for (int i = 0 ; i < 20 ; i ++) {
204+ Query query ;
205+ if (i % 3 == 0 ) {
206+ query = Query .create (ERROR_TABLE_NAME );
207+ errorCount1 += 1 ;
208+ } else {
209+ query = Query .create (SUCCESS_TABLE_NAME );
210+ }
211+ try {
212+ stub .readRowsCallable ().call (query ).iterator ().hasNext ();
213+ } catch (Exception e ) {
214+ // noop
215+ }
211216 }
212- }
213217
214- runInterceptorTasksAndAssertCount ();
215- long errorCount2 = 0 ;
218+ runInterceptorTasksAndAssertCount ();
216219
217- for (int i = 0 ; i < 20 ; i ++) {
218- Query query ;
219- if (i % 3 == 0 ) {
220- query = Query .create (SUCCESS_TABLE_NAME );
221- } else {
222- query = Query .create (ERROR_TABLE_NAME );
223- errorCount2 += 1 ;
224- }
225- try {
226- stub .readRowsCallable ().call (query ).iterator ().hasNext ();
227- } catch (Exception e ) {
228- // noop
220+ for (int i = 0 ; i < 20 ; i ++) {
221+ Query query ;
222+ if (i % 3 == 0 ) {
223+ query = Query .create (SUCCESS_TABLE_NAME );
224+ } else {
225+ query = Query .create (ERROR_TABLE_NAME );
226+ errorCount2 += 1 ;
227+ }
228+ try {
229+ stub .readRowsCallable ().call (query ).iterator ().hasNext ();
230+ } catch (Exception e ) {
231+ // noop
232+ }
229233 }
230234 }
231235
@@ -247,15 +251,16 @@ public void readOverTwoPeriods() throws Exception {
247251
248252 @ Test
249253 public void noFailedRequests () throws Exception {
250- EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ());
251-
252- for ( int i = 0 ; i < 20 ; i ++) {
253- try {
254- stub . readRowsCallable (). call ( Query . create ( SUCCESS_TABLE_NAME )). iterator (). hasNext ();
255- } catch ( Exception e ) {
256- // noop
254+ try ( EnhancedBigtableStub stub = EnhancedBigtableStub .create (builder .build ())) {
255+ for ( int i = 0 ; i < 20 ; i ++) {
256+ try {
257+ stub . readRowsCallable (). call ( Query . create ( SUCCESS_TABLE_NAME )). iterator (). hasNext ();
258+ } catch ( Exception e ) {
259+ // noop
260+ }
257261 }
258262 }
263+
259264 runInterceptorTasksAndAssertCount ();
260265 MetricData metricData =
261266 BuiltinMetricsTestUtils .getMetricData (
0 commit comments