Search in sources :

Example 26 with Subscription

use of java.util.concurrent.Flow.Subscription in project helidon by oracle.

the class SingleToOptionalFuture method onNext.

@Override
public void onNext(T item) {
    // we expect exactly one item
    if (!this.item.compareAndSet(null, item)) {
        super.completeExceptionally(new IllegalStateException("Received more than one value for a single."));
        Subscription s = ref.getAndSet(null);
        s.cancel();
    }
}
Also used : Subscription(java.util.concurrent.Flow.Subscription)

Example 27 with Subscription

use of java.util.concurrent.Flow.Subscription in project helidon by oracle.

the class SingleToOptionalFuture method onSubscribe.

@Override
public void onSubscribe(Subscription next) {
    Subscription current = ref.getAndSet(next);
    Objects.requireNonNull(next, "Subscription cannot be null");
    if (current != null) {
        next.cancel();
        current.cancel();
    } else {
        next.request(Long.MAX_VALUE);
    }
}
Also used : Subscription(java.util.concurrent.Flow.Subscription)

Example 28 with Subscription

use of java.util.concurrent.Flow.Subscription in project helidon by oracle.

the class ReadableByteChannelPublisherTest method testCancelled.

@Test
void testCancelled() throws Exception {
    PeriodicalChannel pc = createChannelWithNoAvailableData(5, 1);
    ReadableByteChannelPublisher publisher = new ReadableByteChannelPublisher(pc, RetrySchema.constant(100));
    AtomicReference<Subscription> subscriptionRef = new AtomicReference<>();
    final CountDownLatch onNextCalled = new CountDownLatch(1);
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicBoolean completeCalled = new AtomicBoolean();
    publisher.subscribe(new Subscriber<>() {

        @Override
        public void onSubscribe(Subscription subscription) {
            subscriptionRef.set(subscription);
            subscription.request(1);
        }

        @Override
        public void onNext(DataChunk item) {
            onNextCalled.countDown();
        }

        @Override
        public void onError(Throwable throwable) {
            failure.set(throwable);
        }

        @Override
        public void onComplete() {
            completeCalled.set(true);
        }
    });
    onNextCalled.await(5, TimeUnit.SECONDS);
    subscriptionRef.get().cancel();
    assertThat("Should not complete", completeCalled.get(), is(false));
    assertThat("Exception should be null", failure.get(), is(nullValue()));
    LazyValue<ScheduledExecutorService> executor = publisher.executor();
    assertThat("Executor should have been used", executor.isLoaded(), is(true));
    assertThat("Executor should have been shut down", executor.get().isShutdown(), is(true));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataChunk(io.helidon.common.http.DataChunk) Subscription(java.util.concurrent.Flow.Subscription) Test(org.junit.jupiter.api.Test)

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