You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lc...@apache.org on 2016/07/21 14:24:28 UTC

[1/2] incubator-beam git commit: BEAM-372 verfify if a nested coder consumes bytes equal to encoded bytes

Repository: incubator-beam
Updated Branches:
  refs/heads/master bdb652788 -> 6d5e8186a


BEAM-372 verfify if a nested coder consumes bytes equal to encoded bytes


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/84332ee9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/84332ee9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/84332ee9

Branch: refs/heads/master
Commit: 84332ee9716233af928e85c14c534714ab828531
Parents: bdb6527
Author: Chandni Singh <ch...@capitalone.com>
Authored: Tue Jul 19 17:30:16 2016 -0700
Committer: Luke Cwik <lc...@visitor-lcwik.wat.corp.google.com>
Committed: Thu Jul 21 09:34:06 2016 -0400

----------------------------------------------------------------------
 .../beam/sdk/testing/CoderProperties.java       | 17 +++++++++++--
 .../beam/sdk/testing/CoderPropertiesTest.java   | 26 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/84332ee9/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
index c217834..c1fc856 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
@@ -40,6 +40,7 @@ import org.apache.beam.sdk.util.UnownedOutputStream;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
+import com.google.common.io.CountingInputStream;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -341,8 +342,20 @@ public class CoderProperties {
     @SuppressWarnings("unchecked")
     Coder<T> deserializedCoder = Serializer.deserialize(coder.asCloudObject(), Coder.class);
 
-    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
-    return deserializedCoder.decode(new UnownedInputStream(is), context);
+    byte[] buffer;
+    if (context == Coder.Context.NESTED) {
+      buffer = new byte[bytes.length + 1];
+      System.arraycopy(bytes, 0, buffer, 0, bytes.length);
+      buffer[bytes.length] = 1;
+    } else {
+      buffer = bytes;
+    }
+
+    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
+    T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
+    assertThat("consumed bytes equal to encoded bytes", cis.getCount(),
+        equalTo((long) bytes.length));
+    return value;
   }
 
   private static <T> T decodeEncode(Coder<T> coder, Coder.Context context, T value)

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/84332ee9/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/CoderPropertiesTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/CoderPropertiesTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/CoderPropertiesTest.java
index 5b5274f..5bf55b0 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/CoderPropertiesTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/CoderPropertiesTest.java
@@ -28,6 +28,7 @@ import org.apache.beam.sdk.coders.StringUtf8Coder;
 import com.google.common.base.Strings;
 
 import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -213,4 +214,29 @@ public class CoderPropertiesTest {
     expectedException.expectMessage("Caller does not own the underlying");
     CoderProperties.encode(new ClosingCoder(), Context.NESTED, "test-value");
   }
+
+  /** Coder that consumes more bytes while decoding than required. */
+  public static class BadCoderThatConsumesMoreBytes extends NonDeterministicCoder {
+
+    @Override
+    public String decode(InputStream inStream, Context context) throws IOException {
+      String value = super.decode(inStream, context);
+      inStream.read();
+      return value;
+    }
+  }
+
+  @Test
+  public void testCoderWhichConsumesMoreBytesThanItProducesFail() throws IOException {
+    try {
+      BadCoderThatConsumesMoreBytes coder = new BadCoderThatConsumesMoreBytes();
+      byte[] bytes = CoderProperties.encode(coder, Context.NESTED, "TestData");
+      CoderProperties.decode(coder, Context.NESTED, bytes);
+      Assert.fail("Expected Assertion Error");
+    } catch (AssertionError error) {
+      assertThat(error.getMessage(),
+          CoreMatchers.containsString("consumed bytes equal to encoded bytes"));
+    }
+  }
+
 }


[2/2] incubator-beam git commit: [BEAM-372] added a test that verifies if a coder consumes bytes equal to encoded bytes

Posted by lc...@apache.org.
[BEAM-372] added a test that verifies if a coder consumes bytes equal to encoded bytes

This closes #695


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/6d5e8186
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/6d5e8186
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/6d5e8186

Branch: refs/heads/master
Commit: 6d5e8186a2da532eb1c29097bc1259a19d9f72c9
Parents: bdb6527 84332ee
Author: Luke Cwik <lc...@visitor-lcwik.wat.corp.google.com>
Authored: Thu Jul 21 09:34:52 2016 -0400
Committer: Luke Cwik <lc...@visitor-lcwik.wat.corp.google.com>
Committed: Thu Jul 21 09:34:52 2016 -0400

----------------------------------------------------------------------
 .../beam/sdk/testing/CoderProperties.java       | 17 +++++++++++--
 .../beam/sdk/testing/CoderPropertiesTest.java   | 26 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
----------------------------------------------------------------------