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

yukon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-remoting.git

commit 4f1e2d2a5a7496527666093122ddff05e81a55f2
Author: yukon <[email protected]>
AuthorDate: Mon May 27 16:09:19 2019 +0800

    Add unit tests for RemotingCommand implementation
---
 pom.xml                                            |   6 +
 .../remoting/api/buffer/ByteBufferWrapper.java     |   4 +
 .../remoting/api/command/RemotingCommand.java      |   2 -
 .../rocketmq/remoting/api/command/TrafficType.java |   2 +-
 .../rocketmq/remoting/common/ResponseFuture.java   |   3 +-
 .../impl/buffer/NettyByteBufferWrapper.java        |  14 ++-
 .../remoting/impl/command/CodecHelper.java         |   6 +-
 .../remoting/impl/command/RemotingCommandImpl.java |  10 +-
 .../remoting/impl/command/RequestIdGenerator.java  |   1 -
 .../org/apache/rocketmq/remoting/BaseTest.java     |  65 ++++++++++
 .../remoting/common/ResponseFutureTest.java        | 105 ++++++++++++++++
 .../common/SemaphoreReleaseOnlyOnceTest.java}      |  28 +++--
 .../remoting/impl/command/CodecHelperTest.java     | 139 +++++++++++++++++++++
 .../command/RemotingCommandFactoryImplTest.java    |  56 +++++++++
 .../impl/command/RequestIdGeneratorTest.java}      |  19 ++-
 15 files changed, 422 insertions(+), 38 deletions(-)

diff --git a/pom.xml b/pom.xml
index 2055a40..23eeeb5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,6 +46,12 @@
             <version>2.6.3</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.7.25</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>
diff --git 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/buffer/ByteBufferWrapper.java
 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/buffer/ByteBufferWrapper.java
index 7360c88..0b0c5fb 100644
--- 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/buffer/ByteBufferWrapper.java
+++ 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/buffer/ByteBufferWrapper.java
@@ -52,5 +52,9 @@ public interface ByteBufferWrapper {
 
     void setReaderIndex(int readerIndex);
 
+    int writerIndex();
+
+    void setWriterIndex(int writerIndex);
+
     void ensureCapacity(int capacity);
 }
diff --git 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/RemotingCommand.java
 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/RemotingCommand.java
index 8496b04..8595111 100644
--- 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/RemotingCommand.java
+++ 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/RemotingCommand.java
@@ -46,8 +46,6 @@ public interface RemotingCommand {
 
     Map<String, String> properties();
 
-    void properties(Map<String, String> value);
-
     String property(String key);
 
     void property(String key, String value);
diff --git 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/TrafficType.java
 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/TrafficType.java
index efebfe7..3c40e7f 100644
--- 
a/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/TrafficType.java
+++ 
b/remoting-core/remoting-api/src/main/java/org/apache/rocketmq/remoting/api/command/TrafficType.java
@@ -34,7 +34,7 @@ public enum TrafficType {
             case 3:
                 return RESPONSE;
             default:
-                throw new IllegalArgumentException("Not supported " + index);
+                throw new IllegalArgumentException("TrafficType " + index + " 
is not supported");
         }
     }
 }
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/common/ResponseFuture.java
 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/common/ResponseFuture.java
index 76f3472..8a2aec2 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/common/ResponseFuture.java
+++ 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/common/ResponseFuture.java
@@ -79,8 +79,7 @@ public class ResponseFuture {
     public RemotingCommand waitResponse(final long timeoutMillis) {
         try {
             this.countDownLatch.await(timeoutMillis, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
+        } catch (InterruptedException ignore) {
         }
         return this.responseCommand;
     }
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/buffer/NettyByteBufferWrapper.java
 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/buffer/NettyByteBufferWrapper.java
index d4fc15c..43545fe 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/buffer/NettyByteBufferWrapper.java
+++ 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/buffer/NettyByteBufferWrapper.java
@@ -104,8 +104,18 @@ public class NettyByteBufferWrapper implements 
ByteBufferWrapper {
     }
 
     @Override
-    public void setReaderIndex(int index) {
-        buffer.setIndex(index, buffer.writerIndex());
+    public void setReaderIndex(int readerIndex) {
+        buffer.setIndex(readerIndex, buffer.writerIndex());
+    }
+
+    @Override
+    public int writerIndex() {
+        return buffer.writerIndex();
+    }
+
+    @Override
+    public void setWriterIndex(int writerIndex) {
+        buffer.setIndex(buffer.readerIndex(), writerIndex);
     }
 
     @Override
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/CodecHelper.java
 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/CodecHelper.java
index 41a5595..8d3ff3a 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/CodecHelper.java
+++ 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/CodecHelper.java
@@ -31,9 +31,9 @@ public class CodecHelper {
     public final static byte PROTOCOL_MAGIC = 0x14;
     private final static char PROPERTY_SEPARATOR = '\n';
     private final static Charset REMOTING_CHARSET = Charset.forName("UTF-8");
-    private final static int REMARK_MAX_LEN = Short.MAX_VALUE;
-    private final static int PROPERTY_MAX_LEN = 524288; // 512KB
-    private final static int PAYLOAD_MAX_LEN = 16777216; // 16MB
+    final static int REMARK_MAX_LEN = Short.MAX_VALUE;
+    final static int PROPERTY_MAX_LEN = 524288; // 512KB
+    final static int PAYLOAD_MAX_LEN = 16777216; // 16MB
     public final static int PACKET_MAX_LEN = MIN_PROTOCOL_LEN + REMARK_MAX_LEN 
+ PROPERTY_MAX_LEN + PAYLOAD_MAX_LEN;
 
     public static void encodeCommand(final RemotingCommand command, final 
ByteBufferWrapper out) {
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandImpl.java
 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandImpl.java
index 3a67ead..4d1af44 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandImpl.java
+++ 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandImpl.java
@@ -17,6 +17,7 @@
 
 package org.apache.rocketmq.remoting.impl.command;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.commons.lang3.builder.EqualsBuilder;
@@ -103,12 +104,7 @@ public class RemotingCommandImpl implements 
RemotingCommand {
 
     @Override
     public Map<String, String> properties() {
-        return this.properties;
-    }
-
-    @Override
-    public void properties(Map<String, String> value) {
-        this.properties = value;
+        return Collections.unmodifiableMap(this.properties);
     }
 
     @Override
@@ -143,6 +139,6 @@ public class RemotingCommandImpl implements RemotingCommand 
{
 
     @Override
     public String toString() {
-        return ToStringBuilder.reflectionToString(this, 
ToStringStyle.MULTI_LINE_STYLE);
+        return ToStringBuilder.reflectionToString(this, 
ToStringStyle.SIMPLE_STYLE);
     }
 }
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
index 9b85c95..5d1c397 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
+++ 
b/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
@@ -25,7 +25,6 @@ public class RequestIdGenerator {
     private AtomicInteger generator = new AtomicInteger(0);
 
     private RequestIdGenerator() {
-
     }
 
     public int incrementAndGet() {
diff --git 
a/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/BaseTest.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/BaseTest.java
new file mode 100644
index 0000000..21c7b38
--- /dev/null
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/BaseTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.rocketmq.remoting;
+
+import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.rocketmq.remoting.api.command.RemotingCommand;
+import org.apache.rocketmq.remoting.api.command.TrafficType;
+import org.apache.rocketmq.remoting.external.ThreadUtils;
+import org.apache.rocketmq.remoting.impl.command.RemotingCommandFactoryImpl;
+
+public class BaseTest {
+    protected void runInThreads(Runnable runnable, int threadsNum) {
+        ExecutorService executor = Executors.newFixedThreadPool(threadsNum);
+        for (int i = 0; i < threadsNum; i++) {
+            executor.submit(runnable);
+        }
+        ThreadUtils.shutdownGracefully(executor, 5, TimeUnit.SECONDS);
+    }
+
+    protected void shouldNotReachHere() {
+        throw new RuntimeException("shouldn't reach here");
+    }
+
+    protected RemotingCommand randomRemotingCommand() {
+        RemotingCommand command = new 
RemotingCommandFactoryImpl().createRequest();
+
+        Random random = new Random(System.currentTimeMillis());
+
+        command.cmdCode((short) random.nextInt());
+        command.cmdVersion((short) random.nextInt());
+        command.remark(RandomStringUtils.random(1024));
+        command.opCode((short) random.nextInt());
+        command.payload(RandomStringUtils.random(2048).getBytes());
+
+        command.requestID(random.nextInt());
+        command.trafficType(TrafficType.REQUEST_SYNC);
+
+        int propertiesLen = 1 + random.nextInt(10);
+
+        for (int i = 0; i < propertiesLen; i++) {
+            command.property(RandomStringUtils.random(512), 
RandomStringUtils.random(1024));
+        }
+
+        return command;
+    }
+}
diff --git 
a/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/ResponseFutureTest.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/ResponseFutureTest.java
new file mode 100644
index 0000000..6292745
--- /dev/null
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/ResponseFutureTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.rocketmq.remoting.common;
+
+import java.util.concurrent.TimeUnit;
+import org.apache.rocketmq.remoting.BaseTest;
+import org.apache.rocketmq.remoting.api.AsyncHandler;
+import org.apache.rocketmq.remoting.api.command.RemotingCommand;
+import org.apache.rocketmq.remoting.impl.command.RemotingCommandFactoryImpl;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class ResponseFutureTest extends BaseTest {
+    private ResponseFuture future;
+    private RemotingCommandFactoryImpl factory = new 
RemotingCommandFactoryImpl();
+
+    @Test
+    public void executeAsyncHandler_WithSuccess() {
+        final RemotingCommand reqCommand = factory.createRequest();
+        final RemotingCommand resCommand = factory.createResponse(reqCommand);
+        future = new ResponseFuture(1, 3000, new AsyncHandler() {
+            @Override
+            public void onFailure(final RemotingCommand request, final 
Throwable cause) {
+                shouldNotReachHere();
+            }
+
+            @Override
+            public void onSuccess(final RemotingCommand response) {
+                assertEquals(resCommand, response);
+            }
+        }, null);
+
+        future.setRequestCommand(reqCommand);
+        future.setResponseCommand(resCommand);
+        future.executeAsyncHandler();
+    }
+
+    @Test
+    public void executeAsyncHandler_WithFailure() {
+        final RemotingCommand reqCommand = factory.createRequest();
+        final RemotingCommand resCommand = factory.createResponse(reqCommand);
+        final Throwable exception = new RuntimeException("Test Exception");
+        future = new ResponseFuture(1, 3000, new AsyncHandler() {
+            @Override
+            public void onFailure(final RemotingCommand request, final 
Throwable cause) {
+                assertEquals(reqCommand, request);
+                assertNull(future.getResponseCommand());
+                assertEquals(exception, cause);
+            }
+
+            @Override
+            public void onSuccess(final RemotingCommand response) {
+                assertEquals(resCommand, response);
+            }
+        }, null);
+
+        future.setRequestCommand(reqCommand);
+        future.setCause(exception);
+        future.executeAsyncHandler();
+    }
+
+    @Test
+    public void waitResponse_WithSuccess() {
+        future = new ResponseFuture(1, 1000, null, null);
+        final RemotingCommand reqCommand = factory.createRequest();
+        final RemotingCommand resCommand = factory.createResponse(reqCommand);
+
+        runInThreads(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    TimeUnit.MILLISECONDS.sleep(100);
+                } catch (InterruptedException ignore) {
+                }
+                future.putResponse(resCommand);
+            }
+        }, 1);
+        RemotingCommand response = future.waitResponse(1000);
+        assertEquals(response, resCommand);
+    }
+
+    @Test
+    public void waitResponse_WithTimeout() {
+        future = new ResponseFuture(1, 1000, null, null);
+        RemotingCommand response = future.waitResponse(10);
+        assertNull(response);
+    }
+}
\ No newline at end of file
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/SemaphoreReleaseOnlyOnceTest.java
similarity index 56%
copy from 
remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
copy to 
remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/SemaphoreReleaseOnlyOnceTest.java
index 9b85c95..312e3b7 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/common/SemaphoreReleaseOnlyOnceTest.java
@@ -15,20 +15,28 @@
  * limitations under the License.
  */
 
-package org.apache.rocketmq.remoting.impl.command;
+package org.apache.rocketmq.remoting.common;
 
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.Semaphore;
+import org.apache.rocketmq.remoting.BaseTest;
+import org.junit.Test;
 
-public class RequestIdGenerator {
-    public static RequestIdGenerator inst = new RequestIdGenerator();
+import static org.junit.Assert.*;
 
-    private AtomicInteger generator = new AtomicInteger(0);
+public class SemaphoreReleaseOnlyOnceTest extends BaseTest {
 
-    private RequestIdGenerator() {
+    @Test
+    public void release() {
+        Semaphore semaphore = new Semaphore(0);
+        final SemaphoreReleaseOnlyOnce once = new 
SemaphoreReleaseOnlyOnce(semaphore);
 
-    }
+        runInThreads(new Runnable() {
+            @Override
+            public void run() {
+                once.release();
+            }
+        }, 10);
 
-    public int incrementAndGet() {
-        return generator.incrementAndGet();
+        assertEquals(1, semaphore.availablePermits());
     }
-}
+}
\ No newline at end of file
diff --git 
a/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/CodecHelperTest.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/CodecHelperTest.java
new file mode 100644
index 0000000..7c1c037
--- /dev/null
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/CodecHelperTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.rocketmq.remoting.impl.command;
+
+import io.netty.buffer.ByteBufAllocator;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.rocketmq.remoting.BaseTest;
+import org.apache.rocketmq.remoting.api.buffer.ByteBufferWrapper;
+import org.apache.rocketmq.remoting.api.command.RemotingCommand;
+import org.apache.rocketmq.remoting.api.exception.RemoteCodecException;
+import org.apache.rocketmq.remoting.impl.buffer.NettyByteBufferWrapper;
+import org.junit.Test;
+
+import static 
org.apache.rocketmq.remoting.impl.command.CodecHelper.PAYLOAD_MAX_LEN;
+import static 
org.apache.rocketmq.remoting.impl.command.CodecHelper.PROPERTY_MAX_LEN;
+import static 
org.apache.rocketmq.remoting.impl.command.CodecHelper.PROTOCOL_MAGIC;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown;
+import static org.junit.Assert.assertEquals;
+
+public class CodecHelperTest extends BaseTest {
+
+    @Test
+    public void encodeAndDecodeCommand() {
+        ByteBufferWrapper buffer = new 
NettyByteBufferWrapper(ByteBufAllocator.DEFAULT.heapBuffer());
+        RemotingCommand command = randomRemotingCommand();
+        CodecHelper.encodeCommand(command, buffer);
+
+        // Skip magic code and total length
+        assertEquals(PROTOCOL_MAGIC, buffer.readByte());
+        buffer.readInt();
+
+        RemotingCommand decodedCommand = CodecHelper.decode(buffer);
+
+        assertEquals(command, decodedCommand);
+    }
+
+    @Test
+    public void encodeCommand_WithException() {
+        ByteBufferWrapper buffer = new 
NettyByteBufferWrapper(ByteBufAllocator.DEFAULT.heapBuffer());
+        RemotingCommand command = randomRemotingCommand();
+
+        // Remark len exceed max limit
+        
command.remark(RandomStringUtils.randomAlphabetic(CodecHelper.REMARK_MAX_LEN + 
1));
+        try {
+            CodecHelper.encodeCommand(command, buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+
+        command = randomRemotingCommand();
+        command.property("a", 
RandomStringUtils.randomAlphabetic(Short.MAX_VALUE));
+
+        try {
+            CodecHelper.encodeCommand(command, buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+
+        command = randomRemotingCommand();
+        command.property("a", 
RandomStringUtils.randomAlphabetic(CodecHelper.PROPERTY_MAX_LEN));
+
+        try {
+            CodecHelper.encodeCommand(command, buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+
+        command = randomRemotingCommand();
+        
command.payload(RandomStringUtils.randomAlphabetic(CodecHelper.PAYLOAD_MAX_LEN 
+ 1).getBytes());
+
+        try {
+            CodecHelper.encodeCommand(command, buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+    }
+
+    @Test
+    public void decodeCommand_WithException() {
+        ByteBufferWrapper buffer = new 
NettyByteBufferWrapper(ByteBufAllocator.DEFAULT.heapBuffer());
+
+        buffer.writeShort((short) 0);
+        buffer.writeShort((short) 0);
+        buffer.writeInt(0);
+        buffer.writeByte((byte) 0);
+        buffer.writeShort((short) 0);
+
+        buffer.writeShort((short) 0);
+        int writerIndex = buffer.writerIndex();
+
+        int propsSize = 1 + PROPERTY_MAX_LEN / Short.MAX_VALUE;
+        buffer.writeShort((short) propsSize);
+
+        for (int i = 0; i < propsSize; i++) {
+            buffer.writeShort(Short.MAX_VALUE);
+            buffer.writeBytes(new byte[Short.MAX_VALUE]);
+        }
+
+        try {
+            CodecHelper.decode(buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+
+        buffer.setReaderIndex(0);
+        buffer.setWriterIndex(writerIndex);
+        buffer.writeShort((short) 0);
+
+        buffer.writeInt(PAYLOAD_MAX_LEN + 1);
+
+        try {
+            CodecHelper.decode(buffer);
+            failBecauseExceptionWasNotThrown(RemoteCodecException.class);
+        } catch (Exception e) {
+            assertThat(e).isInstanceOf(RemoteCodecException.class);
+        }
+    }
+}
\ No newline at end of file
diff --git 
a/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandFactoryImplTest.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandFactoryImplTest.java
new file mode 100644
index 0000000..aa6e9ee
--- /dev/null
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RemotingCommandFactoryImplTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.rocketmq.remoting.impl.command;
+
+import org.apache.rocketmq.remoting.api.command.RemotingCommand;
+import org.apache.rocketmq.remoting.api.command.RemotingCommandFactory;
+import org.apache.rocketmq.remoting.api.command.TrafficType;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class RemotingCommandFactoryImplTest {
+    private RemotingCommandFactory factory = new RemotingCommandFactoryImpl();
+
+    @Test
+    public void createRequest() {
+        RemotingCommand request = factory.createRequest();
+
+        assertEquals(request.cmdCode(), 0);
+        assertEquals(request.cmdVersion(), 0);
+        assertEquals(request.opCode(), RemotingSysResponseCode.SUCCESS);
+        assertNull(request.payload());
+        assertTrue(request.properties().isEmpty());
+        assertNotEquals(request.requestID(), 0);
+        assertEquals(request.remark(), "");
+        assertEquals(request.trafficType(), TrafficType.REQUEST_SYNC);
+    }
+
+    @Test
+    public void createResponse() {
+        RemotingCommand request = factory.createRequest();
+        request.cmdVersion((short) 123);
+        request.cmdCode((short) 100);
+        RemotingCommand response = factory.createResponse(request);
+
+        assertEquals(response.cmdVersion(), request.cmdVersion());
+        assertEquals(response.cmdCode(), request.cmdCode());
+        assertEquals(response.trafficType(), TrafficType.RESPONSE);
+        assertEquals(response.requestID(), request.requestID());
+    }
+}
\ No newline at end of file
diff --git 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RequestIdGeneratorTest.java
similarity index 71%
copy from 
remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
copy to 
remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RequestIdGeneratorTest.java
index 9b85c95..75d0ff1 100644
--- 
a/remoting-core/remoting-impl/src/main/java/org/apache/rocketmq/remoting/impl/command/RequestIdGenerator.java
+++ 
b/remoting-core/remoting-impl/src/test/java/org/apache/rocketmq/remoting/impl/command/RequestIdGeneratorTest.java
@@ -17,18 +17,17 @@
 
 package org.apache.rocketmq.remoting.impl.command;
 
-import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.Test;
 
-public class RequestIdGenerator {
-    public static RequestIdGenerator inst = new RequestIdGenerator();
+import static org.junit.Assert.*;
 
-    private AtomicInteger generator = new AtomicInteger(0);
+public class RequestIdGeneratorTest {
 
-    private RequestIdGenerator() {
+    @Test
+    public void incrementAndGet() {
+        int numA = RequestIdGenerator.inst.incrementAndGet();
+        int numB = RequestIdGenerator.inst.incrementAndGet();
 
+        assertEquals(numA, numB - 1);
     }
-
-    public int incrementAndGet() {
-        return generator.incrementAndGet();
-    }
-}
+}
\ No newline at end of file

Reply via email to