Search in sources :

Example 1 with LargeMessageControllerImpl

use of org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl in project activemq-artemis by apache.

the class LargeMessageBufferTest method testReadIntegers.

// testing void getBytes(int index, ChannelBuffer dst, int dstIndex, int length)
@Test
public void testReadIntegers() throws Exception {
    LargeMessageControllerImpl buffer = createBufferWithIntegers(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
    for (int i = 1; i <= 15; i++) {
        Assert.assertEquals(i, buffer.readInt());
    }
    try {
        buffer.readByte();
        Assert.fail("supposed to throw an exception");
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) Test(org.junit.Test)

Example 2 with LargeMessageControllerImpl

use of org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl in project activemq-artemis by apache.

the class LargeMessageBufferTest method testReadLongs.

// testing void getBytes(int index, ChannelBuffer dst, int dstIndex, int length)
@Test
public void testReadLongs() throws Exception {
    LargeMessageControllerImpl buffer = createBufferWithLongs(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
    for (int i = 1; i <= 15; i++) {
        Assert.assertEquals(i, buffer.readLong());
    }
    try {
        buffer.readByte();
        Assert.fail("supposed to throw an exception");
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) Test(org.junit.Test)

Example 3 with LargeMessageControllerImpl

use of org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl in project activemq-artemis by apache.

the class LargeMessageBufferTest method testGetBytesIByteArray.

// Test for void getBytes(final int index, final byte[] dst)
@Test
public void testGetBytesIByteArray() throws Exception {
    LargeMessageControllerImpl buffer = create15BytesSample();
    byte[] bytes = new byte[15];
    buffer.getBytes(0, bytes);
    validateAgainstSample(bytes);
    try {
        buffer = create15BytesSample();
        bytes = new byte[16];
        buffer.getBytes(0, bytes);
        Assert.fail("supposed to throw an exception");
    } catch (java.lang.IndexOutOfBoundsException e) {
    }
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) Test(org.junit.Test)

Example 4 with LargeMessageControllerImpl

use of org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl in project activemq-artemis by apache.

the class LargeMessageBufferTest method testReadData.

@Test
public void testReadData() throws Exception {
    ActiveMQBuffer dynamic = ActiveMQBuffers.dynamicBuffer(1);
    String str1 = RandomUtil.randomString();
    String str2 = RandomUtil.randomString();
    double d1 = RandomUtil.randomDouble();
    float f1 = RandomUtil.randomFloat();
    dynamic.writeUTF(str1);
    dynamic.writeString(str2);
    dynamic.writeDouble(d1);
    dynamic.writeFloat(f1);
    LargeMessageControllerImpl readBuffer = splitBuffer(3, dynamic.toByteBuffer().array());
    Assert.assertEquals(str1, readBuffer.readUTF());
    Assert.assertEquals(str2, readBuffer.readString());
    Assert.assertEquals(d1, readBuffer.readDouble(), 0.000001);
    Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001);
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 5 with LargeMessageControllerImpl

use of org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl in project activemq-artemis by apache.

the class LargeMessageBufferTest method testStreamData.

@Test
public void testStreamData() throws Exception {
    final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 1024 * 11 + 123, 1000);
    final PipedOutputStream output = new PipedOutputStream();
    final PipedInputStream input = new PipedInputStream(output);
    final AtomicInteger errors = new AtomicInteger(0);
    // Done reading 3 elements
    final CountDownLatch done1 = new CountDownLatch(1);
    // Done with the thread
    final CountDownLatch done2 = new CountDownLatch(1);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger totalBytes = new AtomicInteger(0);
    Thread treader = new Thread("treader") {

        @Override
        public void run() {
            try {
                byte[] line = new byte[1024];
                int dataRead = 0;
                while (dataRead >= 0) {
                    dataRead = input.read(line);
                    if (dataRead > 0) {
                        totalBytes.addAndGet(dataRead);
                        if (count.incrementAndGet() == 3) {
                            done1.countDown();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                done1.countDown();
                done2.countDown();
            }
        }
    };
    treader.setDaemon(true);
    treader.start();
    for (int i = 0; i < 3; i++) {
        outBuffer.addPacket(new byte[1024], 1, true);
    }
    outBuffer.setOutputStream(output);
    final CountDownLatch waiting = new CountDownLatch(1);
    Thread twaiter = new Thread("twaiter") {

        @Override
        public void run() {
            try {
                outBuffer.waitCompletion(0);
                waiting.countDown();
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    };
    twaiter.setDaemon(true);
    twaiter.start();
    Assert.assertTrue(done1.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(3, count.get());
    Assert.assertEquals(1024 * 3, totalBytes.get());
    for (int i = 0; i < 8; i++) {
        outBuffer.addPacket(new byte[1024], 1, true);
    }
    Assert.assertEquals(1, waiting.getCount());
    outBuffer.addPacket(new byte[123], 1, false);
    Assert.assertTrue(done2.await(10, TimeUnit.SECONDS));
    Assert.assertTrue(waiting.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(12, count.get());
    Assert.assertEquals(1024 * 11 + 123, totalBytes.get());
    treader.join();
    twaiter.join();
    Assert.assertEquals(0, errors.get());
    input.close();
}
Also used : LargeMessageControllerImpl(org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

LargeMessageControllerImpl (org.apache.activemq.artemis.core.client.impl.LargeMessageControllerImpl)18 Test (org.junit.Test)17 IOException (java.io.IOException)5 PipedOutputStream (java.io.PipedOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataOutputStream (java.io.DataOutputStream)4 OutputStream (java.io.OutputStream)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)3 DataInputStream (java.io.DataInputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ActiveMQBufferInputStream (org.apache.activemq.artemis.utils.ActiveMQBufferInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PipedInputStream (java.io.PipedInputStream)1