You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by iskl <gi...@git.apache.org> on 2017/01/11 09:07:35 UTC

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

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 <sh...@kailai.me>
Date:   2017-01-10T08:52:09Z

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

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

    [ROCKETMQ-38] Add unit tests for RemotingCommand encoding and decoding

commit 79c5b9951727a499f6481719e31a8306c8ab1959
Author: Kailai Shao <sh...@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.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

Posted by zhouxinyu <gi...@git.apache.org>.
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.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by vongosling <gi...@git.apache.org>.
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.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by zhouxinyu <gi...@git.apache.org>.
Github user zhouxinyu commented on the issue:

    https://github.com/apache/incubator-rocketmq/pull/37
  
    It seems ok, please @vongosling @lizhanhui help review.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

Posted by WillemJiang <gi...@git.apache.org>.
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.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

Posted by iskl <gi...@git.apache.org>.
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...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by zhouxinyu <gi...@git.apache.org>.
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.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

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


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq pull request #37: [ROCKETMQ-38] Some unit tests for rocke...

Posted by iskl <gi...@git.apache.org>.
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~


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by iskl <gi...@git.apache.org>.
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~ :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by iskl <gi...@git.apache.org>.
Github user iskl commented on the issue:

    https://github.com/apache/incubator-rocketmq/pull/37
  
    @zhouxinyu Apologize... It's my mistake to use a wrong version of assertj.-core I've modified it to the correct version for jdk7 :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-rocketmq issue #37: [ROCKETMQ-38] Some unit tests for rocketmq-rem...

Posted by shroman <gi...@git.apache.org>.
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 :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---