Repository: activemq-artemis Updated Branches: refs/heads/master 00bd989f9 -> 9fb8c3c47
ARTEMIS-1586 Added String Pools unit tests - SimpleString::toSimpleString String pooling test - ByteBufSimpleStringPool test - StringSimpleStringPool test - ByteBufStringValuePool test Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/a3c41818 Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/a3c41818 Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/a3c41818 Branch: refs/heads/master Commit: a3c418183aa7f8e7fe92d4db551b9b15777aabb7 Parents: 98028cd Author: Francesco Nigro <[email protected]> Authored: Mon Jan 15 13:56:46 2018 +0100 Committer: Michael Pearce <[email protected]> Committed: Wed Jan 17 09:33:41 2018 +0100 ---------------------------------------------------------------------- .../artemis/utils/TypedPropertiesTest.java | 29 ++++++++++++ .../artemis/tests/util/SimpleStringTest.java | 49 ++++++++++++++++++++ 2 files changed, 78 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/a3c41818/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesTest.java ---------------------------------------------------------------------- diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesTest.java index 38144c9..ea044ac 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesTest.java @@ -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())); + } } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/a3c41818/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/SimpleStringTest.java ---------------------------------------------------------------------- diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/SimpleStringTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/SimpleStringTest.java index b367015..054b186 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/SimpleStringTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/SimpleStringTest.java @@ -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)); + } + } + + }
