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());
}
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());
}
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());
}
Aggregations