Search in sources :

Example 1 with GetRangeResponse

use of io.github.panghy.lionrock.proto.GetRangeResponse in project lionrock by panghy.

the class GrpcAsyncIterableTest method iterator.

@Test
void iterator() {
    // 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());
    assertTrue(cf.isDone());
    assertTrue(iterator.hasNext());
    KeyValue next = iterator.next();
    assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), next.getKey());
    cf = iterator.onHasNext();
    assertFalse(cf.isDone());
    value.accept(GetRangeResponse.newBuilder().setDone(true).addKeyValues(io.github.panghy.lionrock.proto.KeyValue.newBuilder().setKey(ByteString.copyFrom("hello2", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world2", StandardCharsets.UTF_8)).build()).build());
    assertTrue(cf.isDone());
    assertTrue(iterator.hasNext());
    next = iterator.next();
    assertArrayEquals("hello2".getBytes(StandardCharsets.UTF_8), next.getKey());
    iterator.remove();
    iterator.onHasNext();
    assertTrue(cf.isDone());
    assertFalse(iterator.hasNext());
    verify(removalCallback, times(1)).deleteKey(eq(new KeyValue("hello2".getBytes(StandardCharsets.UTF_8), "world2".getBytes(StandardCharsets.UTF_8))));
    // get another iterator.
    iterator = iterable.iterator();
    verify(fetchIssuer, times(2)).issue(respCaptor.capture(), failureCaptor.capture());
    value = respCaptor.getValue();
    cf = iterator.onHasNext();
    assertFalse(cf.isDone());
    value.accept(GetRangeResponse.newBuilder().setDone(true).addKeyValues(io.github.panghy.lionrock.proto.KeyValue.newBuilder().setKey(ByteString.copyFrom("hello2", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world2", StandardCharsets.UTF_8)).build()).build());
    assertTrue(cf.isDone());
    assertTrue(iterator.hasNext());
    next = iterator.next();
    assertArrayEquals("hello2".getBytes(StandardCharsets.UTF_8), next.getKey());
    iterator.onHasNext();
    assertTrue(cf.isDone());
    assertFalse(iterator.hasNext());
}
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) GetRangeResponse(io.github.panghy.lionrock.proto.GetRangeResponse) NamedCompletableFuture(io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture) Test(org.junit.jupiter.api.Test)

Example 2 with GetRangeResponse

use of io.github.panghy.lionrock.proto.GetRangeResponse 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)

Example 3 with GetRangeResponse

use of io.github.panghy.lionrock.proto.GetRangeResponse in project lionrock by panghy.

the class GrpcAsyncIteratorTest method testIterator_failure.

@Test
public void testIterator_failure() {
    GrpcAsyncIterator.RemovalCallback<com.apple.foundationdb.KeyValue> removalCallback = mock(GrpcAsyncIterator.RemovalCallback.class);
    GrpcAsyncIterator<com.apple.foundationdb.KeyValue, GetRangeResponse> iterator = new GrpcAsyncIterator<>(removalCallback, resp -> resp.getKeyValuesList().stream().map(x -> new com.apple.foundationdb.KeyValue(x.getKey().toByteArray(), x.getValue().toByteArray())), NamedCompletableFuture::new, GetRangeResponse::getDone);
    // add 1st and 2nd element.
    CompletableFuture<Boolean> cf = iterator.onHasNext();
    assertFalse(cf.isDone());
    iterator.accept(GetRangeResponse.newBuilder().addKeyValues(KeyValue.newBuilder().setKey(ByteString.copyFrom("hello", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world", StandardCharsets.UTF_8)).build()).addKeyValues(KeyValue.newBuilder().setKey(ByteString.copyFrom("hello2", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world2", StandardCharsets.UTF_8)).build()).build());
    assertTrue(cf.isDone());
    assertTrue(iterator.hasNext());
    com.apple.foundationdb.KeyValue next = iterator.next();
    assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), next.getKey());
    cf = iterator.onHasNext();
    assertTrue(cf.isDone());
    assertTrue(iterator.hasNext());
    next = iterator.next();
    assertArrayEquals("hello2".getBytes(StandardCharsets.UTF_8), next.getKey());
    cf = iterator.onHasNext();
    assertFalse(cf.isDone());
    iterator.accept(OperationFailureResponse.newBuilder().setCode(123).setMessage("failed").build());
    assertTrue(cf.isCompletedExceptionally());
    FDBException join = (FDBException) cf.handle((aBoolean, throwable) -> throwable).join();
    assertEquals(123, join.getCode());
    assertEquals("failed", join.getMessage());
    // does not change the state.
    iterator.accept(GetRangeResponse.newBuilder().addKeyValues(KeyValue.newBuilder().setKey(ByteString.copyFrom("hello", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world", StandardCharsets.UTF_8)).build()).addKeyValues(KeyValue.newBuilder().setKey(ByteString.copyFrom("hello2", StandardCharsets.UTF_8)).setValue(ByteString.copyFrom("world2", StandardCharsets.UTF_8)).build()).build());
    cf = iterator.onHasNext();
    assertTrue(cf.isCompletedExceptionally());
}
Also used : ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) FDBException(com.apple.foundationdb.FDBException) KeyValue(io.github.panghy.lionrock.proto.KeyValue) Assertions(org.junit.jupiter.api.Assertions) NamedCompletableFuture(io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture) CompletableFuture(java.util.concurrent.CompletableFuture) GetRangeResponse(io.github.panghy.lionrock.proto.GetRangeResponse) NoSuchElementException(java.util.NoSuchElementException) StandardCharsets(java.nio.charset.StandardCharsets) OperationFailureResponse(io.github.panghy.lionrock.proto.OperationFailureResponse) KeyValue(io.github.panghy.lionrock.proto.KeyValue) GetRangeResponse(io.github.panghy.lionrock.proto.GetRangeResponse) FDBException(com.apple.foundationdb.FDBException) NamedCompletableFuture(io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture) Test(org.junit.jupiter.api.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)3 NamedCompletableFuture (io.github.panghy.lionrock.client.foundationdb.impl.NamedCompletableFuture)3 GetRangeResponse (io.github.panghy.lionrock.proto.GetRangeResponse)3 OperationFailureResponse (io.github.panghy.lionrock.proto.OperationFailureResponse)3 StandardCharsets (java.nio.charset.StandardCharsets)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Assertions (org.junit.jupiter.api.Assertions)3 Test (org.junit.jupiter.api.Test)3 Mockito (org.mockito.Mockito)3 KeyValue (com.apple.foundationdb.KeyValue)2 AsyncIterator (com.apple.foundationdb.async.AsyncIterator)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 Captor (org.mockito.Captor)2 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)2 FDBException (com.apple.foundationdb.FDBException)1 KeyValue (io.github.panghy.lionrock.proto.KeyValue)1