Search in sources :

Example 21 with Cancellable

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

the class CacheSingleTest method threeSubscribersOneLateQueueData.

@ParameterizedTest
@CsvSource(value = { "true,", "false,1", "false," })
void threeSubscribersOneLateQueueData(boolean onError, @Nullable Integer value) {
    Single<Integer> single = source.cache(2);
    toSource(single).subscribe(subscriber1);
    toSource(single).subscribe(subscriber2);
    Cancellable localSubscription1 = subscriber1.awaitSubscription();
    Cancellable localSubscription2 = subscriber2.awaitSubscription();
    if (onError) {
        source.onError(DELIBERATE_EXCEPTION);
        assertThat(subscriber1.awaitOnError(), is(DELIBERATE_EXCEPTION));
        assertThat(subscriber2.awaitOnError(), is(DELIBERATE_EXCEPTION));
    } else {
        source.onSuccess(value);
        assertThat(subscriber1.awaitOnSuccess(), is(value));
        assertThat(subscriber2.awaitOnSuccess(), is(value));
    }
    toSource(single).subscribe(subscriber3);
    Cancellable localSubscription3 = subscriber3.awaitSubscription();
    if (onError) {
        assertThat(subscriber3.awaitOnError(), is(DELIBERATE_EXCEPTION));
    } else {
        assertThat(subscriber3.awaitOnSuccess(), is(value));
    }
    // cancel after terminal shouldn't impact anything.
    localSubscription1.cancel();
    localSubscription2.cancel();
    localSubscription3.cancel();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with Cancellable

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

the class CacheSingleTest method singleSubscriberCancel.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void singleSubscriberCancel(boolean cancelUpstream) throws InterruptedException {
    toSource(source.cache(1, cancelUpstream)).subscribe(subscriber1);
    Cancellable subscription1 = subscriber1.awaitSubscription();
    subscription1.cancel();
    // multiple cancels should be safe.
    subscription1.cancel();
    if (cancelUpstream) {
        cancellable.awaitCancelled();
    } else {
        assertThat(cancellable.isCancelled(), is(false));
    }
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with Cancellable

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

the class CancellableSetTest method testAddAndRemove.

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

Example 24 with Cancellable

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

the class CancellableSetTest method duplicateAddDoesNotCancel.

@Test
void duplicateAddDoesNotCancel() {
    CancellableSet c = newCompositeCancellable();
    Cancellable cancellable = mock(Cancellable.class);
    int addCount = 0;
    if (add(c, cancellable)) {
        ++addCount;
    }
    if (add(c, cancellable)) {
        ++addCount;
    }
    verify(cancellable, never()).cancel();
    for (int i = 0; i < addCount; ++i) {
        assertTrue(c.remove(cancellable));
    }
    c.cancel();
    verify(cancellable, never()).cancel();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 25 with Cancellable

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

the class ClosableConcurrentStackTest method concurrentClosePushRemove.

@Test
void concurrentClosePushRemove() throws Exception {
    ClosableConcurrentStack<Cancellable> stack = new ClosableConcurrentStack<>();
    final int itemCount = 1000;
    CyclicBarrier barrier = new CyclicBarrier(itemCount + 1);
    List<Single<Cancellable>> completableList = new ArrayList<>(itemCount);
    for (int i = 0; i < itemCount; ++i) {
        final int finalI = i;
        completableList.add(EXECUTOR_RULE.executor().submit(() -> {
            Cancellable c = mock(Cancellable.class);
            try {
                barrier.await();
            } catch (Exception e) {
                throw new AssertionError(e);
            }
            assertTrue(stack.push(c), () -> "failed for index: " + finalI);
            return c;
        }));
    }
    Future<Collection<Cancellable>> future = collectUnordered(completableList, itemCount).toFuture();
    barrier.await();
    Collection<Cancellable> cancellables = future.get();
    stack.close(Cancellable::cancel);
    assertFalse(stack.push(() -> {
    }));
    for (Cancellable c : cancellables) {
        verify(c).cancel();
    }
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) ArrayList(java.util.ArrayList) CyclicBarrier(java.util.concurrent.CyclicBarrier) Collection(java.util.Collection) 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