Search in sources :

Example 16 with Subscription

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

the class StreamPublisherTest method publishLimitedStream.

@Test
public void publishLimitedStream() throws InterruptedException {
    final int generations = 20;
    final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
    final var stream = _engine.stream().limit(generations);
    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) {
            count.incrementAndGet();
            _subscription.request(1);
        }

        @Override
        public void onComplete() {
            lock.lock();
            try {
                running.set(false);
                completed.set(true);
                finished.signal();
            } 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();
    Assert.assertEquals(count.get(), 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) Subscription(java.util.concurrent.Flow.Subscription) Test(org.testng.annotations.Test)

Example 17 with Subscription

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

the class JdkFlowAdaptersTest method fromFlowCancel.

@Test
void fromFlowCancel() {
    AtomicReference<Subscription> receivedSubscription = new AtomicReference<>();
    Publisher<Integer> flowPublisher = newMockFlowPublisher((__, subscription) -> receivedSubscription.set(subscription));
    fromFlowPublisher(flowPublisher).firstOrElse(() -> null).toFuture().cancel(true);
    Subscription subscription = receivedSubscription.get();
    assertThat("Subscription not received.", subscription, is(notNullValue()));
    verify(subscription).cancel();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) 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 18 with Subscription

use of java.util.concurrent.Flow.Subscription in project julian-http-client by ljtfreitas.

the class ByteArrayHTTPMessageCodecTest method write.

@Test
void write() {
    byte[] expected = "response body".getBytes();
    HTTPRequestBody httpRequestBody = codec.write(expected, UTF_8);
    assertTrue(httpRequestBody.contentType().isEmpty());
    httpRequestBody.serialize().subscribe(new Subscriber<>() {

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

        @Override
        public void onNext(ByteBuffer item) {
            assertArrayEquals(expected, item.array());
        }

        @Override
        public void onError(Throwable throwable) {
            fail(throwable);
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : HTTPRequestBody(com.github.ljtfreitas.julian.http.HTTPRequestBody) Subscription(java.util.concurrent.Flow.Subscription) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 19 with Subscription

use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.

the class ItInternalTableScanTest method testExceptionRowScan.

/**
 * Checks that exception from storage cursor creation properly propagates to subscriber.
 */
@Test
public void testExceptionRowScan() throws Exception {
    // The latch that allows to await Subscriber.onError() before asserting test invariants.
    CountDownLatch gotExceptionLatch = new CountDownLatch(1);
    AtomicReference<Throwable> gotException = new AtomicReference<>();
    when(mockStorage.scan(any())).thenThrow(new StorageException("Some storage exception"));
    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);
            gotExceptionLatch.countDown();
        }

        @Override
        public void onComplete() {
            fail("Should never get here.");
        }
    });
    gotExceptionLatch.await();
    assertEquals(gotException.get().getCause().getClass(), StorageException.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) StorageException(org.apache.ignite.internal.storage.StorageException) Test(org.junit.jupiter.api.Test)

Example 20 with Subscription

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

the class SingleTest method testToFutureDoubleOnNext.

@Test
public void testToFutureDoubleOnNext() throws InterruptedException, ExecutionException {
    Single<String> single = new CompletionSingle<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    subscriber.onNext("foo");
                    subscriber.onNext("bar");
                    subscriber.onComplete();
                }

                @Override
                public void cancel() {
                }
            });
        }
    };
    Future<String> future = single.toStage().toCompletableFuture();
    assertThat(future.isDone(), is(equalTo(true)));
    assertThat(future.get(), is(equalTo("foo")));
}
Also used : Subscriber(java.util.concurrent.Flow.Subscriber) 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