Search in sources :

Example 16 with ClientParameters

use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingTest method storeOffsetAndThenAttachByTimestampShouldWork.

@Test
void storeOffsetAndThenAttachByTimestampShouldWork() throws Exception {
    // this test performs a timestamp-based index search within a segment with
    // a lot of non-user entries (chunks that contain tracking info, not messages)
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> confirmLatch = new AtomicReference<>(new CountDownLatch(messageCount));
    AtomicInteger consumed = new AtomicInteger();
    Client client = cf.get(new ClientParameters().publishConfirmListener((publisherId, publishingId) -> confirmLatch.get().countDown()).chunkListener((client1, subscriptionId, offset, messageCount1, dataSize) -> client1.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> consumed.incrementAndGet()));
    assertThat(client.declarePublisher((byte) 0, null, stream).isOk()).isTrue();
    Runnable publishAction = () -> {
        IntStream.range(0, messageCount).forEach(i -> client.publish((byte) 0, Collections.singletonList(client.codec().messageBuilder().addData("hello world".getBytes(StandardCharsets.UTF_8)).build())));
    };
    publishAction.run();
    assertThat(latchAssert(confirmLatch)).completes();
    IntStream.range(0, messageCount).forEach(i -> client.storeOffset("some reference", stream, i));
    waitAtMost(() -> client.queryOffset("some reference", stream).getOffset() == messageCount - 1);
    confirmLatch.set(new CountDownLatch(messageCount));
    long betweenTwoWaves = System.currentTimeMillis();
    publishAction.run();
    assertThat(latchAssert(confirmLatch)).completes();
    assertThat(consumed.get()).isZero();
    client.subscribe((byte) 0, stream, OffsetSpecification.timestamp(betweenTwoWaves), 10);
    waitAtMost(() -> consumed.get() == messageCount);
}
Also used : IntStream(java.util.stream.IntStream) CsvSource(org.junit.jupiter.params.provider.CsvSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Callable(java.util.concurrent.Callable) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) Future(java.util.concurrent.Future) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils.forEach(com.rabbitmq.stream.impl.TestUtils.forEach) BiConsumer(java.util.function.BiConsumer) QueryOffsetResponse(com.rabbitmq.stream.impl.Client.QueryOffsetResponse) ExecutorService(java.util.concurrent.ExecutorService) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Tuple(io.vavr.Tuple) Logger(org.slf4j.Logger) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MessageListener(com.rabbitmq.stream.impl.Client.MessageListener) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Tuple3(io.vavr.Tuple3) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with ClientParameters

use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.

the class PublisherTest method deduplication.

@ParameterizedTest
@MethodSource
void deduplication(String publisherReference, int messageCount, int duplicatedCount, int expectedConsumed) throws Exception {
    CountDownLatch publishLatch = new CountDownLatch(messageCount + duplicatedCount);
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    AtomicInteger consumeCount = new AtomicInteger();
    Client c = cf.get(new ClientParameters().publishConfirmListener((pubId, publishingId) -> publishLatch.countDown()).chunkListener((client, subscriptionId, offset, messageCount1, dataSize) -> client.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
        consumeCount.incrementAndGet();
        consumeLatch.countDown();
    }));
    Response response = c.declarePublisher(b(1), publisherReference, stream);
    assertThat(response.isOk()).isTrue();
    AtomicLong publishingSequence = new AtomicLong(0);
    ToLongFunction<Object> publishingSequenceFunction = o -> publishingSequence.incrementAndGet();
    c.declarePublisher(b(1), null, stream);
    IntConsumer publishing = i -> c.publish(b(1), Collections.singletonList(c.messageBuilder().addData("".getBytes()).build()), publishingSequenceFunction);
    IntStream.range(0, messageCount).forEach(publishing);
    publishingSequence.addAndGet(-duplicatedCount);
    IntStream.range(0, duplicatedCount).forEach(publishing);
    assertThat(publishLatch.await(10, TimeUnit.SECONDS)).isTrue();
    response = c.deletePublisher(b(1));
    assertThat(response.isOk()).isTrue();
    response = c.subscribe(b(1), stream, OffsetSpecification.first(), 10);
    assertThat(response.isOk()).isTrue();
    assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(1000L);
    assertThat(consumeCount.get()).isEqualTo(expectedConsumed);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) Response(com.rabbitmq.stream.impl.Client.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToLongFunction(java.util.function.ToLongFunction) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) NullAndEmptySource(org.junit.jupiter.params.provider.NullAndEmptySource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Constants(com.rabbitmq.stream.Constants) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Response(com.rabbitmq.stream.impl.Client.Response) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with ClientParameters

use of com.rabbitmq.stream.impl.Client.ClientParameters in project rabbitmq-stream-java-client by rabbitmq.

the class MetricsCollectionTest method filteredSmallerOffsetsInChunksShouldNotBeCounted.

@Test
void filteredSmallerOffsetsInChunksShouldNotBeCounted() throws Exception {
    int messageCount = 50000;
    AtomicLong messageIdSequence = new AtomicLong(0);
    TestUtils.publishAndWaitForConfirms(cf, builder -> builder.properties().messageId(messageIdSequence.incrementAndGet()).messageBuilder().build(), messageCount, stream);
    for (int i = 0; i < 10; i++) {
        Map<Byte, CountDownLatch> latches = new ConcurrentHashMap<>();
        latches.put(b(1), new CountDownLatch(1));
        latches.put(b(2), new CountDownLatch(1));
        Map<Byte, AtomicLong> counts = new ConcurrentHashMap<>();
        counts.put(b(1), new AtomicLong());
        counts.put(b(2), new AtomicLong());
        Map<Byte, AtomicLong> expectedCounts = new ConcurrentHashMap<>();
        expectedCounts.put(b(1), new AtomicLong(messageCount - 50));
        expectedCounts.put(b(2), new AtomicLong(messageCount - 100));
        CountMetricsCollector metricsCollector = new CountMetricsCollector();
        Client client = new Client(new ClientParameters().messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
            counts.get(subscriptionId).incrementAndGet();
            if (message.getProperties().getMessageIdAsLong() == messageCount) {
                latches.get(subscriptionId).countDown();
            }
        }).chunkListener((client1, subscriptionId, offset, msgCount, dataSize) -> client1.credit(subscriptionId, 1)).metricsCollector(metricsCollector).eventLoopGroup(eventLoopGroup));
        client.subscribe(b(1), stream, OffsetSpecification.offset(50), 10);
        client.subscribe(b(2), stream, OffsetSpecification.offset(100), 10);
        assertThat(latches.get(b(1)).await(10, SECONDS)).isTrue();
        assertThat(latches.get(b(2)).await(10, SECONDS)).isTrue();
        waitAtMost(() -> counts.get(b(1)).get() == expectedCounts.get(b(1)).get());
        waitAtMost(() -> counts.get(b(2)).get() == expectedCounts.get(b(2)).get());
        waitAtMost(() -> metricsCollector.consume.get() == expectedCounts.values().stream().mapToLong(v -> v.get()).reduce(0, (a, b) -> a + b));
        client.close();
    }
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) MetricsCollector(com.rabbitmq.stream.metrics.MetricsCollector) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Compression(com.rabbitmq.stream.compression.Compression) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 19 with ClientParameters

use of com.rabbitmq.stream.impl.Client.ClientParameters 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)19 Test (org.junit.jupiter.api.Test)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 TestUtils.b (com.rabbitmq.stream.impl.TestUtils.b)11 Collections (java.util.Collections)11 IntStream (java.util.stream.IntStream)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 TestInfo (org.junit.jupiter.api.TestInfo)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 Response (com.rabbitmq.stream.impl.Client.Response)8 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)8 StandardCharsets (java.nio.charset.StandardCharsets)7 Set (java.util.Set)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Stream (java.util.stream.Stream)7