ARTEMIS-1586 Added String Pools unit tests

- SimpleString::toSimpleString String pooling test
- ByteBufSimpleStringPool test
- StringSimpleStringPool test
- ByteBufStringValuePool test
This commit is contained in:
Francesco Nigro 2018-01-15 13:56:46 +01:00 committed by Michael Pearce
parent 98028cdecc
commit a3c418183a
2 changed files with 78 additions and 0 deletions

View File

@ -18,6 +18,8 @@ package org.apache.activemq.artemis.utils;
import java.util.Iterator;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.ActiveMQBuffers;
import org.apache.activemq.artemis.api.core.SimpleString;
@ -226,4 +228,31 @@ public class TypedPropertiesTest {
props = new TypedProperties();
key = RandomUtil.randomSimpleString();
}
@Test
public void testByteBufStringValuePool() {
final int capacity = 8;
final int chars = Integer.toString(capacity).length();
final TypedProperties.StringValue.ByteBufStringValuePool pool = new TypedProperties.StringValue.ByteBufStringValuePool(capacity, chars);
final int bytes = new SimpleString(Integer.toString(capacity)).sizeof();
final ByteBuf bb = Unpooled.buffer(bytes, bytes);
for (int i = 0; i < capacity; i++) {
final SimpleString s = new SimpleString(Integer.toString(i));
bb.resetWriterIndex();
SimpleString.writeSimpleString(bb, s);
bb.resetReaderIndex();
final TypedProperties.StringValue expectedPooled = pool.getOrCreate(bb);
bb.resetReaderIndex();
Assert.assertSame(expectedPooled, pool.getOrCreate(bb));
}
}
@Test
public void testByteBufStringValuePoolTooLong() {
final SimpleString tooLong = new SimpleString("aa");
final ByteBuf bb = Unpooled.buffer(tooLong.sizeof(), tooLong.sizeof());
SimpleString.writeSimpleString(bb, tooLong);
final TypedProperties.StringValue.ByteBufStringValuePool pool = new TypedProperties.StringValue.ByteBufStringValuePool(1, tooLong.length() - 1);
Assert.assertNotSame(pool.getOrCreate(bb), pool.getOrCreate(bb.resetReaderIndex()));
}
}

View File

@ -18,6 +18,8 @@ package org.apache.activemq.artemis.tests.util;
import java.util.concurrent.CountDownLatch;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.utils.DataConstants;
import org.apache.activemq.artemis.utils.RandomUtil;
@ -394,4 +396,51 @@ public class SimpleStringTest extends Assert {
}
}
@Test
public void testToSimpleStringPoolStringArgument() throws Exception {
final String s = "pooled";
final SimpleString ss = SimpleString.toSimpleString(s);
final String s1 = ss.toString();
Assert.assertSame("SimpleString::toSimpleString is not pooling the given String", s, s1);
}
@Test
public void testByteBufSimpleStringPool() {
final int capacity = 8;
final int chars = Integer.toString(capacity).length();
final SimpleString.ByteBufSimpleStringPool pool = new SimpleString.ByteBufSimpleStringPool(capacity, chars);
final int bytes = new SimpleString(Integer.toString(capacity)).sizeof();
final ByteBuf bb = Unpooled.buffer(bytes, bytes);
for (int i = 0; i < capacity; i++) {
final SimpleString s = new SimpleString(Integer.toString(i));
bb.resetWriterIndex();
SimpleString.writeSimpleString(bb, s);
bb.resetReaderIndex();
final SimpleString expectedPooled = pool.getOrCreate(bb);
bb.resetReaderIndex();
Assert.assertSame(expectedPooled, pool.getOrCreate(bb));
}
}
@Test
public void testByteBufSimpleStringPoolTooLong() {
final SimpleString tooLong = new SimpleString("aa");
final ByteBuf bb = Unpooled.buffer(tooLong.sizeof(), tooLong.sizeof());
SimpleString.writeSimpleString(bb, tooLong);
final SimpleString.ByteBufSimpleStringPool pool = new SimpleString.ByteBufSimpleStringPool(1, tooLong.length() - 1);
Assert.assertNotSame(pool.getOrCreate(bb), pool.getOrCreate(bb.resetReaderIndex()));
}
@Test
public void testStringSimpleStringPool() throws Exception {
final int capacity = 8;
final SimpleString.StringSimpleStringPool pool = new SimpleString.StringSimpleStringPool(capacity);
for (int i = 0; i < capacity; i++) {
final String s = Integer.toString(i);
final SimpleString expectedPooled = pool.getOrCreate(s);
Assert.assertSame(expectedPooled, pool.getOrCreate(s));
}
}
}