Search in sources :

Example 1 with DefaultByteBufferFactory

use of com.rabbitmq.client.impl.nio.DefaultByteBufferFactory in project rabbitmq-java-client by rabbitmq.

the class JavaNioTest method byteBufferFactory.

@Test
public void byteBufferFactory() throws Exception {
    ConnectionFactory cf = new ConnectionFactory();
    cf.useNio();
    int baseCapacity = 32768;
    NioParams nioParams = new NioParams();
    nioParams.setReadByteBufferSize(baseCapacity / 2);
    nioParams.setWriteByteBufferSize(baseCapacity / 4);
    List<ByteBuffer> byteBuffers = new CopyOnWriteArrayList<>();
    cf.setNioParams(nioParams.setByteBufferFactory(new DefaultByteBufferFactory(capacity -> {
        ByteBuffer bb = ByteBuffer.allocate(capacity);
        byteBuffers.add(bb);
        return bb;
    })));
    try (Connection c = cf.newConnection()) {
        sendAndVerifyMessage(c, 100);
    }
    assertThat(byteBuffers).hasSize(2);
    Condition<Integer> condition = new Condition<>(c -> c == nioParams.getReadByteBufferSize() || c == nioParams.getWriteByteBufferSize(), "capacity set by factory");
    assertThat(byteBuffers.get(0).capacity()).is(condition);
    assertThat(byteBuffers.get(1).capacity()).is(condition);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Condition(org.assertj.core.api.Condition) NioParams(com.rabbitmq.client.impl.nio.NioParams) DefaultByteBufferFactory(com.rabbitmq.client.impl.nio.DefaultByteBufferFactory) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

DefaultByteBufferFactory (com.rabbitmq.client.impl.nio.DefaultByteBufferFactory)1 NioParams (com.rabbitmq.client.impl.nio.NioParams)1 ByteBuffer (java.nio.ByteBuffer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Condition (org.assertj.core.api.Condition)1 Test (org.junit.Test)1