You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by ji...@apache.org on 2020/03/05 07:27:34 UTC

[helix] branch master updated: Fix type cast in TestRawZkClient (#853)

This is an automated email from the ASF dual-hosted git repository.

jiajunwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git


The following commit(s) were added to refs/heads/master by this push:
     new 1747ac0  Fix type cast in TestRawZkClient (#853)
1747ac0 is described below

commit 1747ac022f2d544a0bf2649dd438f3a1544fab8b
Author: Jiajun Wang <18...@users.noreply.github.com>
AuthorDate: Wed Mar 4 23:27:25 2020 -0800

    Fix type cast in TestRawZkClient (#853)
    
    The type cast from char to int is unexpected in testAsyncWriteOperations. We would like to fix it and make the type cast expected and the test accurate.
---
 .../apache/helix/manager/zk/TestRawZkClient.java   | 24 ++++++++++++++--------
 .../apache/helix/zookeeper/zkclient/ZkClient.java  | 14 ++++++-------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/helix-core/src/test/java/org/apache/helix/manager/zk/TestRawZkClient.java b/helix-core/src/test/java/org/apache/helix/manager/zk/TestRawZkClient.java
index bf1a82c..5771003 100644
--- a/helix-core/src/test/java/org/apache/helix/manager/zk/TestRawZkClient.java
+++ b/helix-core/src/test/java/org/apache/helix/manager/zk/TestRawZkClient.java
@@ -760,14 +760,14 @@ public class TestRawZkClient extends ZkUnitTestBase {
       zkClient.setZkSerializer(new ZNRecordSerializer());
 
       ZNRecord oversizeZNRecord = new ZNRecord("Oversize");
-      StringBuilder sb = new StringBuilder(1204);
+      char[] buff = new char[1024];
       Random ran = new Random();
       for (int i = 0; i < 1024; i++) {
-        sb.append(ran.nextInt(26) + 'a');
+        buff[i] = (char) (ran.nextInt(26) + 'a');
       }
-      String buf = sb.toString();
+      String buffString = new String(buff);
       for (int i = 0; i < 1024; i++) {
-        oversizeZNRecord.setSimpleField(Integer.toString(i), buf);
+        oversizeZNRecord.setSimpleField(Integer.toString(i), buffString);
       }
 
       // ensure /tmp exists for the test
@@ -776,31 +776,37 @@ public class TestRawZkClient extends ZkUnitTestBase {
       }
 
       org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.CreateCallbackHandler
-          createCallback = new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.CreateCallbackHandler();
+          createCallback =
+          new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.CreateCallbackHandler();
+
       zkClient.asyncCreate("/tmp/async", null, CreateMode.PERSISTENT, createCallback);
       createCallback.waitForSuccess();
       Assert.assertEquals(createCallback.getRc(), 0);
       Assert.assertTrue(zkClient.exists("/tmp/async"));
 
+      Assert.assertFalse(zkClient.exists("/tmp/asyncOversize"));
       // try to create oversize node, should fail
       zkClient.asyncCreate("/tmp/asyncOversize", oversizeZNRecord, CreateMode.PERSISTENT,
           createCallback);
       createCallback.waitForSuccess();
-      Assert.assertEquals(createCallback.getRc(), KeeperException.Code.MarshallingError);
+      Assert.assertEquals(createCallback.getRc(), KeeperException.Code.MARSHALLINGERROR.intValue());
       Assert.assertFalse(zkClient.exists("/tmp/asyncOversize"));
 
       ZNRecord normalZNRecord = new ZNRecord("normal");
-      normalZNRecord.setSimpleField("key", buf);
+      normalZNRecord.setSimpleField("key", buffString);
 
       org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.SetDataCallbackHandler
-          setDataCallbackHandler = new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.SetDataCallbackHandler();
+          setDataCallbackHandler =
+          new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.SetDataCallbackHandler();
+
       zkClient.asyncSetData("/tmp/async", normalZNRecord, -1, setDataCallbackHandler);
       setDataCallbackHandler.waitForSuccess();
       Assert.assertEquals(setDataCallbackHandler.getRc(), 0);
 
       zkClient.asyncSetData("/tmp/async", oversizeZNRecord, -1, setDataCallbackHandler);
       setDataCallbackHandler.waitForSuccess();
-      Assert.assertEquals(setDataCallbackHandler.getRc(), KeeperException.Code.MarshallingError);
+      Assert.assertEquals(setDataCallbackHandler.getRc(),
+          KeeperException.Code.MARSHALLINGERROR.intValue());
       Assert.assertEquals(zkClient.readData("/tmp/async"), normalZNRecord);
     } finally {
       if (originSizeLimit == null) {
diff --git a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
index 3424f06..fa22927 100644
--- a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
+++ b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
@@ -1721,7 +1721,7 @@ public class ZkClient implements Watcher {
   public void asyncCreate(final String path, Object datat, final CreateMode mode,
       final ZkAsyncCallbacks.CreateCallbackHandler cb) {
     final long startT = System.currentTimeMillis();
-    byte[] data = null;
+    final byte[] data;
     try {
       data = (datat == null ? null : serialize(datat, path));
     } catch (ZkMarshallingError e) {
@@ -1729,13 +1729,12 @@ public class ZkClient implements Watcher {
           new ZkAsyncCallbacks.ZkAsyncCallContext(_monitor, startT, 0, false), null);
       return;
     }
-    final byte[] finalData = data;
     retryUntilConnected(() -> {
       ((ZkConnection) getConnection()).getZookeeper()
-          .create(path, finalData, ZooDefs.Ids.OPEN_ACL_UNSAFE,
+          .create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
               // Arrays.asList(DEFAULT_ACL),
               mode, cb, new ZkAsyncCallbacks.ZkAsyncCallContext(_monitor, startT,
-                  finalData == null ? 0 : finalData.length, false));
+                  data == null ? 0 : data.length, false));
       return null;
     });
   }
@@ -1744,7 +1743,7 @@ public class ZkClient implements Watcher {
   public void asyncSetData(final String path, Object datat, final int version,
       final ZkAsyncCallbacks.SetDataCallbackHandler cb) {
     final long startT = System.currentTimeMillis();
-    byte[] data = null;
+    final byte[] data;
     try {
       data = serialize(datat, path);
     } catch (ZkMarshallingError e) {
@@ -1752,11 +1751,10 @@ public class ZkClient implements Watcher {
           new ZkAsyncCallbacks.ZkAsyncCallContext(_monitor, startT, 0, false), null);
       return;
     }
-    final byte[] finalData = data;
     retryUntilConnected(() -> {
-      ((ZkConnection) getConnection()).getZookeeper().setData(path, finalData, version, cb,
+      ((ZkConnection) getConnection()).getZookeeper().setData(path, data, version, cb,
           new ZkAsyncCallbacks.ZkAsyncCallContext(_monitor, startT,
-              finalData == null ? 0 : finalData.length, false));
+              data == null ? 0 : data.length, false));
       return null;
     });
   }