20
20
import static org .mockito .Mockito .doAnswer ;
21
21
import static org .mockito .Mockito .when ;
22
22
23
- import com .google .api .gax .batching .BatchResource ;
24
23
import com .google .api .gax .batching .Batcher ;
25
24
import com .google .api .gax .batching .BatcherImpl ;
26
- import com .google .api .gax .batching .BatchingDescriptor ;
27
25
import com .google .api .gax .batching .FlowController ;
28
26
import com .google .api .gax .grpc .GrpcCallContext ;
29
27
import com .google .api .gax .rpc .ApiCallContext ;
@@ -387,45 +385,38 @@ public Object answer(InvocationOnMock invocation) {
387
385
.when (mockService )
388
386
.readRows (any (ReadRowsRequest .class ), any ());
389
387
390
- try (Batcher batcher =
388
+ try (Batcher < ByteString , Row > batcher =
391
389
stub .newBulkReadRowsBatcher (Query .create (TABLE_ID ), GrpcCallContext .createDefault ())) {
392
390
batcher .add (ByteString .copyFromUtf8 ("row1" ));
393
- batcher .sendOutstanding ();
394
-
395
- long throttledTimeMetric =
396
- StatsTestUtils .getAggregationValueAsLong (
397
- localStats ,
398
- RpcViewConstants .BIGTABLE_BATCH_THROTTLED_TIME_VIEW ,
399
- ImmutableMap .of (
400
- RpcMeasureConstants .BIGTABLE_OP , TagValue .create ("Bigtable.ReadRows" )),
401
- PROJECT_ID ,
402
- INSTANCE_ID ,
403
- APP_PROFILE_ID );
404
- assertThat (throttledTimeMetric ).isEqualTo (0 );
405
391
}
392
+
393
+ long throttledTimeMetric =
394
+ StatsTestUtils .getAggregationValueAsLong (
395
+ localStats ,
396
+ RpcViewConstants .BIGTABLE_BATCH_THROTTLED_TIME_VIEW ,
397
+ ImmutableMap .of (RpcMeasureConstants .BIGTABLE_OP , TagValue .create ("Bigtable.ReadRows" )),
398
+ PROJECT_ID ,
399
+ INSTANCE_ID ,
400
+ APP_PROFILE_ID );
401
+ assertThat (throttledTimeMetric ).isEqualTo (0 );
406
402
}
407
403
408
404
@ Test
409
405
public void testBatchMutateRowsThrottledTime () throws Exception {
410
406
FlowController flowController = Mockito .mock (FlowController .class );
411
- BatchingDescriptor batchingDescriptor = Mockito .mock (MutateRowsBatchingDescriptor .class );
412
- when (batchingDescriptor .createResource (any ())).thenReturn (new FakeBatchResource ());
413
- when (batchingDescriptor .createEmptyResource ()).thenReturn (new FakeBatchResource ());
407
+ MutateRowsBatchingDescriptor batchingDescriptor = new MutateRowsBatchingDescriptor ();
408
+
414
409
// Mock throttling
415
410
final long throttled = 50 ;
416
411
doAnswer (
417
- new Answer () {
418
- @ Override
419
- public Object answer (InvocationOnMock invocation ) throws Throwable {
420
- Thread .sleep (throttled );
421
- return null ;
422
- }
412
+ invocation -> {
413
+ Thread .sleep (throttled );
414
+ return null ;
423
415
})
424
416
.when (flowController )
425
417
.reserve (any (Long .class ), any (Long .class ));
426
418
when (flowController .getMaxElementCountLimit ()).thenReturn (null );
427
419
when (flowController .getMaxRequestBytesLimit ()).thenReturn (null );
428
- when (batchingDescriptor .newRequestBuilder (any ())).thenCallRealMethod ();
429
420
430
421
doAnswer (
431
422
new Answer () {
@@ -444,18 +435,18 @@ public Object answer(InvocationOnMock invocation) {
444
435
445
436
ApiCallContext defaultContext = GrpcCallContext .createDefault ();
446
437
447
- Batcher batcher =
448
- new BatcherImpl (
438
+ try ( Batcher < RowMutationEntry , Void > batcher =
439
+ new BatcherImpl <> (
449
440
batchingDescriptor ,
450
441
stub .bulkMutateRowsCallable ().withDefaultCallContext (defaultContext ),
451
442
BulkMutation .create (TABLE_ID ),
452
443
settings .getStubSettings ().bulkMutateRowsSettings ().getBatchingSettings (),
453
444
Executors .newSingleThreadScheduledExecutor (),
454
445
flowController ,
455
- defaultContext );
446
+ defaultContext )) {
456
447
457
- batcher .add (RowMutationEntry .create ("key" ));
458
- batcher . sendOutstanding ();
448
+ batcher .add (RowMutationEntry .create ("key" ). deleteRow ( ));
449
+ }
459
450
460
451
long throttledTimeMetric =
461
452
StatsTestUtils .getAggregationValueAsLong (
@@ -473,29 +464,4 @@ public Object answer(InvocationOnMock invocation) {
473
464
private static <T > StreamObserver <T > anyObserver (Class <T > returnType ) {
474
465
return (StreamObserver <T >) any (returnType );
475
466
}
476
-
477
- private class FakeBatchResource implements BatchResource {
478
-
479
- FakeBatchResource () {}
480
-
481
- @ Override
482
- public BatchResource add (BatchResource resource ) {
483
- return new FakeBatchResource ();
484
- }
485
-
486
- @ Override
487
- public long getElementCount () {
488
- return 1 ;
489
- }
490
-
491
- @ Override
492
- public long getByteCount () {
493
- return 1 ;
494
- }
495
-
496
- @ Override
497
- public boolean shouldFlush (long maxElementThreshold , long maxBytesThreshold ) {
498
- return false ;
499
- }
500
- }
501
467
}
0 commit comments