use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.
the class ItInternalTableScanTest method requestNtest.
/**
* Checks whether publisher provides all existing data and then completes if requested by reqAmount rows at a time.
*
* @param submittedItems Items to be pushed by publisher.
* @param reqAmount Amount of rows to request at a time.
* @throws Exception If Any.
*/
private void requestNtest(List<DataRow> submittedItems, int reqAmount) throws Exception {
AtomicInteger cursorTouchCnt = new AtomicInteger(0);
List<BinaryRow> retrievedItems = Collections.synchronizedList(new ArrayList<>());
when(mockStorage.scan(any())).thenAnswer(invocation -> {
var cursor = mock(Cursor.class);
when(cursor.hasNext()).thenAnswer(hnInvocation -> cursorTouchCnt.get() < submittedItems.size());
when(cursor.next()).thenAnswer(ninvocation -> submittedItems.get(cursorTouchCnt.getAndIncrement()));
return cursor;
});
// The latch that allows to await Subscriber.onError() before asserting test invariants.
CountDownLatch subscriberAllDataAwaitLatch = new CountDownLatch(1);
internalTbl.scan(0, null).subscribe(new Subscriber<>() {
private Subscription subscription;
@Override
public void onSubscribe(Subscription subscription) {
this.subscription = subscription;
subscription.request(reqAmount);
}
@Override
public void onNext(BinaryRow item) {
retrievedItems.add(item);
if (retrievedItems.size() % reqAmount == 0) {
subscription.request(reqAmount);
}
}
@Override
public void onError(Throwable throwable) {
fail("onError call is not expected.");
}
@Override
public void onComplete() {
subscriberAllDataAwaitLatch.countDown();
}
});
subscriberAllDataAwaitLatch.await();
assertEquals(submittedItems.size(), retrievedItems.size());
List<byte[]> expItems = submittedItems.stream().map(DataRow::valueBytes).collect(Collectors.toList());
List<byte[]> gotItems = retrievedItems.stream().map(BinaryRow::bytes).collect(Collectors.toList());
for (int i = 0; i < expItems.size(); i++) {
assertTrue(Arrays.equals(expItems.get(i), gotItems.get(i)));
}
}
use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.
the class ItInternalTableScanTest method testSecondSubscriptionFiresIllegalStateException.
/**
* Checks that in case of second subscription {@link IllegalStateException} will be fires to onError.
*
* @throws Exception If any.
*/
@Test
public void testSecondSubscriptionFiresIllegalStateException() throws Exception {
Flow.Publisher<BinaryRow> scan = internalTbl.scan(0, null);
scan.subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
}
@Override
public void onNext(BinaryRow item) {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
});
// The latch that allows to await Subscriber.onError() before asserting test invariants.
CountDownLatch gotExceptionLatch = new CountDownLatch(1);
AtomicReference<Throwable> gotException = new AtomicReference<>();
scan.subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
}
@Override
public void onNext(BinaryRow item) {
}
@Override
public void onError(Throwable throwable) {
gotException.set(throwable);
gotExceptionLatch.countDown();
}
@Override
public void onComplete() {
}
});
gotExceptionLatch.await();
assertEquals(gotException.get().getClass(), IllegalStateException.class);
}
use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.
the class ItInternalTableScanTest method testExceptionRowScanCursorHasNext.
/**
* Checks that exception from storage cursors has next properly propagates to subscriber.
*/
@Test
public void testExceptionRowScanCursorHasNext() throws Exception {
// The latch that allows to await Subscriber.onComplete() before asserting test invariants
// and avoids the race between closing the cursor and stopping the node.
CountDownLatch subscriberFinishedLatch = new CountDownLatch(2);
AtomicReference<Throwable> gotException = new AtomicReference<>();
when(mockStorage.scan(any())).thenAnswer(invocation -> {
var cursor = mock(Cursor.class);
when(cursor.hasNext()).thenAnswer(hnInvocation -> true);
when(cursor.next()).thenAnswer(hnInvocation -> {
throw new NoSuchElementException("test");
});
doAnswer(invocationClose -> {
subscriberFinishedLatch.countDown();
return null;
}).when(cursor).close();
return cursor;
});
internalTbl.scan(0, null).subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(1);
}
@Override
public void onNext(BinaryRow item) {
fail("Should never get here.");
}
@Override
public void onError(Throwable throwable) {
gotException.set(throwable);
subscriberFinishedLatch.countDown();
}
@Override
public void onComplete() {
fail("Should never get here.");
}
});
subscriberFinishedLatch.await();
assertEquals(gotException.get().getCause().getClass(), NoSuchElementException.class);
}
use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.
the class ItInternalTableScanTest method invalidRequestNtest.
/**
* Checks whether {@link IllegalArgumentException} is thrown and inner storage cursor is closes in case of invalid requested amount of
* items.
*
* @param reqAmount Amount of rows to request at a time.
* @throws Exception If Any.
*/
private void invalidRequestNtest(int reqAmount) throws InterruptedException {
// The latch that allows to await Subscriber.onComplete() before asserting test invariants
// and avoids the race between closing the cursor and stopping the node.
CountDownLatch subscriberFinishedLatch = new CountDownLatch(2);
when(mockStorage.scan(any())).thenAnswer(invocation -> {
var cursor = mock(Cursor.class);
doAnswer(invocationClose -> {
subscriberFinishedLatch.countDown();
return null;
}).when(cursor).close();
when(cursor.hasNext()).thenAnswer(hnInvocation -> {
throw new StorageException("test");
});
return cursor;
});
AtomicReference<Throwable> gotException = new AtomicReference<>();
internalTbl.scan(0, null).subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(reqAmount);
}
@Override
public void onNext(BinaryRow item) {
fail("Should never get here.");
}
@Override
public void onError(Throwable throwable) {
gotException.set(throwable);
subscriberFinishedLatch.countDown();
}
@Override
public void onComplete() {
fail("Should never get here.");
}
});
subscriberFinishedLatch.await();
assertThrows(IllegalArgumentException.class, () -> {
throw gotException.get();
});
}
use of java.util.concurrent.Flow.Subscription in project servicetalk by apple.
the class JdkFlowAdaptersTest method newMockFlowPublisher.
private Publisher<Integer> newMockFlowPublisher(BiConsumer<Subscriber<? super Integer>, Subscription> subscriberTerminator) {
@SuppressWarnings("unchecked") Publisher<Integer> flowPublisher = mock(Publisher.class);
doAnswer(invocation -> {
Subscriber<? super Integer> subscriber = invocation.getArgument(0);
Subscription subscription = mock(Subscription.class);
doAnswer(invocation1 -> {
subscriberTerminator.accept(subscriber, subscription);
return null;
}).when(subscription).request(anyLong());
subscriber.onSubscribe(subscription);
return null;
}).when(flowPublisher).subscribe(any());
return flowPublisher;
}
Aggregations