Search in sources :

Example 11 with TestCancellable

use of io.servicetalk.concurrent.api.TestCancellable in project servicetalk by apple.

the class CompletableConcatWithPublisherTest method setUp.

@BeforeEach
void setUp() {
    subscriber = new TestPublisherSubscriber<>();
    cancellable = new TestCancellable();
    source = new TestCompletable.Builder().disableAutoOnSubscribe().build();
    next = new TestPublisher.Builder<Integer>().disableAutoOnSubscribe().build();
    subscription = new TestSubscription();
    toSource(source.concat(next)).subscribe(subscriber);
    source.onSubscribe(cancellable);
    subscriber.awaitSubscription();
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with TestCancellable

use of io.servicetalk.concurrent.api.TestCancellable in project servicetalk by apple.

the class CompletableConcatWithCompletablesTest method testCancelNext.

@ParameterizedTest
@ValueSource(ints = { 1, 2 })
void testCancelNext(int num) {
    initNexts(num);
    toSource(source.concat(nexts)).subscribe(subscriber);
    source.onComplete();
    assertThat(subscriber.pollTerminal(10, MILLISECONDS), is(nullValue()));
    subscriber.awaitSubscription().cancel();
    TestCancellable sourceCancellable = new TestCancellable();
    source.onSubscribe(sourceCancellable);
    assertFalse(sourceCancellable.isCancelled());
    TestCancellable nextCancellable = new TestCancellable();
    source.onSubscribe(nextCancellable);
    assertTrue(nextCancellable.isCancelled());
}
Also used : TestCancellable(io.servicetalk.concurrent.api.TestCancellable) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with TestCancellable

use of io.servicetalk.concurrent.api.TestCancellable in project servicetalk by apple.

the class SingleToPublisherTest method publishOnOriginalIsPreserved0.

private CountDownLatch publishOnOriginalIsPreserved0(final ConcurrentLinkedQueue<AssertionError> errors, final io.servicetalk.concurrent.test.internal.TestPublisherSubscriber<String> subscriber, final TestSingle<String> single, @Nullable final CountDownLatch receivedOnSuccessFromSingle) throws Exception {
    final Thread testThread = currentThread();
    CountDownLatch analyzed = new CountDownLatch(1);
    CountDownLatch receivedOnSubscribe = new CountDownLatch(1);
    toSource(single.publishOn(EXEC.executor()).beforeOnSuccess(__ -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked onSuccess " + "(from Single). Thread: " + currentThread()));
        }
    }).afterOnSuccess(__ -> {
        if (receivedOnSuccessFromSingle != null) {
            receivedOnSuccessFromSingle.countDown();
        }
    }).beforeOnError(__ -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked onError" + "(from Single). Thread: " + currentThread()));
        }
    }).toPublisher().beforeOnNext(__ -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked onNext " + "(from Publisher). Thread: " + currentThread()));
        }
    }).beforeOnError(__ -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked onError " + "(from Publisher). Thread: " + currentThread()));
        }
    }).afterOnSubscribe(__ -> receivedOnSubscribe.countDown()).afterOnComplete(analyzed::countDown).afterOnError(__ -> analyzed.countDown())).subscribe(subscriber);
    // await subscribe
    single.onSubscribe(new TestCancellable());
    receivedOnSubscribe.await();
    assertThat("Single not subscribed.", single.isSubscribed(), is(true));
    return analyzed;
}
Also used : TestUtils.assertNoAsyncErrors(io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) ExecutorExtension(io.servicetalk.concurrent.api.ExecutorExtension) TestSingle(io.servicetalk.concurrent.api.TestSingle) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Single(io.servicetalk.concurrent.api.Single) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Thread.currentThread(java.lang.Thread.currentThread) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Disabled(org.junit.jupiter.api.Disabled) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Executor(io.servicetalk.concurrent.api.Executor) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Nullable(javax.annotation.Nullable) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Thread.currentThread(java.lang.Thread.currentThread) TestCancellable(io.servicetalk.concurrent.api.TestCancellable)

Example 14 with TestCancellable

use of io.servicetalk.concurrent.api.TestCancellable in project servicetalk by apple.

the class SingleToPublisherTest method subscribeOnOriginalIsPreserved.

@Test
void subscribeOnOriginalIsPreserved() throws Exception {
    final Thread testThread = currentThread();
    final CountDownLatch analyzed = new CountDownLatch(1);
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    TestSingle<String> single = new TestSingle.Builder<String>().disableAutoOnSubscribe().build();
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    toSource(single.beforeCancel(() -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked cancel. Thread: " + currentThread()));
        }
    }).afterCancel(analyzed::countDown).subscribeOn(EXEC.executor()).toPublisher()).subscribe(subscriber);
    TestCancellable cancellable = new TestCancellable();
    // waits till subscribed.
    single.onSubscribe(cancellable);
    assertThat("Single not subscribed.", single.isSubscribed(), is(true));
    subscriber.awaitSubscription().cancel();
    analyzed.await();
    assertThat("Single did not get a cancel.", cancellable.isCancelled(), is(true));
    assertNoAsyncErrors(errors);
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Thread.currentThread(java.lang.Thread.currentThread) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Test(org.junit.jupiter.api.Test)

Example 15 with TestCancellable

use of io.servicetalk.concurrent.api.TestCancellable 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

TestCancellable (io.servicetalk.concurrent.api.TestCancellable)16 Test (org.junit.jupiter.api.Test)12 TestCompletable (io.servicetalk.concurrent.api.TestCompletable)4 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)3 Thread.currentThread (java.lang.Thread.currentThread)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Cancellable (io.servicetalk.concurrent.Cancellable)2 DelegatingExecutor (io.servicetalk.concurrent.api.DelegatingExecutor)2 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)2 TimeUnit (java.util.concurrent.TimeUnit)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 Executor (io.servicetalk.concurrent.api.Executor)1 ExecutorExtension (io.servicetalk.concurrent.api.ExecutorExtension)1 Single (io.servicetalk.concurrent.api.Single)1 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)1 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)1 TestSingle (io.servicetalk.concurrent.api.TestSingle)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1