Details

    • Type: Sub-task
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 4.0.0-incubating
    • Fix Version/s: 4.0.0-incubating
    • Component/s: rocketmq-remoting
    • Labels:
      None

      Description

      Polish unit tests for rocketmq-remoting.

        Issue Links

          Activity

          Hide
          roman_s Roman Shtykh added a comment -

          I would consider it `Major` issue

          Also, I think we have to split this task in sub-tasks for every module.

          Show
          roman_s Roman Shtykh added a comment - I would consider it `Major` issue Also, I think we have to split this task in sub-tasks for every module.
          Hide
          vongosling vongosling added a comment -

          Good idea. But, firstly, we should provide some guide for Unit test.

          1. Using testMethod_Condition naming conversion,
          Because it's more friendly for maven surefire and idea moreunit plugin

          2. Mock the module's interaction parts.
          We can use mockito tool to do this

          3. More assert instead of simple printting
          Recommend to use assert4j tool

          4. Do not throw exception in test method

          5. Use latch instead of thead.sleep

          6. Pay more attention for your test time-consuming

          Show
          vongosling vongosling added a comment - Good idea. But, firstly, we should provide some guide for Unit test. 1. Using testMethod_Condition naming conversion, Because it's more friendly for maven surefire and idea moreunit plugin 2. Mock the module's interaction parts. We can use mockito tool to do this 3. More assert instead of simple printting Recommend to use assert4j tool 4. Do not throw exception in test method 5. Use latch instead of thead.sleep 6. Pay more attention for your test time-consuming
          Hide
          kailai Kailai Shao added a comment -

          Roman Shtykh Thanks for your suggestion. I've modified the issue.

          Show
          kailai Kailai Shao added a comment - Roman Shtykh Thanks for your suggestion. I've modified the issue.
          Hide
          roman_s Roman Shtykh added a comment -

          Definitely, this is good to have such a guideline. How about having it on http://rocketmq.apache.org/docs/code-guidelines/?

          Show
          roman_s Roman Shtykh added a comment - Definitely, this is good to have such a guideline. How about having it on http://rocketmq.apache.org/docs/code-guidelines/?
          Hide
          kailai Kailai Shao added a comment -

          vongosling Thanks for your professional guidance. I will follow these best practices.

          Show
          kailai Kailai Shao added a comment - vongosling Thanks for your professional guidance. I will follow these best practices.
          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user iskl opened a pull request:

          https://github.com/apache/incubator-rocketmq/pull/37

          ROCKETMQ-38 Some unit tests for rocketmq-remoting

          [ref issue](https://issues.apache.org/jira/browse/ROCKETMQ-38).

          I wish to improve unit test coverage for RocketMQ and wrote these *sample* tests following @vongosling 's guidance.

          > 1. Using testMethod_Condition naming conversion,
          Because it's more friendly for maven surefire and idea moreunit plugin
          > 2. Mock the module's interaction parts.
          We can use mockito tool to do this
          > 3. More assert instead of simple printting.
          Recommend to use assert4j tool
          > 4. Do not throw exception in test method
          > 5. Use latch instead of thead.sleep
          > 6. Pay more attention for your test time-consuming

          @vongosling , @zhouxinyu , Would you mind review these *sample* test code to see whether the code style is suitable? Peg for suggestion. If that's OK, The remaining part would be finished just like the sample test code.

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/iskl/incubator-rocketmq ROCKETMQ-38

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/incubator-rocketmq/pull/37.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #37


          commit 62da108245029c10f8fe84c8b041ec6c44d73493
          Author: Kailai Shao <shao@kailai.me>
          Date: 2017-01-10T08:52:09Z

          ROCKETMQ-38 Add unit tests for request & response RemotingCommand creating.

          commit bca350ff17f2fab2aee4e350d0692e08fab59e7c
          Author: Kailai Shao <shao@kailai.me>
          Date: 2017-01-11T06:36:22Z

          ROCKETMQ-38 Add unit tests for RemotingCommand encoding and decoding

          commit 79c5b9951727a499f6481719e31a8306c8ab1959
          Author: Kailai Shao <shao@kailai.me>
          Date: 2017-01-11T07:46:43Z

          ROCKETMQ-38 According to vongosling's guidance, modify the code style and use AssertJ instead of JUnit.


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user iskl opened a pull request: https://github.com/apache/incubator-rocketmq/pull/37 ROCKETMQ-38 Some unit tests for rocketmq-remoting [ref issue] ( https://issues.apache.org/jira/browse/ROCKETMQ-38 ). I wish to improve unit test coverage for RocketMQ and wrote these * sample * tests following @vongosling 's guidance. > 1. Using testMethod_Condition naming conversion, Because it's more friendly for maven surefire and idea moreunit plugin > 2. Mock the module's interaction parts. We can use mockito tool to do this > 3. More assert instead of simple printting. Recommend to use assert4j tool > 4. Do not throw exception in test method > 5. Use latch instead of thead.sleep > 6. Pay more attention for your test time-consuming @vongosling , @zhouxinyu , Would you mind review these * sample * test code to see whether the code style is suitable? Peg for suggestion. If that's OK, The remaining part would be finished just like the sample test code. You can merge this pull request into a Git repository by running: $ git pull https://github.com/iskl/incubator-rocketmq ROCKETMQ-38 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/incubator-rocketmq/pull/37.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #37 commit 62da108245029c10f8fe84c8b041ec6c44d73493 Author: Kailai Shao <shao@kailai.me> Date: 2017-01-10T08:52:09Z ROCKETMQ-38 Add unit tests for request & response RemotingCommand creating. commit bca350ff17f2fab2aee4e350d0692e08fab59e7c Author: Kailai Shao <shao@kailai.me> Date: 2017-01-11T06:36:22Z ROCKETMQ-38 Add unit tests for RemotingCommand encoding and decoding commit 79c5b9951727a499f6481719e31a8306c8ab1959 Author: Kailai Shao <shao@kailai.me> Date: 2017-01-11T07:46:43Z ROCKETMQ-38 According to vongosling's guidance, modify the code style and use AssertJ instead of JUnit.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user zhouxinyu commented on the issue:

          https://github.com/apache/incubator-rocketmq/pull/37

          Hi @iskl, please make sure the PR can pass the CI test.

          Show
          githubbot ASF GitHub Bot added a comment - Github user zhouxinyu commented on the issue: https://github.com/apache/incubator-rocketmq/pull/37 Hi @iskl, please make sure the PR can pass the CI test.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user WillemJiang commented on a diff in the pull request:

          https://github.com/apache/incubator-rocketmq/pull/37#discussion_r95748248

          — 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 –

          We don't need to catch the exception here. The unit test can be failed when the exception is thrown.

          Show
          githubbot ASF GitHub Bot added a comment - Github user WillemJiang commented on a diff in the pull request: https://github.com/apache/incubator-rocketmq/pull/37#discussion_r95748248 — 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 – We don't need to catch the exception here. The unit test can be failed when the exception is thrown.
          Hide
          githubbot ASF GitHub Bot added a comment -

          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...

          Show
          githubbot ASF GitHub Bot added a comment - 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...
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user zhouxinyu commented on a diff in the pull request:

          https://github.com/apache/incubator-rocketmq/pull/37#discussion_r96000424

          — Diff: remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingCommandTest.java —
          @@ -0,0 +1,222 @@
          +/*
          + * 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 response + }

          +
          + @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 response + }

          +
          + @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() throws RemotingCommandException

          { + 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"); + + 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); + }

          +}
          +
          +class SampleCommandCustomHeader implements CommandCustomHeader {
          + @Override
          + public void checkFields() throws RemotingCommandException {
          + return;
          — End diff –

          I think we can remove the empty return.

          Show
          githubbot ASF GitHub Bot added a comment - Github user zhouxinyu commented on a diff in the pull request: https://github.com/apache/incubator-rocketmq/pull/37#discussion_r96000424 — Diff: remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingCommandTest.java — @@ -0,0 +1,222 @@ +/* + * 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 response + } + + @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 response + } + + @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() throws RemotingCommandException { + 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"); + + 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); + } +} + +class SampleCommandCustomHeader implements CommandCustomHeader { + @Override + public void checkFields() throws RemotingCommandException { + return; — End diff – I think we can remove the empty return.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user zhouxinyu commented on the issue:

          https://github.com/apache/incubator-rocketmq/pull/37

          It seems ok, please @vongosling @lizhanhui help review.

          Show
          githubbot ASF GitHub Bot added a comment - Github user zhouxinyu commented on the issue: https://github.com/apache/incubator-rocketmq/pull/37 It seems ok, please @vongosling @lizhanhui help review.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user shroman commented on the issue:

          https://github.com/apache/incubator-rocketmq/pull/37

          A suggestion: how about renaming this JIRA issue? This issue is for RemotingCommand, 'some' is very vague.
          We can end up with many more 'some' tests in future

          Show
          githubbot ASF GitHub Bot added a comment - Github user shroman commented on the issue: https://github.com/apache/incubator-rocketmq/pull/37 A suggestion: how about renaming this JIRA issue? This issue is for RemotingCommand, 'some' is very vague. We can end up with many more 'some' tests in future
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user iskl commented on the issue:

          https://github.com/apache/incubator-rocketmq/pull/37

          @shroman , issue-38 is for the whole rocketmq-remoting submoudle. The code already committed is the sample just for code style suggestion. I am writing the remaining test cases and nearly complete. For other submodules, I will create new issues~

          Show
          githubbot ASF GitHub Bot added a comment - Github user iskl commented on the issue: https://github.com/apache/incubator-rocketmq/pull/37 @shroman , issue-38 is for the whole rocketmq-remoting submoudle. The code already committed is the sample just for code style suggestion. I am writing the remaining test cases and nearly complete. For other submodules, I will create new issues~
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user iskl commented on a diff in the pull request:

          https://github.com/apache/incubator-rocketmq/pull/37#discussion_r96157152

          — Diff: remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingCommandTest.java —
          @@ -0,0 +1,222 @@
          +/*
          + * 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 response + }

          +
          + @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 response + }

          +
          + @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() throws RemotingCommandException

          { + 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"); + + 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); + }

          +}
          +
          +class SampleCommandCustomHeader implements CommandCustomHeader {
          + @Override
          + public void checkFields() throws RemotingCommandException {
          + return;
          — End diff –

          You're right~ I will improve it in next commit~

          Show
          githubbot ASF GitHub Bot added a comment - Github user iskl commented on a diff in the pull request: https://github.com/apache/incubator-rocketmq/pull/37#discussion_r96157152 — Diff: remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingCommandTest.java — @@ -0,0 +1,222 @@ +/* + * 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 response + } + + @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 response + } + + @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() throws RemotingCommandException { + 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"); + + 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); + } +} + +class SampleCommandCustomHeader implements CommandCustomHeader { + @Override + public void checkFields() throws RemotingCommandException { + return; — End diff – You're right~ I will improve it in next commit~
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 91675effe6942fc5e7e460f96639199f5b36b839 in incubator-rocketmq's branch refs/heads/master from Kailai Shao
          [ https://git-wip-us.apache.org/repos/asf?p=incubator-rocketmq.git;h=91675ef ]

          ROCKETMQ-38 Polish unit tests for remoting module, closes apache/incubator-rocketmq#37

          Show
          jira-bot ASF subversion and git services added a comment - Commit 91675effe6942fc5e7e460f96639199f5b36b839 in incubator-rocketmq's branch refs/heads/master from Kailai Shao [ https://git-wip-us.apache.org/repos/asf?p=incubator-rocketmq.git;h=91675ef ] ROCKETMQ-38 Polish unit tests for remoting module, closes apache/incubator-rocketmq#37
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/incubator-rocketmq/pull/37

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/incubator-rocketmq/pull/37
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user vongosling commented on the issue:

          https://github.com/apache/incubator-rocketmq/pull/37

          Thanks @iskl @shroman @WillemJiang , I will work on the rocketmq remoting module these days.

          Show
          githubbot ASF GitHub Bot added a comment - Github user vongosling commented on the issue: https://github.com/apache/incubator-rocketmq/pull/37 Thanks @iskl @shroman @WillemJiang , I will work on the rocketmq remoting module these days.

            People

            • Assignee:
              vongosling vongosling
              Reporter:
              kailai Kailai Shao
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development