From e447bd01759da37cff2358ded09d8349e143c818 Mon Sep 17 00:00:00 2001 From: stevenschew Date: Sun, 22 Jan 2017 15:04:32 +0800 Subject: [PATCH] [ROCKETMQ-54] Add unit tests for rocketmq-namesrv --- .../namesrv/NameServerInstanceTest.java | 47 ++++++++ .../namesrv/NamesrvControllerTest.java | 47 ++++++++ .../namesrv/kvconfig/KVConfigManagerTest.java | 54 +++++++++ .../KVConfigSerializeWrapperTest.java | 53 +++++++++ .../DefaultRequestProcessorTest.java | 19 ++-- .../routeinfo/RouteInfoManagerTest.java | 103 ++++++++++++++++++ 6 files changed, 313 insertions(+), 10 deletions(-) create mode 100644 namesrv/src/test/java/org/apache/rocketmq/namesrv/NameServerInstanceTest.java create mode 100644 namesrv/src/test/java/org/apache/rocketmq/namesrv/NamesrvControllerTest.java create mode 100644 namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigManagerTest.java create mode 100644 namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigSerializeWrapperTest.java create mode 100644 namesrv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/RouteInfoManagerTest.java diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/NameServerInstanceTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/NameServerInstanceTest.java new file mode 100644 index 00000000..83ab103f --- /dev/null +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/NameServerInstanceTest.java @@ -0,0 +1,47 @@ +/* + * 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.namesrv; + +import org.apache.rocketmq.common.namesrv.NamesrvConfig; +import org.apache.rocketmq.remoting.netty.NettyServerConfig; +import org.junit.After; +import org.junit.Before; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NameServerInstanceTest { + protected NamesrvController nameSrvController = null; + protected NettyServerConfig nettyServerConfig = new NettyServerConfig(); + protected NamesrvConfig namesrvConfig = new NamesrvConfig(); + + @Before + public void startup() throws Exception { + nettyServerConfig.setListenPort(9876); + nameSrvController = new NamesrvController(namesrvConfig, nettyServerConfig); + boolean initResult = nameSrvController.initialize(); + assertThat(initResult).isTrue(); + nameSrvController.start(); + } + + @After + public void shutdown() throws Exception { + if (nameSrvController != null) { + nameSrvController.shutdown(); + } + //maybe need to clean the file store. But we do not suggest deleting anything. + } +} diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/NamesrvControllerTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/NamesrvControllerTest.java new file mode 100644 index 00000000..215d051c --- /dev/null +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/NamesrvControllerTest.java @@ -0,0 +1,47 @@ +/* + * 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.namesrv; + +import org.apache.rocketmq.common.namesrv.NamesrvConfig; +import org.apache.rocketmq.remoting.netty.NettyServerConfig; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NamesrvControllerTest { + private final int RESTART_NUM = 2; + + /** + * Tests if the controller can be properly stopped and started. + * + * @throws Exception If fails. + */ + @Test + public void testRestart() throws Exception { + for (int i = 0; i < RESTART_NUM; i++) { + NamesrvController namesrvController = new NamesrvController( + new NamesrvConfig(), + new NettyServerConfig() + ); + boolean initResult = namesrvController.initialize(); + assertThat(initResult).isEqualTo(true); + namesrvController.start(); + namesrvController.shutdown(); + } + } + +} \ No newline at end of file diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigManagerTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigManagerTest.java new file mode 100644 index 00000000..8efa536f --- /dev/null +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigManagerTest.java @@ -0,0 +1,54 @@ +/* + * 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.namesrv.kvconfig; + +import org.apache.rocketmq.common.namesrv.NamesrvUtil; +import org.apache.rocketmq.namesrv.NameServerInstanceTest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class KVConfigManagerTest extends NameServerInstanceTest { + private KVConfigManager kvConfigManager; + + @Before + public void setup() throws Exception { + kvConfigManager = new KVConfigManager(nameSrvController); + } + + @Test + public void testPutKVConfig() { + kvConfigManager.load(); + kvConfigManager.putKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest", "test"); + byte[] kvConfig = kvConfigManager.getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG); + assertThat(kvConfig).isNotNull(); + String value = kvConfigManager.getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest"); + assertThat(value).isEqualTo("test"); + } + + @Test + public void testDeleteKVConfig() { + kvConfigManager.deleteKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest"); + byte[] kvConfig = kvConfigManager.getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG); + assertThat(kvConfig).isNull(); + Assert.assertTrue(kvConfig == null); + String value = kvConfigManager.getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest"); + assertThat(value).isNull(); + } +} \ No newline at end of file diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigSerializeWrapperTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigSerializeWrapperTest.java new file mode 100644 index 00000000..a24d86dc --- /dev/null +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/kvconfig/KVConfigSerializeWrapperTest.java @@ -0,0 +1,53 @@ +/* + * 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.namesrv.kvconfig; + +import java.util.HashMap; +import org.apache.rocketmq.common.namesrv.NamesrvUtil; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class KVConfigSerializeWrapperTest { + private KVConfigSerializeWrapper kvConfigSerializeWrapper; + + @Before + public void setup() throws Exception { + kvConfigSerializeWrapper = new KVConfigSerializeWrapper(); + } + + @Test + public void testEncodeAndDecode() { + HashMap> result = new HashMap<>(); + HashMap kvs = new HashMap<>(); + kvs.put("broker-name", "default-broker"); + kvs.put("topic-name", "default-topic"); + kvs.put("cid", "default-consumer-name"); + result.put(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, kvs); + kvConfigSerializeWrapper.setConfigTable(result); + byte[] serializeByte = KVConfigSerializeWrapper.encode(kvConfigSerializeWrapper); + assertThat(serializeByte).isNotNull(); + + KVConfigSerializeWrapper deserializeObject = KVConfigSerializeWrapper.decode(serializeByte, KVConfigSerializeWrapper.class); + assertThat(deserializeObject.getConfigTable()).containsKey(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG); + assertThat(deserializeObject.getConfigTable().get(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG).get("broker-name")).isEqualTo("default-broker"); + assertThat(deserializeObject.getConfigTable().get(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG).get("topic-name")).isEqualTo("default-topic"); + assertThat(deserializeObject.getConfigTable().get(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG).get("cid")).isEqualTo("default-consumer-name"); + } + +} \ No newline at end of file diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessorTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessorTest.java index 861e2841..80d968f7 100644 --- a/namesrv/src/test/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessorTest.java +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessorTest.java @@ -41,19 +41,20 @@ import org.junit.Test; import org.slf4j.Logger; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DefaultRequestProcessorTest { /** Test Target */ private DefaultRequestProcessor defaultRequestProcessor; - private NamesrvController namesrvController; + private NamesrvController namesrvController; - private NamesrvConfig namesrvConfig; + private NamesrvConfig namesrvConfig; - private NettyServerConfig nettyServerConfig; + private NettyServerConfig nettyServerConfig; - private Logger logger; + private Logger logger; @Before public void init() throws Exception { @@ -147,7 +148,7 @@ public class DefaultRequestProcessorTest { @Test public void testProcessRequest_RegisterBroker() throws RemotingCommandException, - NoSuchFieldException, IllegalAccessException { + NoSuchFieldException, IllegalAccessException { RemotingCommand request = genSampleRegisterCmd(true); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); @@ -176,7 +177,7 @@ public class DefaultRequestProcessorTest { RemotingCommand request = genSampleRegisterCmd(true); // version >= MQVersion.Version.V3_0_11.ordinal() to register with filter server - request.setVersion(100); + request.setVersion(100); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); when(ctx.channel()).thenReturn(null); @@ -218,10 +219,9 @@ public class DefaultRequestProcessorTest { Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable"); brokerAddrTable.setAccessible(true); - assertThat((Map)brokerAddrTable.get(routes)).isEmpty(); + assertThat((Map) brokerAddrTable.get(routes)).isEmpty(); } - private static RemotingCommand genSampleRegisterCmd(boolean reg) { RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader(); header.setBrokerName("broker"); @@ -235,7 +235,6 @@ public class DefaultRequestProcessorTest { return request; } - private static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); diff --git a/namesrv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/RouteInfoManagerTest.java b/namesrv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/RouteInfoManagerTest.java new file mode 100644 index 00000000..cd6c50c1 --- /dev/null +++ b/namesrv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/RouteInfoManagerTest.java @@ -0,0 +1,103 @@ +/* + * 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.namesrv.routeinfo; + +import io.netty.channel.Channel; +import java.util.ArrayList; +import org.apache.rocketmq.common.namesrv.RegisterBrokerResult; +import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper; +import org.apache.rocketmq.common.protocol.route.TopicRouteData; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class RouteInfoManagerTest { + + private RouteInfoManager routeInfoManager; + + @Before + public void setup() { + routeInfoManager = new RouteInfoManager(); + } + + @Test + public void testGetAllClusterInfo() { + byte[] clusterInfo = routeInfoManager.getAllClusterInfo(); + assertThat(clusterInfo).isNotNull(); + } + + @Test + public void testGetAllTopicList() { + byte[] topicInfo = routeInfoManager.getAllTopicList(); + Assert.assertTrue(topicInfo != null); + assertThat(topicInfo).isNotNull(); + } + + @Test + public void testRegisterBroker() { + TopicConfigSerializeWrapper topicConfigSerializeWrapper = mock(TopicConfigSerializeWrapper.class); + Channel channel = mock(Channel.class); + RegisterBrokerResult registerBrokerResult = routeInfoManager.registerBroker("default-cluster", "127.0.0.1:10911", "default-broker", 1234, "127.0.0.1:1001", + topicConfigSerializeWrapper, new ArrayList(), channel); + assertThat(registerBrokerResult).isNotNull(); + } + + @Test + public void testWipeWritePermOfBrokerByLock() { + int result = routeInfoManager.wipeWritePermOfBrokerByLock("default-broker-name"); + assertThat(result).isEqualTo(0); + } + + @Test + public void testPickupTopicRouteData() { + TopicRouteData result = routeInfoManager.pickupTopicRouteData("unit_test"); + assertThat(result).isNull(); + } + + @Test + public void testGetSystemTopicList() { + byte[] topicList = routeInfoManager.getSystemTopicList(); + assertThat(topicList).isNotNull(); + } + + @Test + public void testGetTopicsByCluster() { + byte[] topicList = routeInfoManager.getTopicsByCluster("default-cluster"); + assertThat(topicList).isNotNull(); + } + + @Test + public void testGetUnitTopics() { + byte[] topicList = routeInfoManager.getUnitTopics(); + assertThat(topicList).isNotNull(); + } + + @Test + public void testGetHasUnitSubTopicList() { + byte[] topicList = routeInfoManager.getHasUnitSubTopicList(); + assertThat(topicList).isNotNull(); + } + + @Test + public void testGetHasUnitSubUnUnitTopicList() { + byte[] topicList = routeInfoManager.getHasUnitSubUnUnitTopicList(); + assertThat(topicList).isNotNull(); + } +} \ No newline at end of file -- GitLab