Search in sources :

Example 6 with Subscription

use of java.util.concurrent.Flow.Subscription in project servicetalk by apple.

the class JdkFlowAdaptersTest method toFlowCancel.

@Test
void toFlowCancel() {
    TestPublisher<Integer> stPublisher = new TestPublisher<>();
    Subscriber<Integer> subscriber = toFlowPublisherAndSubscribe(stPublisher);
    TestSubscription subscription = new TestSubscription();
    stPublisher.onSubscribe(subscription);
    assertThat("Source not subscribed.", stPublisher.isSubscribed(), is(true));
    ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(subscriber).onSubscribe(subscriptionCaptor.capture());
    subscriptionCaptor.getValue().cancel();
    assertThat("Subscription not cancelled.", subscription.isCancelled(), is(true));
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Subscription(java.util.concurrent.Flow.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Test(org.junit.jupiter.api.Test)

Example 7 with Subscription

use of java.util.concurrent.Flow.Subscription in project jenetics by jenetics.

the class StreamPublisherTest method publishClosingPublisher.

@Test
public void publishClosingPublisher() throws InterruptedException {
    final int generations = 20;
    final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
    final var stream = _engine.stream();
    final var lock = new ReentrantLock();
    final var finished = lock.newCondition();
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicBoolean completed = new AtomicBoolean(false);
    final AtomicInteger count = new AtomicInteger();
    publisher.subscribe(new Subscriber<>() {

        private Subscription _subscription;

        @Override
        public void onSubscribe(final Subscription subscription) {
            _subscription = requireNonNull(subscription);
            _subscription.request(1);
        }

        @Override
        public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new CancellationException();
            }
            count.incrementAndGet();
            lock.lock();
            try {
                running.set(er.generation() < generations);
                finished.signal();
            } finally {
                lock.unlock();
            }
            _subscription.request(1);
        }

        @Override
        public void onComplete() {
            lock.lock();
            try {
                completed.set(true);
                finished.signalAll();
            } finally {
                lock.unlock();
            }
        }

        @Override
        public void onError(final Throwable throwable) {
        }
    });
    publisher.attach(stream);
    lock.lock();
    try {
        while (running.get()) {
            finished.await();
        }
    } finally {
        lock.unlock();
    }
    publisher.close();
    lock.lock();
    try {
        while (!completed.get()) {
            finished.await();
        }
    } finally {
        lock.unlock();
    }
    assertThat(count.get()).isGreaterThanOrEqualTo(generations);
    Assert.assertTrue(completed.get());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerGene(io.jenetics.IntegerGene) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) Subscription(java.util.concurrent.Flow.Subscription) Test(org.testng.annotations.Test)

Example 8 with Subscription

use of java.util.concurrent.Flow.Subscription in project jenetics by jenetics.

the class StreamPublisherTest method creation.

@Test
public void creation() throws InterruptedException {
    final var lock = new ReentrantLock();
    final var finished = lock.newCondition();
    final var running = new AtomicBoolean(true);
    final var generation = new AtomicLong();
    final Stream<Long> stream = _engine.stream().limit(33).map(EvolutionResult::generation);
    try (var publisher = new StreamPublisher<Long>()) {
        publisher.subscribe(new Subscriber<>() {

            private Subscription _subscription;

            @Override
            public void onSubscribe(final Subscription subscription) {
                _subscription = subscription;
                _subscription.request(1);
            }

            @Override
            public void onNext(final Long g) {
                generation.set(g);
                _subscription.request(1);
            }

            @Override
            public void onError(final Throwable throwable) {
            }

            @Override
            public void onComplete() {
                lock.lock();
                try {
                    running.set(false);
                    finished.signal();
                } finally {
                    lock.unlock();
                }
            }
        });
        publisher.attach(stream);
        lock.lock();
        try {
            while (running.get()) {
                finished.await();
            }
        } finally {
            lock.unlock();
        }
    }
    Assert.assertEquals(generation.get(), 33);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) EvolutionResult(io.jenetics.engine.EvolutionResult) Subscription(java.util.concurrent.Flow.Subscription) Test(org.testng.annotations.Test)

Example 9 with Subscription

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

the class TestPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super T> subscriber) {
    subscribed = true;
    subscriber.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            if (n > 0) {
                Queue<T> items = new LinkedList<>(Arrays.asList(TestPublisher.this.items));
                for (; n > 0 && !items.isEmpty(); n--) {
                    subscriber.onNext(items.poll());
                }
                if (items.isEmpty()) {
                    subscriber.onComplete();
                }
            }
        }

        @Override
        public void cancel() {
        }
    });
}
Also used : Subscription(java.util.concurrent.Flow.Subscription) Queue(java.util.Queue)

Example 10 with Subscription

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

the class MultiPartDecoder method subscribe.

@Override
public void subscribe(Subscriber<? super ReadableBodyPart> subscriber) {
    Objects.requireNonNull(subscriber);
    if (!halfInit(UPSTREAM_INIT)) {
        Multi.<ReadableBodyPart>error(new IllegalStateException("Only one Subscriber allowed")).subscribe(subscriber);
        return;
    }
    this.downstream = subscriber;
    // contenders < 0, so any part request will be deferred
    // drain() will not request anything from upstream until the second deferredInit() invocation witnesses
    // that upstream is set
    downstream.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            long curr = n <= 0 ? partsRequested.getAndSet(-1) : partsRequested.getAndUpdate(v -> Long.MAX_VALUE - v > n ? v + n : v < 0 ? v : Long.MAX_VALUE);
            if (curr == 0) {
                drain();
            }
        }

        @Override
        public void cancel() {
            cancelled = true;
            if (partsRequested.getAndSet(-1) == 0) {
                drain();
            }
        }
    });
    deferredInit();
}
Also used : Subscription(java.util.concurrent.Flow.Subscription)

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