Search in sources :

Example 36 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class AfterFinallyTest method testCallbackThrowsErrorOnError.

@Test
@Override
void testCallbackThrowsErrorOnError() {
    TerminalSignalConsumer mock = throwableMock(new DeliberateException());
    toSource(doFinally(Completable.failed(DELIBERATE_EXCEPTION), mock)).subscribe(listener);
    assertThat(listener.awaitOnError(), is(DELIBERATE_EXCEPTION));
    verify(mock).onError(DELIBERATE_EXCEPTION);
    verifyNoMoreInteractions(mock);
}
Also used : TerminalSignalConsumer(io.servicetalk.concurrent.api.TerminalSignalConsumer) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 37 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class BeforeFinallyTest method testCallbackThrowsErrorOnError.

@Test
@Override
void testCallbackThrowsErrorOnError() {
    DeliberateException exception = new DeliberateException();
    TerminalSignalConsumer mock = throwableMock(exception);
    toSource(doFinally(Completable.failed(DELIBERATE_EXCEPTION), mock)).subscribe(listener);
    assertThat(listener.awaitOnError(), is(exception));
    assertThat(listener.awaitOnError().getSuppressed()[0], is(DELIBERATE_EXCEPTION));
    verify(mock).onError(DELIBERATE_EXCEPTION);
    verifyNoMoreInteractions(mock);
}
Also used : TerminalSignalConsumer(io.servicetalk.concurrent.api.TerminalSignalConsumer) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 38 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class AbstractWhenCancelTest method testCallbackThrowsError.

@Test
void testCallbackThrowsError() {
    LegacyTestCompletable completable = new LegacyTestCompletable();
    DeliberateException e = assertThrows(DeliberateException.class, () -> {
        try {
            toSource(doCancel(completable, () -> {
                throw DELIBERATE_EXCEPTION;
            })).subscribe(listener);
            listener.awaitSubscription().cancel();
        } finally {
            completable.verifyCancelled();
        }
    });
    assertThat(e, is(sameInstance(DELIBERATE_EXCEPTION)));
}
Also used : LegacyTestCompletable(io.servicetalk.concurrent.api.LegacyTestCompletable) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 39 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class RoundRobinLoadBalancerTest method hostUnhealthyDoesntRaceToRunHealthCheck.

// Concurrency test, run multiple times (at least 1000).
@Test
void hostUnhealthyDoesntRaceToRunHealthCheck() throws Exception {
    serviceDiscoveryPublisher.onComplete();
    final Single<TestLoadBalancedConnection> properConnection = newRealizedConnectionSingle("address-1");
    final int timeAdvancementsTillHealthy = 3;
    final UnhealthyHostConnectionFactory unhealthyHostConnectionFactory = new UnhealthyHostConnectionFactory("address-1", timeAdvancementsTillHealthy, properConnection);
    final DelegatingConnectionFactory connectionFactory = unhealthyHostConnectionFactory.createFactory();
    lb = defaultLb(connectionFactory);
    sendServiceDiscoveryEvents(upEvent("address-1"));
    // Imitate concurrency by running multiple threads attempting to establish connections.
    ExecutorService executor = Executors.newFixedThreadPool(3);
    try {
        final Runnable runnable = () -> assertThrows(ExecutionException.class, () -> lb.selectConnection(any(), null).toFuture().get());
        for (int i = 0; i < 1000; i++) {
            executor.submit(runnable);
        }
        // From test main thread, wait until the host becomes UNHEALTHY, which is apparent from
        // NoHostAvailableException being thrown from selection AFTER a health check was scheduled by any thread.
        final Executor executorForRetries = io.servicetalk.concurrent.api.Executors.newFixedSizeExecutor(1);
        try {
            awaitIndefinitely(lb.selectConnection(any(), null).retryWhen(retryWithConstantBackoffFullJitter((t) -> t instanceof DeliberateException || testExecutor.scheduledTasksPending() == 0, // try to prevent stack overflow
            Duration.ofMillis(30), executorForRetries)));
        } catch (Exception e) {
            assertThat(e.getCause(), instanceOf(NoAvailableHostException.class));
        } finally {
            executorForRetries.closeAsync().toFuture().get();
        }
        // is not selected. If our assumption doesn't hold, it means more than one health check was scheduled.
        for (int i = 0; i < timeAdvancementsTillHealthy - 1; ++i) {
            unhealthyHostConnectionFactory.advanceTime(testExecutor);
            // Assert still unhealthy
            Exception e = assertThrows(ExecutionException.class, () -> lb.selectConnection(any(), null).toFuture().get());
            assertThat(e.getCause(), instanceOf(NoAvailableHostException.class));
        }
    } finally {
        // Shutdown the concurrent validation of unhealthiness.
        executor.shutdownNow();
        executor.awaitTermination(10, SECONDS);
    }
    unhealthyHostConnectionFactory.advanceTime(testExecutor);
    final TestLoadBalancedConnection selectedConnection = lb.selectConnection(any(), null).toFuture().get();
    assertThat(selectedConnection, equalTo(properConnection.toFuture().get()));
}
Also used : Executor(io.servicetalk.concurrent.api.Executor) DelegatingExecutor(io.servicetalk.concurrent.api.DelegatingExecutor) TestExecutor(io.servicetalk.concurrent.api.TestExecutor) NoAvailableHostException(io.servicetalk.client.api.NoAvailableHostException) ExecutorService(java.util.concurrent.ExecutorService) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) ConnectionRejectedException(io.servicetalk.client.api.ConnectionRejectedException) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) NoSuchElementException(java.util.NoSuchElementException) NoAvailableHostException(io.servicetalk.client.api.NoAvailableHostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 40 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class PublisherConcatMapIterableTest method exceptionFromOnCompleteIsPropagated.

@Test
void exceptionFromOnCompleteIsPropagated() {
    toSource(publisher.flatMapConcatIterable(identity()).afterOnComplete(() -> {
        throw DELIBERATE_EXCEPTION;
    })).subscribe(subscriber);
    subscriber.awaitSubscription();
    DeliberateException exception = assertThrows(DeliberateException.class, () -> publisher.onComplete());
    assertThat(exception, is(DELIBERATE_EXCEPTION));
}
Also used : DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Aggregations

DeliberateException (io.servicetalk.concurrent.internal.DeliberateException)80 Test (org.junit.jupiter.api.Test)77 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)6 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)5 PublisherSource (io.servicetalk.concurrent.PublisherSource)4 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)4 TerminalSignalConsumer (io.servicetalk.concurrent.api.TerminalSignalConsumer)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Subscriber (io.servicetalk.concurrent.PublisherSource.Subscriber)3 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)3 PlatformDependent.throwException (io.servicetalk.utils.internal.PlatformDependent.throwException)3 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)3