Search in sources :

Example 1 with Subscription

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)));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription)

Example 2 with Subscription

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);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Subscription(java.util.concurrent.Flow.Subscription) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(java.util.concurrent.Flow) Test(org.junit.jupiter.api.Test)

Example 3 with Subscription

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);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.jupiter.api.Test)

Example 4 with Subscription

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();
    });
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 5 with Subscription

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;
}
Also used : Subscription(java.util.concurrent.Flow.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription)

Aggregations

Subscription (java.util.concurrent.Flow.Subscription)28 Test (org.junit.jupiter.api.Test)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 DataChunk (io.helidon.common.http.DataChunk)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)5 Subscriber (java.util.concurrent.Flow.Subscriber)4 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)3 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 Test (org.testng.annotations.Test)3 IntegerGene (io.jenetics.IntegerGene)2 ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 Flow (java.util.concurrent.Flow)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2