Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target
.settings

# IntelliJ
.ijwb
.idea
*.iml
out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ public void sendOutstanding() {
callContextWithOption =
callContext.withOption(THROTTLED_TIME_KEY, accumulatedBatch.totalThrottledTimeMs);
}
final ApiFuture<ResponseT> batchResponse =
unaryCallable.futureCall(accumulatedBatch.builder.build(), callContextWithOption);
ApiFuture<ResponseT> batchResponse;
try {
batchResponse =
unaryCallable.futureCall(accumulatedBatch.builder.build(), callContextWithOption);
} catch (Exception ex) {
batchResponse = ApiFutures.immediateFailedFuture(ex);
}

numOfOutstandingBatches.incrementAndGet();
ApiFutures.addCallback(
Expand Down
75 changes: 75 additions & 0 deletions gax/src/test/java/com/google/api/gax/batching/BatcherImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList;
import com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntSquarerCallable;
import com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2;
import com.google.api.gax.rpc.testing.FakeCallContext;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Queues;
Expand Down Expand Up @@ -931,6 +932,80 @@ public void testThrottlingNonBlocking() throws Exception {
}
}

/**
* If the batcher's unary callable throws an exception when obtaining a response, then the
* response .get() should throw the exception
*/
@Test
public void testAddDoesNotHangIfExceptionThrowStartingACall() {
BatchingDescriptor<Object, Object, Object, Object> batchingDescriptor =
new BatchingDescriptor<Object, Object, Object, Object>() {
@Override
public BatchingRequestBuilder<Object, Object> newRequestBuilder(Object o) {
return new BatchingRequestBuilder<Object, Object>() {
@Override
public void add(Object o) {}

@Override
public Object build() {
return new Object();
}
};
}

@Override
public void splitResponse(Object o, List<BatchEntry<Object, Object>> list) {
for (BatchEntry<Object, Object> e : list) {
e.getResultFuture().set(new Object());
}
}

@Override
public void splitException(Throwable throwable, List<BatchEntry<Object, Object>> list) {
for (BatchEntry<Object, Object> e : list) {
e.getResultFuture().setException(new RuntimeException("fake"));
}
}

@Override
public long countBytes(Object o) {
return 1;
}
};

UnaryCallable<Object, Object> unaryCallable =
new UnaryCallable<Object, Object>() {
@Override
public ApiFuture<Object> futureCall(Object o, ApiCallContext apiCallContext) {
throw new RuntimeException("this should bubble up");
}
};
Object prototype = new Object();
BatchingSettings batchingSettings =
BatchingSettings.newBuilder()
.setDelayThreshold(Duration.ofSeconds(1))
.setElementCountThreshold(100L)
.setRequestByteThreshold(100L)
.setFlowControlSettings(FlowControlSettings.getDefaultInstance())
.build();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
FlowController flowController = new FlowController(batchingSettings.getFlowControlSettings());
ApiCallContext callContext = FakeCallContext.createDefault();

BatcherImpl<Object, Object, Object, Object> batcher =
new BatcherImpl<>(
batchingDescriptor,
unaryCallable,
prototype,
batchingSettings,
executor,
flowController,
callContext);

ApiFuture<Object> f = batcher.add(new Object());
Assert.assertThrows(ExecutionException.class, f::get);
}

private void testElementTriggers(BatchingSettings settings) throws Exception {
underTest = createDefaultBatcherImpl(settings, null);
Future<Integer> result = underTest.add(4);
Expand Down