diff options
author | Yang Song <songy23@users.noreply.github.com> | 2017-11-17 12:58:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-17 12:58:38 -0800 |
commit | 467028253aa02ce7e0763a9d472dc0dde8f3038d (patch) | |
tree | 80fde929d81e26c263bfca2967a5aa8f3f5eeb12 /impl_core/src/test/java/io/opencensus/implcore | |
parent | f01394e3f067a8045e8a5651bbc5b95622d8a37e (diff) | |
download | opencensus-java-467028253aa02ce7e0763a9d472dc0dde8f3038d.tar.gz |
Only apply size limit to the bytes representing tag keys and values. (#807)
* Only apply size limit to the bytes representing tag keys and values.
* Reword error message when size exceeds limit
* Add a note about chars == bytes
Diffstat (limited to 'impl_core/src/test/java/io/opencensus/implcore')
3 files changed, 55 insertions, 31 deletions
diff --git a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextDeserializationTest.java b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextDeserializationTest.java index f6d8dd09..273a6ce2 100644 --- a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextDeserializationTest.java +++ b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextDeserializationTest.java @@ -78,9 +78,30 @@ public class TagContextDeserializationTest { @Test public void testDeserializeTooLargeByteArrayThrowException() throws TagContextDeserializationException { + ByteArrayDataOutput output = ByteStreams.newDataOutput(); + output.write(SerializationUtils.VERSION_ID); + for (int i = 0; i < SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) { + // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8. + String str; + if (i < 10) { + str = "000" + i; + } else if (i < 100) { + str = "00" + i; + } else if (i < 1000) { + str = "0" + i; + } else { + str = String.valueOf(i); + } + encodeTagToOutput(str, str, output); + } + // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte + // more than limit. + encodeTagToOutput("last", "last1", output); + + byte[] bytes = output.toByteArray(); thrown.expect(TagContextDeserializationException.class); - thrown.expectMessage("Size of input byte[] exceeds the maximum serialized size "); - serializer.fromByteArray(new byte[SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT + 1]); + thrown.expectMessage("Size of TagContext exceeds the maximum serialized size "); + serializer.fromByteArray(bytes); } @Test diff --git a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextRoundtripTest.java b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextRoundtripTest.java index c49e53a4..1b1aa042 100644 --- a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextRoundtripTest.java +++ b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextRoundtripTest.java @@ -59,23 +59,22 @@ public class TagContextRoundtripTest { @Test public void testRoundtrip_TagContextWithMaximumSize() throws Exception { TagContextBuilder builder = tagger.emptyBuilder(); - int i = 0; - - // This loop should fill in tags that have a total size of 8185 - while (serializer.toByteArray(builder.build()).length - < SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT - 8) { - TagKey key = TagKey.create("k" + i); - TagValue value = TagValue.create("v" + i); - builder.put(key, value); - i++; + for (int i = 0; i < SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8; i++) { + // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8. + // Add 1024 tags, the total size should just be 8192. + String str; + if (i < 10) { + str = "000" + i; + } else if (i < 100) { + str = "00" + i; + } else if (i < 1000) { + str = "0" + i; + } else { + str = "" + i; + } + builder.put(TagKey.create(str), TagValue.create(str)); } - // The last tag has size 7, after putting it, the size of TagContext should just meet the limit - builder.put(TagKey.create("last"), TagValue.create("")); - - TagContext expected = builder.build(); - assertThat(serializer.toByteArray(expected).length) - .isEqualTo(SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT); - testRoundtripSerialization(expected); + testRoundtripSerialization(builder.build()); } private void testRoundtripSerialization(TagContext expected) throws Exception { diff --git a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextSerializationTest.java b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextSerializationTest.java index 551b7ff8..771b2899 100644 --- a/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextSerializationTest.java +++ b/impl_core/src/test/java/io/opencensus/implcore/tags/propagation/TagContextSerializationTest.java @@ -91,23 +91,27 @@ public class TagContextSerializationTest { @Test public void testSerializeTooLargeTagContext() throws TagContextSerializationException { TagContextBuilder builder = tagger.emptyBuilder(); - int i = 0; - - // This loop should fill in tags that have a total size of 8185 - while (serializer.toByteArray(builder.build()).length - < SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT - 8) { - TagKey key = TagKey.create("k" + i); - TagValue value = TagValue.create("v" + i); - builder.put(key, value); - i++; + for (int i = 0; i < SerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) { + // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8. + String str; + if (i < 10) { + str = "000" + i; + } else if (i < 100) { + str = "00" + i; + } else if (i < 1000) { + str = "0" + i; + } else { + str = String.valueOf(i); + } + builder.put(TagKey.create(str), TagValue.create(str)); } - // The last tag has size 8, after putting it, the size of TagContext (8193) should just exceed - // the limit - builder.put(TagKey.create("last"), TagValue.create("1")); + // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte + // more than limit. + builder.put(TagKey.create("last"), TagValue.create("last1")); TagContext tagContext = builder.build(); thrown.expect(TagContextSerializationException.class); - thrown.expectMessage("Size of serialized TagContext exceeds the maximum serialized size "); + thrown.expectMessage("Size of TagContext exceeds the maximum serialized size "); serializer.toByteArray(tagContext); } |