aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorsongy23 <songy23@users.noreply.github.com>2017-03-03 09:33:24 -0800
committerGitHub <noreply@github.com>2017-03-03 09:33:24 -0800
commitd9aa0d3dc003c0775f32c7b37e909fe40235cdd5 (patch)
tree570ad12417acf6517c8fb993cb11b6a5d79ef439 /core
parent60c35d42b482f6713ba8884f502bfc5d92ff5975 (diff)
parent5867516b83e01b1f0eb8a7e4e96a7a031ed86ab4 (diff)
downloadplatform_external_opencensus-java-d9aa0d3dc003c0775f32c7b37e909fe40235cdd5.tar.gz
platform_external_opencensus-java-d9aa0d3dc003c0775f32c7b37e909fe40235cdd5.tar.bz2
platform_external_opencensus-java-d9aa0d3dc003c0775f32c7b37e909fe40235cdd5.zip
Merge pull request #113 from songy23/Yang
Modify serialize and deserialize to match the new tag encodings.
Diffstat (limited to 'core')
-rw-r--r--core/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java85
1 files changed, 83 insertions, 2 deletions
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));
}
}