Search in sources :

Example 66 with Cancellable

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

the class SingleZip4Test method cancelAfterCompleteOutOfOrder.

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

Example 67 with Cancellable

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

the class SingleZip5Test method cancelAfterCompleteOutOfOrder.

@Test
void cancelAfterCompleteOutOfOrder() throws InterruptedException {
    TestCancellable cancellable1 = new TestCancellable();
    TestSingle<Integer> first = new TestSingle.Builder<Integer>().disableAutoOnSubscribe().build(subscriber1 -> {
        subscriber1.onSubscribe(cancellable1);
        return subscriber1;
    });
    TestCancellable cancellable3 = new TestCancellable();
    TestSingle<Short> third = new TestSingle.Builder<Short>().disableAutoOnSubscribe().build(subscriber1 -> {
        subscriber1.onSubscribe(cancellable3);
        return subscriber1;
    });
    TestCancellable cancellable4 = new TestCancellable();
    TestSingle<Byte> fourth = new TestSingle.Builder<Byte>().disableAutoOnSubscribe().build(subscriber1 -> {
        subscriber1.onSubscribe(cancellable4);
        return subscriber1;
    });
    TestCancellable cancellable5 = new TestCancellable();
    TestSingle<Byte> fifth = new TestSingle.Builder<Byte>().disableAutoOnSubscribe().build(subscriber1 -> {
        subscriber1.onSubscribe(cancellable5);
        return subscriber1;
    });
    toSource(zip(combineFunc(), first, second, third, fourth, fifth)).subscribe(subscriber);
    Cancellable c = subscriber.awaitSubscription();
    second.onSuccess(10.1);
    c.cancel();
    cancellable1.awaitCancelled();
    cancellable3.awaitCancelled();
    cancellable4.awaitCancelled();
    cancellable5.awaitCancelled();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 68 with Cancellable

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

the class SourceAdaptersTest method completableFromSourceCancel.

@Test
void completableFromSourceCancel() {
    Cancellable srcCancellable = mock(Cancellable.class);
    CompletableSource source = s -> s.onSubscribe(srcCancellable);
    fromSource(source).toFuture().cancel(true);
    verify(srcCancellable).cancel();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SingleSource(io.servicetalk.concurrent.SingleSource) Cancellable(io.servicetalk.concurrent.Cancellable) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) ArgumentCaptor(org.mockito.ArgumentCaptor) EMPTY_SUBSCRIPTION(io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) PublisherSource(io.servicetalk.concurrent.PublisherSource) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Cancellable(io.servicetalk.concurrent.Cancellable) CompletableSource(io.servicetalk.concurrent.CompletableSource) Test(org.junit.jupiter.api.Test)

Example 69 with Cancellable

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

the class SourceAdaptersTest method singleFromSourceCancel.

@Test
void singleFromSourceCancel() {
    Cancellable srcCancellable = mock(Cancellable.class);
    SingleSource<Integer> source = s -> s.onSubscribe(srcCancellable);
    fromSource(source).toFuture().cancel(true);
    verify(srcCancellable).cancel();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SingleSource(io.servicetalk.concurrent.SingleSource) Cancellable(io.servicetalk.concurrent.Cancellable) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) ArgumentCaptor(org.mockito.ArgumentCaptor) EMPTY_SUBSCRIPTION(io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) PublisherSource(io.servicetalk.concurrent.PublisherSource) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 70 with Cancellable

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

the class TimeoutSingleTest method executorScheduleThrows.

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

        @Override
        public Cancellable schedule(final Runnable task, final long delay, final TimeUnit unit) {
            throw DELIBERATE_EXCEPTION;
        }
    })).subscribe(subscriber);
    assertThat(subscriber.awaitOnError(), sameInstance(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)

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