From 5867516b83e01b1f0eb8a7e4e96a7a031ed86ab4 Mon Sep 17 00:00:00 2001 From: songy23 Date: Thu, 2 Mar 2017 18:16:26 -0800 Subject: Modify serialize and deserialize to match the new tag encodings. --- .../stats/StatsContextFactoryTest.java | 85 +++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java b/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java index 12efa98f..004d8030 100644 --- a/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java +++ b/core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java @@ -13,8 +13,13 @@ package com.google.instrumentation.stats; +import static com.google.common.truth.Truth.assertThat; + +import com.google.io.base.VarInt; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -24,8 +29,84 @@ import org.junit.runners.JUnit4; */ @RunWith(JUnit4.class) public class StatsContextFactoryTest { + private static final int VALUE_TYPE_STRING = 0; + private static final int VALUE_TYPE_INTEGER = 1; + private static final int VALUE_TYPE_BOOLEAN = 2; + + private static final String KEY1 = "Key"; + private static final String VALUE_STRING = "String"; + private static final int VALUE_INT = 10; + private static final boolean VALUE_BOOL = true; + + @Test + public void testDeserializeEmptyReturnDefaultStatsContext() throws Exception { + StatsContext expected = Stats.getStatsContextFactory().getDefault(); + StatsContext actual = testDeserialize(new ByteArrayInputStream(new byte[0])); + assertThat(actual).isEqualTo(expected); + } + + @Test(expected = IOException.class) + public void testDeserializeValueTypeInteger() throws Exception { + // TODO(songya): test should pass after we add support for type integer + testDeserialize(constructInputStream(VALUE_TYPE_INTEGER)); + } + + @Test(expected = IOException.class) + public void testDeserializeValueTypeBoolean() throws Exception { + // TODO(songya): test should pass after we add support for type boolean + testDeserialize(constructInputStream(VALUE_TYPE_BOOLEAN)); + } + @Test(expected = IOException.class) - public void testDeserializeEmpty() throws Exception { - Stats.getStatsContextFactory().deserialize(new ByteArrayInputStream(new byte[0])); + public void testDeserializeWrongFormat() throws Exception { + // encoded tags should follow the format [tag_type key_len key_bytes value_len value_bytes]* + testDeserialize(new ByteArrayInputStream(new byte[1])); + } + + private StatsContext testDeserialize(InputStream inputStream) throws IOException { + return Stats.getStatsContextFactory().deserialize(inputStream); + } + + /* + * TODO(songya): after supporting serialize integer and boolean, + * remove this method and use StatsContext.serialize() instead. + * Currently StatsContext.serialize() can only serialize strings. + */ + private InputStream constructInputStream(int valueType) { + // TODO(songya): please note that ByteBuffer will overflow if > 1024 bytes were appended. + ByteBuffer buffer = ByteBuffer.allocate(1024); + VarInt.putVarInt(valueType, buffer); + encodeString(KEY1, buffer); + switch (valueType) { + case VALUE_TYPE_STRING: + encodeString(VALUE_STRING, buffer); + break; + case VALUE_TYPE_INTEGER: + encodeInteger(VALUE_INT, buffer); + break; + case VALUE_TYPE_BOOLEAN: + encodeBoolean(VALUE_BOOL, buffer); + break; + default: + return null; + } + return new ByteArrayInputStream(buffer.array()); + } + + private void encodeString(String input, ByteBuffer buffer) { + VarInt.putVarInt(input.length(), buffer); + for (int i = 0; i < input.length(); i++) { + buffer.put((byte) input.charAt(i)); + } + } + + private void encodeInteger(int input, ByteBuffer buffer) { + VarInt.putVarInt(Integer.valueOf(input).toString().length(), buffer); + buffer.put((byte) input); + } + + private void encodeBoolean(boolean input, ByteBuffer buffer) { + VarInt.putVarInt(Boolean.valueOf(input).toString().length(), buffer); + buffer.put((byte) (input? 1 : 0)); } } -- cgit v1.2.3