Search in sources :

Example 6 with ClientFactory

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

the class ProducersCoordinatorTest method shouldGetExactNodeImmediatelyWithAdvertisedHostNameClientFactoryAndExactNodeOnFirstTime.

@Test
void shouldGetExactNodeImmediatelyWithAdvertisedHostNameClientFactoryAndExactNodeOnFirstTime() {
    ClientFactory cf = context -> Utils.connectToAdvertisedNodeClientFactory(context.key(), clientFactory, Duration.ofMillis(1)).client(context);
    ProducersCoordinator c = new ProducersCoordinator(environment, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT, ProducersCoordinator.MAX_TRACKING_CONSUMERS_PER_CLIENT, type -> "producer-connection", cf);
    when(locator.metadata("stream")).thenReturn(metadata(leader(), replicas()));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.serverAdvertisedHost()).thenReturn(leader().getHost());
    when(client.serverAdvertisedPort()).thenReturn(leader().getPort());
    Runnable cleanTask = c.registerProducer(producer, null, "stream");
    verify(clientFactory, times(1)).client(any());
    verify(producer, times(1)).setClient(client);
    cleanTask.run();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.answer(com.rabbitmq.stream.impl.TestUtils.answer) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Mockito.times(org.mockito.Mockito.times) Mockito.anyByte(org.mockito.Mockito.anyByte) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito.never(org.mockito.Mockito.never) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Test(org.junit.jupiter.api.Test)

Example 7 with ClientFactory

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

the class ProducersCoordinatorTest method shouldRetryUntilGettingExactNodeWithAdvertisedHostNameClientFactoryAndNotExactNodeOnFirstTime.

@Test
void shouldRetryUntilGettingExactNodeWithAdvertisedHostNameClientFactoryAndNotExactNodeOnFirstTime() {
    ClientFactory cf = context -> Utils.connectToAdvertisedNodeClientFactory(context.key(), clientFactory, Duration.ofMillis(1)).client(context);
    ProducersCoordinator c = new ProducersCoordinator(environment, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT, ProducersCoordinator.MAX_TRACKING_CONSUMERS_PER_CLIENT, type -> "producer-connection", cf);
    when(locator.metadata("stream")).thenReturn(metadata(leader(), replicas()));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.serverAdvertisedHost()).thenReturn("foo").thenReturn(leader().getHost());
    when(client.serverAdvertisedPort()).thenReturn(42).thenReturn(leader().getPort());
    Runnable cleanTask = c.registerProducer(producer, null, "stream");
    verify(clientFactory, times(2)).client(any());
    verify(producer, times(1)).setClient(client);
    cleanTask.run();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.answer(com.rabbitmq.stream.impl.TestUtils.answer) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Mockito.times(org.mockito.Mockito.times) Mockito.anyByte(org.mockito.Mockito.anyByte) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito.never(org.mockito.Mockito.never) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Test(org.junit.jupiter.api.Test)

Example 8 with ClientFactory

use of com.rabbitmq.stream.impl.Utils.ClientFactory 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

ClientFactory (com.rabbitmq.stream.impl.Utils.ClientFactory)8 Test (org.junit.jupiter.api.Test)8 Duration (java.time.Duration)7 IntStream (java.util.stream.IntStream)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.times (org.mockito.Mockito.times)7 Mockito.verify (org.mockito.Mockito.verify)7 Mockito.when (org.mockito.Mockito.when)7 BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)6 Constants (com.rabbitmq.stream.Constants)6 StreamDoesNotExistException (com.rabbitmq.stream.StreamDoesNotExistException)6 TestUtils.metadata (com.rabbitmq.stream.impl.TestUtils.metadata)6 ArrayList (java.util.ArrayList)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Executors (java.util.concurrent.Executors)6