Repository: cassandra
Updated Branches:
  refs/heads/trunk 6651b0271 -> c406c456c


Follow up fixes for 13789

patch by jasobrown, reviewed by Michael Kjellman for CASSANDRA-13789


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/c406c456
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/c406c456
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/c406c456

Branch: refs/heads/trunk
Commit: c406c456c007e59d50919eb23b5d887e7e647f10
Parents: 6651b02
Author: Jason Brown <jasedbr...@gmail.com>
Authored: Thu Sep 7 12:50:48 2017 -0700
Committer: Jason Brown <jasedbr...@gmail.com>
Committed: Fri Sep 8 03:19:57 2017 -0700

----------------------------------------------------------------------
 .../org/apache/cassandra/transport/CBUtil.java  |  4 +-
 .../apache/cassandra/transport/CBUtilTest.java  | 68 ++++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/c406c456/src/java/org/apache/cassandra/transport/CBUtil.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/transport/CBUtil.java 
b/src/java/org/apache/cassandra/transport/CBUtil.java
index d5d733c..52217a9 100644
--- a/src/java/org/apache/cassandra/transport/CBUtil.java
+++ b/src/java/org/apache/cassandra/transport/CBUtil.java
@@ -140,7 +140,7 @@ public abstract class CBUtil
     public static void writeString(String str, ByteBuf cb)
     {
         int writerIndex = cb.writerIndex();
-        cb.writerIndex(writerIndex + 2);
+        cb.writeShort(0);
         int written = ByteBufUtil.writeUtf8(cb, str);
         cb.setShort(writerIndex, written);
     }
@@ -166,7 +166,7 @@ public abstract class CBUtil
     public static void writeLongString(String str, ByteBuf cb)
     {
         int writerIndex = cb.writerIndex();
-        cb.writerIndex(writerIndex + 4);
+        cb.writeInt(0);
         int written = ByteBufUtil.writeUtf8(cb, str);
         cb.setInt(writerIndex, written);
     }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c406c456/test/unit/org/apache/cassandra/transport/CBUtilTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/transport/CBUtilTest.java 
b/test/unit/org/apache/cassandra/transport/CBUtilTest.java
new file mode 100644
index 0000000..e65fdb2
--- /dev/null
+++ b/test/unit/org/apache/cassandra/transport/CBUtilTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.transport;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.PooledByteBufAllocator;
+
+public class CBUtilTest
+{
+    private static final ByteBufAllocator allocator = 
PooledByteBufAllocator.DEFAULT;
+    private ByteBuf buf;
+
+    @After
+    public void tearDown()
+    {
+        if (buf != null && buf.refCnt() > 0)
+            buf.release(buf.refCnt());
+    }
+
+    @Test
+    public void writeAndReadString()
+    {
+        final String text = "if you're happy and you know it, write your 
tests";
+        int size = CBUtil.sizeOfString(text);
+
+        buf = allocator.heapBuffer(size);
+        CBUtil.writeString(text, buf);
+        Assert.assertEquals(size, buf.writerIndex());
+        Assert.assertEquals(0, buf.readerIndex());
+        Assert.assertEquals(text, CBUtil.readString(buf));
+        Assert.assertEquals(buf.writerIndex(), buf.readerIndex());
+    }
+
+    @Test
+    public void writeAndReadLongString()
+    {
+        final String text = "if you're happy and you know it, write your 
tests";
+        int size = CBUtil.sizeOfLongString(text);
+
+        buf = allocator.heapBuffer(size);
+        CBUtil.writeLongString(text, buf);
+        Assert.assertEquals(size, buf.writerIndex());
+        Assert.assertEquals(0, buf.readerIndex());
+        Assert.assertEquals(text, CBUtil.readLongString(buf));
+        Assert.assertEquals(buf.writerIndex(), buf.readerIndex());
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to