Search in sources :

Example 1 with DelegatingExecutor

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

the class TimeoutCompletableTest method executorScheduleThrows.

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

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

Example 2 with DelegatingExecutor

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

the class RoundRobinLoadBalancerTest method healthCheckRecoversFromUnexpectedError.

@Test
void healthCheckRecoversFromUnexpectedError() throws Exception {
    serviceDiscoveryPublisher.onComplete();
    final Single<TestLoadBalancedConnection> properConnection = newRealizedConnectionSingle("address-1");
    final UnhealthyHostConnectionFactory unhealthyHostConnectionFactory = new UnhealthyHostConnectionFactory("address-1", 4, properConnection);
    final DelegatingConnectionFactory connectionFactory = unhealthyHostConnectionFactory.createFactory();
    final AtomicInteger scheduleCnt = new AtomicInteger();
    lb = (RoundRobinLoadBalancer<String, TestLoadBalancedConnection>) new RoundRobinLoadBalancerFactory.Builder<String, TestLoadBalancedConnection>().healthCheckFailedConnectionsThreshold(1).backgroundExecutor(new DelegatingExecutor(testExecutor) {

        @Override
        public Completable timer(final long delay, final TimeUnit unit) {
            if (scheduleCnt.incrementAndGet() == 2) {
                throw DELIBERATE_EXCEPTION;
            }
            return delegate().timer(delay, unit);
        }
    }).build().newLoadBalancer("test-service", serviceDiscoveryPublisher, connectionFactory);
    sendServiceDiscoveryEvents(upEvent("address-1"));
    // Trigger first health check:
    Exception e = assertThrows(ExecutionException.class, () -> lb.selectConnection(any()).toFuture().get());
    assertThat(e.getCause(), is(UNHEALTHY_HOST_EXCEPTION));
    // second - due to an unexpected error from executor:
    for (int i = 0; i < 2; ++i) {
        unhealthyHostConnectionFactory.advanceTime(testExecutor);
    }
    // Trigger yet another health check:
    e = assertThrows(ExecutionException.class, () -> lb.selectConnection(any()).toFuture().get());
    assertThat(e.getCause(), is(UNHEALTHY_HOST_EXCEPTION));
    // Execute two health checks: first will fail due to connectionFactory, second succeeds:
    for (int i = 0; i < 2; ++i) {
        unhealthyHostConnectionFactory.advanceTime(testExecutor);
    }
    // Make sure we can select a connection:
    final TestLoadBalancedConnection selectedConnection = lb.selectConnection(any()).toFuture().get();
    assertThat(selectedConnection, equalTo(properConnection.toFuture().get()));
    assertThat(unhealthyHostConnectionFactory.requests.get(), equalTo(5));
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) 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) DelegatingExecutor(io.servicetalk.concurrent.api.DelegatingExecutor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 3 with DelegatingExecutor

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

Example 4 with DelegatingExecutor

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

the class TimeoutPublisherTest method executorScheduleThrowsTerminalTimeout.

@Test
void executorScheduleThrowsTerminalTimeout() {
    toSource(publisher.timeoutTerminal(1, NANOSECONDS, new DelegatingExecutor(testExecutor) {

        @Override
        public Cancellable schedule(final Runnable task, final long delay, final TimeUnit unit) {
            throw DELIBERATE_EXCEPTION;
        }
    })).subscribe(subscriber);
    publisher.onSubscribe(subscription);
    assertThat(subscriber.awaitOnError(), sameInstance(DELIBERATE_EXCEPTION));
    assertTrue(subscription.isCancelled());
}
Also used : DelegatingExecutor(io.servicetalk.concurrent.api.DelegatingExecutor) Cancellable(io.servicetalk.concurrent.Cancellable) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with DelegatingExecutor

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

the class TimeoutPublisherTest method executorScheduleThrowsIdleTimeout.

@Test
void executorScheduleThrowsIdleTimeout() {
    toSource(publisher.timeout(1, NANOSECONDS, new DelegatingExecutor(testExecutor) {

        @Override
        public Cancellable schedule(final Runnable task, final long delay, final TimeUnit unit) {
            throw DELIBERATE_EXCEPTION;
        }
    })).subscribe(subscriber);
    publisher.onSubscribe(subscription);
    assertThat(subscriber.awaitOnError(), sameInstance(DELIBERATE_EXCEPTION));
    assertTrue(subscription.isCancelled());
}
Also used : DelegatingExecutor(io.servicetalk.concurrent.api.DelegatingExecutor) Cancellable(io.servicetalk.concurrent.Cancellable) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

DelegatingExecutor (io.servicetalk.concurrent.api.DelegatingExecutor)5 TimeUnit (java.util.concurrent.TimeUnit)5 Test (org.junit.jupiter.api.Test)5 Cancellable (io.servicetalk.concurrent.Cancellable)4 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ConnectionRejectedException (io.servicetalk.client.api.ConnectionRejectedException)1 NoAvailableHostException (io.servicetalk.client.api.NoAvailableHostException)1 Completable (io.servicetalk.concurrent.api.Completable)1 DeliberateException (io.servicetalk.concurrent.internal.DeliberateException)1 NoSuchElementException (java.util.NoSuchElementException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1