use of com.apple.foundationdb.KeyValue in project lionrock by panghy.
the class GrpcAsyncIterableTest method iterator_testFailure.
@Test
void iterator_testFailure() {
// setup mocks.
GrpcAsyncIterator.RemovalCallback<KeyValue> removalCallback = mock(GrpcAsyncIterator.RemovalCallback.class);
GrpcAsyncIterable.FetchIssuer<GetRangeResponse> fetchIssuer = mock(GrpcAsyncIterable.FetchIssuer.class);
GrpcAsyncIterable<KeyValue, GetRangeResponse> iterable = new GrpcAsyncIterable<>(removalCallback, resp -> resp.getKeyValuesList().stream().map(x -> new com.apple.foundationdb.KeyValue(x.getKey().toByteArray(), x.getValue().toByteArray())), GetRangeResponse::getDone, NamedCompletableFuture::new, fetchIssuer, MoreExecutors.directExecutor());
AsyncIterator<KeyValue> iterator = iterable.iterator();
verify(fetchIssuer, times(1)).issue(respCaptor.capture(), failureCaptor.capture());
Consumer<GetRangeResponse> value = respCaptor.getValue();
CompletableFuture<Boolean> cf = iterator.onHasNext();
assertFalse(cf.isDone());
value.accept(GetRangeResponse.newBuilder().addKeyValues(io.github.panghy.lionrock.proto.KeyValue.newBuilder().setKey(ByteString.copyFrom("hello", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world", StandardCharsets.UTF_8)).build()).build());
Consumer<OperationFailureResponse> failureResponseConsumer = failureCaptor.getValue();
failureResponseConsumer.accept(OperationFailureResponse.newBuilder().setMessage("failed!!!").setCode(123).build());
assertTrue(cf.isDone());
assertTrue(iterator.hasNext());
KeyValue next = iterator.next();
assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), next.getKey());
cf = iterator.onHasNext();
assertTrue(cf.isCompletedExceptionally());
}
Aggregations