Search in sources :

Example 1 with ClientFactory

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

the class ProducersCoordinatorTest method init.

@BeforeEach
void init() {
    Client.ClientParameters clientParameters = new Client.ClientParameters() {

        @Override
        public Client.ClientParameters shutdownListener(Client.ShutdownListener shutdownListener) {
            ProducersCoordinatorTest.this.shutdownListener = shutdownListener;
            return super.shutdownListener(shutdownListener);
        }

        @Override
        public Client.ClientParameters metadataListener(Client.MetadataListener metadataListener) {
            ProducersCoordinatorTest.this.metadataListener = metadataListener;
            return super.metadataListener(metadataListener);
        }
    };
    mocks = MockitoAnnotations.openMocks(this);
    when(environment.locator()).thenReturn(locator);
    when(environment.locatorOperation(any())).thenCallRealMethod();
    when(environment.clientParametersCopy()).thenReturn(clientParameters);
    when(environment.addressResolver()).thenReturn(address -> address);
    when(trackingConsumer.stream()).thenReturn("stream");
    when(client.declarePublisher(anyByte(), isNull(), anyString())).thenReturn(new Response(Constants.RESPONSE_CODE_OK));
    coordinator = new ProducersCoordinator(environment, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT, ProducersCoordinator.MAX_TRACKING_CONSUMERS_PER_CLIENT, type -> "producer-connection", clientFactory);
}
Also used : Response(com.rabbitmq.stream.impl.Client.Response) 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) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ClientFactory

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

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

the class ConsumersCoordinatorTest method shouldRetryUntilGettingExactNodeWithAdvertisedHostNameClientFactoryAndNotExactNodeOnFirstTime.

@Test
void shouldRetryUntilGettingExactNodeWithAdvertisedHostNameClientFactoryAndNotExactNodeOnFirstTime() {
    ClientFactory cf = context -> Utils.connectToAdvertisedNodeClientFactory(context.key(), clientFactory, Duration.ofMillis(1)).client(context);
    ConsumersCoordinator c = new ConsumersCoordinator(environment, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT, type -> "consumer-connection", cf);
    when(locator.metadata("stream")).thenReturn(metadata(null, replica()));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    when(client.serverAdvertisedHost()).thenReturn("foo").thenReturn(replica().get(0).getHost());
    when(client.serverAdvertisedPort()).thenReturn(42).thenReturn(replica().get(0).getPort());
    c.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(clientFactory, times(2)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) MockitoAnnotations(org.mockito.MockitoAnnotations) StreamException(com.rabbitmq.stream.StreamException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ConsumersPoolInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumersPoolInfo) MethodSource(org.junit.jupiter.params.provider.MethodSource) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) TestUtils.namedConsumer(com.rabbitmq.stream.impl.TestUtils.namedConsumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessageListener(com.rabbitmq.stream.impl.Client.MessageListener) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ArgumentMatchers.anyByte(org.mockito.ArgumentMatchers.anyByte) List(java.util.List) Stream(java.util.stream.Stream) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Mockito.mock(org.mockito.Mockito.mock) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Mock(org.mockito.Mock) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) BackOffDelayPolicy.fixedWithInitialDelay(com.rabbitmq.stream.BackOffDelayPolicy.fixedWithInitialDelay) Captor(org.mockito.Captor) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) ExecutorService(java.util.concurrent.ExecutorService) SubscriptionListener(com.rabbitmq.stream.SubscriptionListener) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ClientFactory

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

the class ConsumersCoordinatorTest method shouldRemoveClientSubscriptionManagerFromPoolIfEmptyAfterMetadataUpdate.

@Test
void shouldRemoveClientSubscriptionManagerFromPoolIfEmptyAfterMetadataUpdate() throws Exception {
    BackOffDelayPolicy delayPolicy = fixedWithInitialDelay(ms(50), ms(50));
    when(environment.topologyUpdateBackOffDelayPolicy()).thenReturn(delayPolicy);
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    when(environment.scheduledExecutorService()).thenReturn(scheduledExecutorService);
    when(consumer.isOpen()).thenReturn(true);
    when(locator.metadata("stream")).thenReturn(metadata(null, replicas().subList(0, 1)));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    int extraSubscriptionCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT / 5;
    int subscriptionCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT + extraSubscriptionCount;
    IntStream.range(0, subscriptionCount).forEach(i -> {
        coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
        });
    });
    // the extra is allocated on another client from the same pool
    verify(clientFactory, times(2)).client(any());
    verify(client, times(subscriptionCount)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    List<ConsumersPoolInfo> info = MonitoringTestUtils.extract(coordinator);
    assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount);
    // let's kill the first client connection
    metadataListeners.get(0).handle("stream", Constants.RESPONSE_CODE_STREAM_NOT_AVAILABLE);
    Thread.sleep(delayPolicy.delay(0).toMillis() * 5);
    info = MonitoringTestUtils.extract(coordinator);
    assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount);
    // the MAX consumers must have been re-allocated to the existing client and a new one
    // let's add a new subscription to make sure we are still using the same pool
    coordinator.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(clientFactory, times(2 + 1)).client(any());
    verify(client, times(subscriptionCount + ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT + 1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
    info = MonitoringTestUtils.extract(coordinator);
    assertThat(info).hasSize(1).element(0).extracting(pool -> pool.consumerCount()).isEqualTo(subscriptionCount + 1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) MockitoAnnotations(org.mockito.MockitoAnnotations) StreamException(com.rabbitmq.stream.StreamException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ConsumersPoolInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumersPoolInfo) MethodSource(org.junit.jupiter.params.provider.MethodSource) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) TestUtils.namedConsumer(com.rabbitmq.stream.impl.TestUtils.namedConsumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessageListener(com.rabbitmq.stream.impl.Client.MessageListener) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ArgumentMatchers.anyByte(org.mockito.ArgumentMatchers.anyByte) List(java.util.List) Stream(java.util.stream.Stream) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Mockito.mock(org.mockito.Mockito.mock) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Mock(org.mockito.Mock) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) BackOffDelayPolicy.fixedWithInitialDelay(com.rabbitmq.stream.BackOffDelayPolicy.fixedWithInitialDelay) Captor(org.mockito.Captor) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) ExecutorService(java.util.concurrent.ExecutorService) SubscriptionListener(com.rabbitmq.stream.SubscriptionListener) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) ConsumersPoolInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumersPoolInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ClientFactory

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

the class ConsumersCoordinatorTest method shouldGetExactNodeImmediatelyWithAdvertisedHostNameClientFactoryAndExactNodeOnFirstTime.

@Test
void shouldGetExactNodeImmediatelyWithAdvertisedHostNameClientFactoryAndExactNodeOnFirstTime() {
    ClientFactory cf = context -> Utils.connectToAdvertisedNodeClientFactory(context.key(), clientFactory, Duration.ofMillis(1)).client(context);
    ConsumersCoordinator c = new ConsumersCoordinator(environment, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT, type -> "consumer-connection", cf);
    when(locator.metadata("stream")).thenReturn(metadata(null, replica()));
    when(clientFactory.client(any())).thenReturn(client);
    when(client.subscribe(subscriptionIdCaptor.capture(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap())).thenReturn(new Client.Response(Constants.RESPONSE_CODE_OK));
    when(client.serverAdvertisedHost()).thenReturn(replica().get(0).getHost());
    when(client.serverAdvertisedPort()).thenReturn(replica().get(0).getPort());
    c.subscribe(consumer, "stream", OffsetSpecification.first(), null, NO_OP_SUBSCRIPTION_LISTENER, (offset, message) -> {
    });
    verify(clientFactory, times(1)).client(any());
    verify(client, times(1)).subscribe(anyByte(), anyString(), any(OffsetSpecification.class), anyInt(), anyMap());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) MockitoAnnotations(org.mockito.MockitoAnnotations) StreamException(com.rabbitmq.stream.StreamException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ConsumersPoolInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumersPoolInfo) MethodSource(org.junit.jupiter.params.provider.MethodSource) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) TestUtils.namedConsumer(com.rabbitmq.stream.impl.TestUtils.namedConsumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessageListener(com.rabbitmq.stream.impl.Client.MessageListener) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ArgumentMatchers.anyByte(org.mockito.ArgumentMatchers.anyByte) List(java.util.List) Stream(java.util.stream.Stream) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Mockito.mock(org.mockito.Mockito.mock) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Mock(org.mockito.Mock) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) BackOffDelayPolicy.fixedWithInitialDelay(com.rabbitmq.stream.BackOffDelayPolicy.fixedWithInitialDelay) Captor(org.mockito.Captor) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) ExecutorService(java.util.concurrent.ExecutorService) SubscriptionListener(com.rabbitmq.stream.SubscriptionListener) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collections(java.util.Collections) TestUtils.metadata(com.rabbitmq.stream.impl.TestUtils.metadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) ClientFactory(com.rabbitmq.stream.impl.Utils.ClientFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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