Search in sources :

Example 1 with LocatorNotAvailableException

use of com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentUnitTest method locatorOperationShouldRetryAndReturnResultIfLocatorException.

@Test
void locatorOperationShouldRetryAndReturnResultIfLocatorException() {
    AtomicInteger counter = new AtomicInteger();
    int result = StreamEnvironment.locatorOperation(c -> {
        if (counter.incrementAndGet() < 2) {
            throw new LocatorNotAvailableException();
        } else {
            return counter.get();
        }
    }, () -> null, BackOffDelayPolicy.fixed(Duration.ofMillis(10)));
    assertThat(result).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with LocatorNotAvailableException

use of com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentUnitTest method locatorOperationShouldThrowLocatorExceptionWhenRetryExhausts.

@Test
void locatorOperationShouldThrowLocatorExceptionWhenRetryExhausts() {
    AtomicInteger counter = new AtomicInteger();
    assertThatThrownBy(() -> StreamEnvironment.locatorOperation(c -> {
        counter.incrementAndGet();
        throw new LocatorNotAvailableException();
    }, () -> null, BackOffDelayPolicy.fixed(Duration.ofMillis(10)))).isInstanceOf(LocatorNotAvailableException.class);
    assertThat(counter).hasValue(3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with LocatorNotAvailableException

use of com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentUnitTest method locatorOperationShouldThrowInterruptedExceptionAsCauseIfInterrupted.

@Test
void locatorOperationShouldThrowInterruptedExceptionAsCauseIfInterrupted() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Exception> exception = new AtomicReference<>();
    Thread thread = new Thread(() -> {
        try {
            StreamEnvironment.locatorOperation(c -> {
                latch.countDown();
                throw new LocatorNotAvailableException();
            }, () -> null, BackOffDelayPolicy.fixed(Duration.ofMinutes(10)));
        } catch (StreamException e) {
            exception.set(e);
        }
    });
    thread.start();
    latchAssert(latch).completes();
    Thread.sleep(100);
    thread.interrupt();
    Thread.sleep(100);
    assertThat(exception.get()).isInstanceOf(StreamException.class).hasCauseInstanceOf(InterruptedException.class);
}
Also used : LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamException(com.rabbitmq.stream.StreamException) LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) StreamException(com.rabbitmq.stream.StreamException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

LocatorNotAvailableException (com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 StreamException (com.rabbitmq.stream.StreamException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1