Search in sources :

Example 1 with ExactNodeRetryClientFactory

use of com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory in project rabbitmq-stream-java-client by rabbitmq.

the class UtilsTest method exactNodeRetryClientFactoryShouldRetryUntilConditionOk.

@Test
@SuppressWarnings("unchecked")
void exactNodeRetryClientFactoryShouldRetryUntilConditionOk() {
    Client client = mock(Client.class);
    ClientFactory cf = mock(ClientFactory.class);
    when(cf.client(any())).thenReturn(client);
    Predicate<Client> condition = mock(Predicate.class);
    when(condition.test(any())).thenReturn(false).thenReturn(false).thenReturn(true);
    Client result = new ExactNodeRetryClientFactory(cf, condition, Duration.ofMillis(1)).client(ClientFactoryContext.fromParameters(new ClientParameters()));
    assertThat(result).isEqualTo(client);
    verify(cf, times(3)).client(any());
    verify(client, times(2)).close();
}
Also used : ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) ExactNodeRetryClientFactory(com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory) ExactNodeRetryClientFactory(com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory) Test(org.junit.jupiter.api.Test)

Example 2 with ExactNodeRetryClientFactory

use of com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory in project rabbitmq-stream-java-client by rabbitmq.

the class UtilsTest method exactNodeRetryClientFactoryShouldReturnImmediatelyIfConditionOk.

@Test
void exactNodeRetryClientFactoryShouldReturnImmediatelyIfConditionOk() {
    Client client = mock(Client.class);
    ClientFactory cf = mock(ClientFactory.class);
    when(cf.client(any())).thenReturn(client);
    Predicate<Client> condition = c -> true;
    Client result = new ExactNodeRetryClientFactory(cf, condition, Duration.ofMillis(1)).client(ClientFactoryContext.fromParameters(new ClientParameters()));
    assertThat(result).isEqualTo(client);
    verify(cf, times(1)).client(any());
    verify(client, never()).close();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ClientConnectionType(com.rabbitmq.stream.impl.Utils.ClientConnectionType) Mockito.verify(org.mockito.Mockito.verify) ClientFactoryContext(com.rabbitmq.stream.impl.Utils.ClientFactoryContext) RESPONSE_CODE_OK(com.rabbitmq.stream.Constants.RESPONSE_CODE_OK) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) ExactNodeRetryClientFactory(com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory) Duration(java.time.Duration) Utils.formatConstant(com.rabbitmq.stream.impl.Utils.formatConstant) CODE_MESSAGE_ENQUEUEING_FAILED(com.rabbitmq.stream.Constants.CODE_MESSAGE_ENQUEUEING_FAILED) RESPONSE_CODE_STREAM_DOES_NOT_EXIST(com.rabbitmq.stream.Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST) Utils.defaultConnectionNamingStrategy(com.rabbitmq.stream.impl.Utils.defaultConnectionNamingStrategy) Mockito.mock(org.mockito.Mockito.mock) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) ExactNodeRetryClientFactory(com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory) ExactNodeRetryClientFactory(com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ClientParameters (com.rabbitmq.stream.impl.Client.ClientParameters)2 ClientFactory (com.rabbitmq.stream.impl.Utils.ClientFactory)2 ExactNodeRetryClientFactory (com.rabbitmq.stream.impl.Utils.ExactNodeRetryClientFactory)2 Test (org.junit.jupiter.api.Test)2 CODE_MESSAGE_ENQUEUEING_FAILED (com.rabbitmq.stream.Constants.CODE_MESSAGE_ENQUEUEING_FAILED)1 RESPONSE_CODE_OK (com.rabbitmq.stream.Constants.RESPONSE_CODE_OK)1 RESPONSE_CODE_STREAM_DOES_NOT_EXIST (com.rabbitmq.stream.Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST)1 ClientConnectionType (com.rabbitmq.stream.impl.Utils.ClientConnectionType)1 ClientFactoryContext (com.rabbitmq.stream.impl.Utils.ClientFactoryContext)1 Utils.defaultConnectionNamingStrategy (com.rabbitmq.stream.impl.Utils.defaultConnectionNamingStrategy)1 Utils.formatConstant (com.rabbitmq.stream.impl.Utils.formatConstant)1 Duration (java.time.Duration)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.never (org.mockito.Mockito.never)1 Mockito.times (org.mockito.Mockito.times)1