Search in sources :

Example 1 with TestSingle

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

the class ReactiveStreamsAdaptersTest method singleToRSCancel.

@Test
void singleToRSCancel() {
    TestSingle<Integer> stSingle = new TestSingle<>();
    Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(stSingle);
    TestSubscription subscription = new TestSubscription();
    stSingle.onSubscribe(subscription);
    assertThat("Source not subscribed.", stSingle.isSubscribed(), is(true));
    ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(subscriber).onSubscribe(subscriptionCaptor.capture());
    subscriptionCaptor.getValue().cancel();
    assertThat("Subscription not cancelled.", subscription.isCancelled(), is(true));
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestSingle(io.servicetalk.concurrent.api.TestSingle) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 2 with TestSingle

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

Aggregations

TestSingle (io.servicetalk.concurrent.api.TestSingle)2 Test (org.junit.jupiter.api.Test)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 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)1 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)1 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)1 TestUtils.assertNoAsyncErrors (io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors)1 Thread.currentThread (java.lang.Thread.currentThread)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 Nullable (javax.annotation.Nullable)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.is (org.hamcrest.Matchers.is)1