Search in sources :

Example 26 with Cancellable

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

the class CompletableProcessorTest method synchronousCancelStillAllowsForGCDoSubscribe.

private WeakReference<Subscriber> synchronousCancelStillAllowsForGCDoSubscribe(CompletableProcessor processor, ReferenceQueue<Subscriber> queue) {
    Subscriber subscriber = new Subscriber() {

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

        @Override
        public void onComplete() {
        }

        @Override
        public void onError(final Throwable t) {
        }
    };
    processor.subscribe(subscriber);
    return new WeakReference<>(subscriber, queue);
}
Also used : TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) Cancellable(io.servicetalk.concurrent.Cancellable) WeakReference(java.lang.ref.WeakReference)

Example 27 with Cancellable

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

the class CompositeCancellableTest method testOne.

@Test
void testOne() {
    Cancellable composite = holder.init(1);
    assertThat("Composite of 1 must not create a new instance.", composite, is(holder.components[0]));
    composite.cancel();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 28 with Cancellable

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

the class DefaultExecutorTest method cancelExecute.

@ParameterizedTest(name = "{displayName} [{index}] {arguments}")
@EnumSource(ExecutorParam.class)
void cancelExecute(ExecutorParam executorParam) throws Throwable {
    executor = executorParam.get();
    assumeTrue(executorParam.supportsCancellation(), () -> "Ignoring executor: " + executorParam + ", it does not support cancellation.");
    CountDownLatch latch = new CountDownLatch(1);
    Task awaitTillCancelled = Task.awaitFor(latch);
    Cancellable cancellable = executor.execute(awaitTillCancelled);
    awaitTillCancelled.awaitStart();
    cancellable.cancel();
    Executable executable = () -> awaitTillCancelled.awaitDone();
    assertThrows(InterruptedException.class, executable);
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) Executable(org.junit.jupiter.api.function.Executable) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with Cancellable

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

the class AbstractCompositeCancellableTest method testCancel.

@Test
void testCancel() {
    T c = newCompositeCancellable();
    Cancellable cancellable = mock(Cancellable.class);
    add(c, cancellable);
    c.cancel();
    verify(cancellable).cancel();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 30 with Cancellable

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

the class AbstractCompositeCancellableTest method multiThreadedAddCancel.

@Test
void multiThreadedAddCancel() throws Exception {
    final int addThreads = 1000;
    final CyclicBarrier barrier = new CyclicBarrier(addThreads + 1);
    final List<Single<Cancellable>> cancellableSingles = new ArrayList<>(addThreads);
    T dynamicCancellable = newCompositeCancellable();
    for (int i = 0; i < addThreads; ++i) {
        cancellableSingles.add(EXECUTOR_RULE.executor().submit(() -> {
            Cancellable c = mock(Cancellable.class);
            barrier.await();
            add(dynamicCancellable, c);
            return c;
        }));
    }
    Future<Collection<Cancellable>> future = collectUnordered(cancellableSingles, addThreads).toFuture();
    barrier.await();
    dynamicCancellable.cancel();
    Collection<Cancellable> cancellables = future.get();
    for (Cancellable c : cancellables) {
        verify(c).cancel();
    }
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.jupiter.api.Test)

Aggregations

Cancellable (io.servicetalk.concurrent.Cancellable)74 Test (org.junit.jupiter.api.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)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