Search in sources :

Example 51 with KeyValue

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());
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) KeyValue(com.apple.foundationdb.KeyValue) AsyncIterator(com.apple.foundationdb.async.AsyncIterator) CompletableFuture(java.util.concurrent.CompletableFuture) Captor(org.mockito.Captor) StandardCharsets(java.nio.charset.StandardCharsets) OperationFailureResponse(io.github.panghy.lionrock.proto.OperationFailureResponse) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.junit.jupiter.api.Assertions) NamedCompletableFuture(io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture) GetRangeResponse(io.github.panghy.lionrock.proto.GetRangeResponse) KeyValue(com.apple.foundationdb.KeyValue) OperationFailureResponse(io.github.panghy.lionrock.proto.OperationFailureResponse) GetRangeResponse(io.github.panghy.lionrock.proto.GetRangeResponse) NamedCompletableFuture(io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture) Test(org.junit.jupiter.api.Test)

Aggregations

KeyValue (com.apple.foundationdb.KeyValue)51 Test (org.junit.jupiter.api.Test)23 Subspace (com.apple.foundationdb.subspace.Subspace)22 Nonnull (javax.annotation.Nonnull)22 CompletableFuture (java.util.concurrent.CompletableFuture)19 List (java.util.List)18 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)16 ArrayList (java.util.ArrayList)16 Transaction (com.apple.foundationdb.Transaction)15 ScanProperties (com.apple.foundationdb.record.ScanProperties)15 Tuple (com.apple.foundationdb.tuple.Tuple)14 Map (java.util.Map)14 Collectors (java.util.stream.Collectors)12 Nullable (javax.annotation.Nullable)12 ByteArrayUtil (com.apple.foundationdb.tuple.ByteArrayUtil)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 AsyncIterator (com.apple.foundationdb.async.AsyncIterator)10 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)10 Collections (java.util.Collections)10 ReadTransaction (com.apple.foundationdb.ReadTransaction)9