Search in sources :

Example 1 with SimpleCodec

use of com.rabbitmq.stream.codec.SimpleCodec in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducerUnitTest method init.

@BeforeEach
@SuppressWarnings("unchecked")
void init() {
    mocks = MockitoAnnotations.openMocks(this);
    executorService = Executors.newScheduledThreadPool(2);
    when(channel.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
    when(channel.writeAndFlush(Mockito.any())).thenReturn(channelFuture);
    when(client.allocateNoCheck(any(ByteBufAllocator.class), anyInt())).thenAnswer((Answer<ByteBuf>) invocation -> {
        ByteBufAllocator allocator = invocation.getArgument(0);
        int capacity = invocation.getArgument(1);
        ByteBuf buffer = allocator.buffer(capacity);
        buffers.add(buffer);
        return buffer;
    });
    when(client.maxFrameSize()).thenReturn(Integer.MAX_VALUE);
    when(client.publishInternal(anyByte(), anyList(), any(OutboundEntityWriteCallback.class), any(ToLongFunction.class))).thenAnswer(invocation -> client.publishInternal(channel, invocation.getArgument(0), invocation.getArgument(1), invocation.getArgument(2), invocation.getArgument(3)));
    when(client.publishInternal(any(Channel.class), anyByte(), anyList(), any(OutboundEntityWriteCallback.class), any(ToLongFunction.class))).thenCallRealMethod();
    when(env.scheduledExecutorService()).thenReturn(executorService);
    when(env.locator()).thenReturn(client);
    when(env.locatorOperation(any())).thenCallRealMethod();
    when(env.clock()).thenReturn(clock);
    when(env.codec()).thenReturn(new SimpleCodec());
    doAnswer((Answer<Runnable>) invocationOnMock -> {
        StreamProducer p = invocationOnMock.getArgument(0);
        p.setClient(client);
        p.setPublisherId((byte) 0);
        return () -> {
        };
    }).when(env).registerProducer(any(StreamProducer.class), nullable(String.class), anyString());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) OutboundEntityWriteCallback(com.rabbitmq.stream.impl.Client.OutboundEntityWriteCallback) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) MockitoAnnotations(org.mockito.MockitoAnnotations) Answer(org.mockito.stubbing.Answer) ThrowingCallable(org.assertj.core.api.ThrowableAssert.ThrowingCallable) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ToLongFunction(java.util.function.ToLongFunction) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Compression(com.rabbitmq.stream.compression.Compression) Executors(java.util.concurrent.Executors) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ArgumentMatchers.anyByte(org.mockito.ArgumentMatchers.anyByte) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Constants(com.rabbitmq.stream.Constants) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ToLongFunction(java.util.function.ToLongFunction) Channel(io.netty.channel.Channel) OutboundEntityWriteCallback(com.rabbitmq.stream.impl.Client.OutboundEntityWriteCallback) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with SimpleCodec

use of com.rabbitmq.stream.codec.SimpleCodec in project rabbitmq-stream-java-client by rabbitmq.

the class ClientTest method publishConsumeWithSimpleCodec.

@Test
void publishConsumeWithSimpleCodec() throws Exception {
    int messageCount = 1000;
    Codec codec = new SimpleCodec();
    Client publisher = cf.get(new Client.ClientParameters().codec(codec));
    publisher.declarePublisher(b(1), null, stream);
    IntStream.range(0, 1000).forEach(i -> publisher.publish(b(1), Collections.singletonList(publisher.messageBuilder().addData(String.valueOf(i).getBytes(UTF8)).build())));
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    Set<String> messageBodies = ConcurrentHashMap.newKeySet(messageCount);
    Client consumer = cf.get(new Client.ClientParameters().codec(codec).chunkListener((client, subscriptionId, offset, messageCount1, dataSize) -> client.credit(subscriptionId, 1)).messageListener((subscriptionId, offset, chunkTimestamp, message) -> {
        messageBodies.add(new String(message.getBodyAsBinary()));
        consumeLatch.countDown();
    }));
    consumer.subscribe(b(1), stream, OffsetSpecification.first(), 10);
    assertThat(consumeLatch.await(10, SECONDS)).isTrue();
    IntStream.range(0, messageCount).forEach(i -> assertThat(messageBodies).contains(String.valueOf(i)));
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) IntStream(java.util.stream.IntStream) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) 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) ByteBuffer(java.nio.ByteBuffer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DataOutputStream(java.io.DataOutputStream) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) SwiftMqCodec(com.rabbitmq.stream.codec.SwiftMqCodec) ValueSource(org.junit.jupiter.params.provider.ValueSource) Properties(com.rabbitmq.stream.Properties) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) com.rabbitmq.stream(com.rabbitmq.stream) Semaphore(java.util.concurrent.Semaphore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) LongConsumer(java.util.function.LongConsumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StreamParametersBuilder(com.rabbitmq.stream.impl.Client.StreamParametersBuilder) Channel(com.rabbitmq.client.Channel) SECONDS(java.util.concurrent.TimeUnit.SECONDS) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) SwiftMqCodec(com.rabbitmq.stream.codec.SwiftMqCodec) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SimpleCodec (com.rabbitmq.stream.codec.SimpleCodec)2 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 Duration (java.time.Duration)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IntStream (java.util.stream.IntStream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 com.rabbitmq.stream (com.rabbitmq.stream)1 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)1 Constants (com.rabbitmq.stream.Constants)1 Properties (com.rabbitmq.stream.Properties)1 StreamException (com.rabbitmq.stream.StreamException)1