Search in sources :

Example 6 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class CompositeCancellable method create.

/**
 * Creates new instance of {@link Cancellable} which is a composite of {@code toCompose}.
 *
 * @param toCompose All {@link Cancellable} to compose.
 * @return A composite {@link Cancellable} for all {@code toCompose}.
 */
static Cancellable create(Cancellable... toCompose) {
    switch(toCompose.length) {
        case 0:
            throw new IllegalArgumentException("At least one Cancellable required to compose.");
        case 1:
            return requireNonNull(toCompose[0]);
        case 2:
            Cancellable first = requireNonNull(toCompose[0]);
            Cancellable second = requireNonNull(toCompose[1]);
            return () -> {
                try {
                    first.cancel();
                } finally {
                    second.cancel();
                }
            };
        default:
            return new CompositeCancellable(toCompose);
    }
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable)

Example 7 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class RunnableCompletableTest method cancelInterrupts.

@Test
void cancelInterrupts() throws Exception {
    final Completable source = Completable.fromRunnable(factory);
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        try {
            // await till interrupted.
            latch.await();
        } catch (InterruptedException e) {
            latch.countDown();
            Thread.currentThread().interrupt();
            throw e;
        }
        return 1;
    }).when(factory).run();
    toSource(source).subscribe(new CompletableSource.Subscriber() {

        @Override
        public void onSubscribe(final Cancellable cancellable) {
            cancellable.cancel();
        }

        @Override
        public void onComplete() {
        // noop
        }

        @Override
        public void onError(final Throwable t) {
        // noop
        }
    });
    latch.await();
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) Cancellable(io.servicetalk.concurrent.Cancellable) CompletableSource(io.servicetalk.concurrent.CompletableSource) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class TimeoutCompletableTest method executorScheduleThrows.

@Test
void executorScheduleThrows() {
    toSource(source.ignoreElement().timeout(1, NANOSECONDS, new DelegatingExecutor(testExecutor) {

        @Override
        public Cancellable schedule(final Runnable task, final long delay, final TimeUnit unit) {
            throw DELIBERATE_EXCEPTION;
        }
    })).subscribe(listener);
    assertThat(listener.awaitOnError(), is(DELIBERATE_EXCEPTION));
    TestCancellable cancellable = new TestCancellable();
    source.onSubscribe(cancellable);
    assertTrue(cancellable.isCancelled());
}
Also used : DelegatingExecutor(io.servicetalk.concurrent.api.DelegatingExecutor) Cancellable(io.servicetalk.concurrent.Cancellable) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) TimeUnit(java.util.concurrent.TimeUnit) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Test(org.junit.jupiter.api.Test)

Example 9 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class ShareContextOnSubscribeTest method awaitTermination.

private void awaitTermination(Completable completable) throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    // completable.toFuture() will use the toSingle() conversion and share context operator will not be effective
    // since it isn't the last operator. So we directly subscribe to the completable.
    toSource(completable).subscribe(new CompletableSource.Subscriber() {

        @Override
        public void onSubscribe(final Cancellable cancellable) {
        // Noop
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }

        @Override
        public void onError(final Throwable t) {
            latch.countDown();
        }
    });
    latch.await();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) CompletableSource(io.servicetalk.concurrent.CompletableSource) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class SingleZipWithTest method cancelAfterCompleteOutOfOrder.

@Test
void cancelAfterCompleteOutOfOrder() throws InterruptedException {
    TestCancellable cancellable = new TestCancellable();
    TestSingle<Integer> first = new TestSingle.Builder<Integer>().disableAutoOnSubscribe().build(subscriber1 -> {
        subscriber1.onSubscribe(cancellable);
        return subscriber1;
    });
    toSource(first.zipWith(second, SingleZipWithTest::combine)).subscribe(subscriber);
    Cancellable c = subscriber.awaitSubscription();
    second.onSuccess(10.1);
    c.cancel();
    cancellable.awaitCancelled();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Aggregations

Cancellable (io.servicetalk.concurrent.Cancellable)68 Test (org.junit.jupiter.api.Test)46 CountDownLatch (java.util.concurrent.CountDownLatch)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)12 SingleSource (io.servicetalk.concurrent.SingleSource)11 Matchers.is (org.hamcrest.Matchers.is)10 CompletableSource (io.servicetalk.concurrent.CompletableSource)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)7 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)7 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)7 Subscriber (io.servicetalk.concurrent.CompletableSource.Subscriber)6 Executor (io.servicetalk.concurrent.api.Executor)6 Single (io.servicetalk.concurrent.api.Single)6 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)6 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)6 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)6 TimeUnit (java.util.concurrent.TimeUnit)6 PublisherSource (io.servicetalk.concurrent.PublisherSource)5 Completable (io.servicetalk.concurrent.api.Completable)5