[
https://issues.apache.org/jira/browse/ROCKETMQ-38?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15820617#comment-15820617
]
ASF GitHub Bot commented on ROCKETMQ-38:
----------------------------------------
Github user iskl commented on a diff in the pull request:
https://github.com/apache/incubator-rocketmq/pull/37#discussion_r95756507
--- Diff:
remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingCommandTest.java
---
@@ -0,0 +1,227 @@
+/*
+ * 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.protocol;
+
+import static org.assertj.core.api.Assertions.*;
+
+import java.nio.ByteBuffer;
+import org.apache.rocketmq.remoting.CommandCustomHeader;
+import org.apache.rocketmq.remoting.exception.RemotingCommandException;
+import org.junit.Test;
+
+public class RemotingCommandTest {
+ @Test
+ public void testMarkProtocolType_JSONProtocolType() {
+ int source = 261;
+ SerializeType type = SerializeType.JSON;
+ byte[] result = RemotingCommand.markProtocolType(source, type);
+ assertThat(result).isEqualTo(new byte[]{0, 0, 1, 5});
+ }
+
+ @Test
+ public void testMarkProtocolType_ROCKETMQProtocolType() {
+ int source = 16777215;
+ SerializeType type = SerializeType.ROCKETMQ;
+ byte[] result = RemotingCommand.markProtocolType(source, type);
+ assertThat(result).isEqualTo(new byte[]{1, -1, -1, -1});
+ }
+
+ @Test
+ public void testCreateRequestCommand_RegisterBroker() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = 103;
//org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
+ CommandCustomHeader header = new SampleCommandCustomHeader();
+ RemotingCommand cmd = RemotingCommand.createRequestCommand(code,
header);
+ assertThat(cmd.getCode()).isEqualTo(code);
+ assertThat(cmd.getVersion()).isEqualTo(2333);
+ assertThat(cmd.getFlag() & 0x01).isEqualTo(0); //flag bit 0: 0
presents request
+ }
+
+ @Test
+ public void testCreateResponseCommand_SuccessWithHeader() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = RemotingSysResponseCode.SUCCESS;
+ String remark = "Sample remark";
+ RemotingCommand cmd = RemotingCommand.createResponseCommand(code
,remark, SampleCommandCustomHeader.class);
+ assertThat(cmd.getCode()).isEqualTo(code);
+ assertThat(cmd.getVersion()).isEqualTo(2333);
+ assertThat(cmd.getRemark()).isEqualTo(remark);
+ assertThat(cmd.getFlag() & 0x01).isEqualTo(1); //flag bit 0: 1
presents request
+ }
+
+ @Test
+ public void testCreateResponseCommand_SuccessWithoutHeader() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = RemotingSysResponseCode.SUCCESS;
+ String remark = "Sample remark";
+ RemotingCommand cmd = RemotingCommand.createResponseCommand(code
,remark);
+ assertThat(cmd.getCode()).isEqualTo(code);
+ assertThat(cmd.getVersion()).isEqualTo(2333);
+ assertThat(cmd.getRemark()).isEqualTo(remark);
+ assertThat(cmd.getFlag() & 0x01).isEqualTo(1); //flag bit 0: 1
presents request
+ }
+
+ @Test
+ public void testCreateResponseCommand_FailToCreateCommand() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = RemotingSysResponseCode.SUCCESS;
+ String remark = "Sample remark";
+ RemotingCommand cmd = RemotingCommand.createResponseCommand(code
,remark, CommandCustomHeader.class);
+ assertThat(cmd).isNull();
+ }
+
+ @Test
+ public void testCreateResponseCommand_SystemError() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ RemotingCommand cmd =
RemotingCommand.createResponseCommand(SampleCommandCustomHeader.class);
+
assertThat(cmd.getCode()).isEqualTo(RemotingSysResponseCode.SYSTEM_ERROR);
+ assertThat(cmd.getVersion()).isEqualTo(2333);
+ assertThat(cmd.getRemark()).contains("not set any response code");
+ assertThat(cmd.getFlag() & 0x01).isEqualTo(1); //flag bit 0: 1
presents response
+ }
+
+ @Test
+ public void testEncodeAndDecode_EmptyBody() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = 103;
//org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
+ CommandCustomHeader header = new SampleCommandCustomHeader();
+ RemotingCommand cmd = RemotingCommand.createRequestCommand(code,
header);
+
+ ByteBuffer buffer = cmd.encode();
+
+ //Simulate buffer being read in NettyDecoder
+ buffer.getInt();
+ byte[] bytes = new byte[buffer.limit() - 4];
+ buffer.get(bytes, 0, buffer.limit() - 4);
+ buffer = ByteBuffer.wrap(bytes);
+
+ RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
+
+
assertThat(decodedCommand.getSerializeTypeCurrentRPC()).isEqualTo(SerializeType.JSON);
+ assertThat(decodedCommand.getBody()).isNull();
+ }
+
+ @Test
+ public void testEncodeAndDecode_FilledBody() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = 103;
//org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
+ CommandCustomHeader header = new SampleCommandCustomHeader();
+ RemotingCommand cmd = RemotingCommand.createRequestCommand(code,
header);
+ cmd.setBody(new byte[] { 0, 1, 2, 3, 4});
+
+ ByteBuffer buffer = cmd.encode();
+
+ //Simulate buffer being read in NettyDecoder
+ buffer.getInt();
+ byte[] bytes = new byte[buffer.limit() - 4];
+ buffer.get(bytes, 0, buffer.limit() - 4);
+ buffer = ByteBuffer.wrap(bytes);
+
+ RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
+
+
assertThat(decodedCommand.getSerializeTypeCurrentRPC()).isEqualTo(SerializeType.JSON);
+ assertThat(decodedCommand.getBody()).isEqualTo(new byte[]{ 0, 1,
2, 3, 4});
+ }
+
+ @Test
+ public void testEncodeAndDecode_FilledBodyWithExtFields() {
+ System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
+
+ int code = 103;
//org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
+ CommandCustomHeader header = new ExtFieldsHeader();
+ RemotingCommand cmd = RemotingCommand.createRequestCommand(code,
header);
+
+ cmd.addExtField("key", "value");
+
+ ByteBuffer buffer = cmd.encode();
+
+ //Simulate buffer being read in NettyDecoder
+ buffer.getInt();
+ byte[] bytes = new byte[buffer.limit() - 4];
+ buffer.get(bytes, 0, buffer.limit() - 4);
+ buffer = ByteBuffer.wrap(bytes);
+
+ RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
+
+
assertThat(decodedCommand.getExtFields().get("stringValue")).isEqualTo("bilibili");
+
assertThat(decodedCommand.getExtFields().get("intValue")).isEqualTo("2333");
+
assertThat(decodedCommand.getExtFields().get("longValue")).isEqualTo("23333333");
+
assertThat(decodedCommand.getExtFields().get("booleanValue")).isEqualTo("true");
+
assertThat(decodedCommand.getExtFields().get("doubleValue")).isEqualTo("0.618");
+
+
assertThat(decodedCommand.getExtFields().get("key")).isEqualTo("value");
+
+ try {
+ CommandCustomHeader decodedHeader =
decodedCommand.decodeCommandCustomHeader(ExtFieldsHeader.class);
+
assertThat(((ExtFieldsHeader)decodedHeader).getStringValue()).isEqualTo("bilibili");
+
assertThat(((ExtFieldsHeader)decodedHeader).getIntValue()).isEqualTo(2333);
+
assertThat(((ExtFieldsHeader)decodedHeader).getLongValue()).isEqualTo(23333333l);
+
assertThat(((ExtFieldsHeader)decodedHeader).isBooleanValue()).isEqualTo(true);
+
assertThat(((ExtFieldsHeader)decodedHeader).getDoubleValue()).isBetween(0.617,
0.619);
+
+ } catch (RemotingCommandException ex) {
--- End diff --
Thanks for your correction... I will keep in mind in my future work...
> Unit test for RocketMQ
> ----------------------
>
> Key: ROCKETMQ-38
> URL: https://issues.apache.org/jira/browse/ROCKETMQ-38
> Project: Apache RocketMQ
> Issue Type: Bug
> Reporter: Kailai Shao
> Assignee: vongosling
> Fix For: 4.1.0-incubating
>
>
> The issue is to provide unit tests and improve code coverage for RocketMQ. I
> would start from sub-module rocketmq-remoting since the sub-module is more
> independent in the whole project.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)