use of com.google.api.gax.rpc.DeadlineExceededException in project spring-cloud-gcp by spring-cloud.
the class PubSubReactiveFactoryTests method setUpMessages.
/**
* Replays provided messages.
* If a synthetic message "stop" is encountered, immediately returns previously collected messages.
* If a synthetic message "timeout" is encountered, throws an {@link DeadlineExceededException}.
* If a synthetic message "throw" is encountered, throws an {@link RuntimeException}.
* Fails the calling test if there are not enough messages to fulfill demand from cumulative calls to {@code pull()}.
* @param messages messages to replay
*/
private void setUpMessages(String... messages) {
List<String> msgList = new ArrayList<>(Arrays.asList(messages));
when(subscriberOperations.pullAsync(eq("sub1"), any(Integer.class), any(Boolean.class))).then(invocationOnMock -> {
List<AcknowledgeablePubsubMessage> result = new ArrayList<>();
for (int i = 0; i < (Integer) invocationOnMock.getArgument(1); i++) {
if (msgList.isEmpty()) {
fail("Ran out of provided messages.");
}
String nextPayload = msgList.remove(0);
switch(nextPayload) {
case "stop":
return AsyncResult.forValue(result);
case "timeout":
if (!result.isEmpty()) {
fail("Bad setup -- 'throw' should be the first event in batch");
}
return AsyncResult.forExecutionException(new DeadlineExceededException("this is a noop", null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), true));
case "throw":
return AsyncResult.forExecutionException(new RuntimeException("expected exception during pull of messages"));
}
AcknowledgeablePubsubMessage msg = mock(AcknowledgeablePubsubMessage.class);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom((nextPayload).getBytes())).build();
when(msg.getPubsubMessage()).thenReturn(pubsubMessage);
result.add(msg);
}
return AsyncResult.forValue(result);
});
}
Aggregations