提交 e447bd01 编写于 作者: S stevenschew 提交者: yukon

[ROCKETMQ-54] Add unit tests for rocketmq-namesrv

上级 11ff542c
/*
* 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.
}
}
/*
* 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
/*
* 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
/*
* 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<String, HashMap<String, String>> result = new HashMap<>();
HashMap<String, String> 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
...@@ -41,19 +41,20 @@ import org.junit.Test; ...@@ -41,19 +41,20 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import static org.assertj.core.api.Assertions.assertThat; 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 { public class DefaultRequestProcessorTest {
/** Test Target */ /** Test Target */
private DefaultRequestProcessor defaultRequestProcessor; 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 @Before
public void init() throws Exception { public void init() throws Exception {
...@@ -147,7 +148,7 @@ public class DefaultRequestProcessorTest { ...@@ -147,7 +148,7 @@ public class DefaultRequestProcessorTest {
@Test @Test
public void testProcessRequest_RegisterBroker() throws RemotingCommandException, public void testProcessRequest_RegisterBroker() throws RemotingCommandException,
NoSuchFieldException, IllegalAccessException { NoSuchFieldException, IllegalAccessException {
RemotingCommand request = genSampleRegisterCmd(true); RemotingCommand request = genSampleRegisterCmd(true);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
...@@ -176,7 +177,7 @@ public class DefaultRequestProcessorTest { ...@@ -176,7 +177,7 @@ public class DefaultRequestProcessorTest {
RemotingCommand request = genSampleRegisterCmd(true); RemotingCommand request = genSampleRegisterCmd(true);
// version >= MQVersion.Version.V3_0_11.ordinal() to register with filter server // version >= MQVersion.Version.V3_0_11.ordinal() to register with filter server
request.setVersion(100); request.setVersion(100);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.channel()).thenReturn(null); when(ctx.channel()).thenReturn(null);
...@@ -218,10 +219,9 @@ public class DefaultRequestProcessorTest { ...@@ -218,10 +219,9 @@ public class DefaultRequestProcessorTest {
Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable"); Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable");
brokerAddrTable.setAccessible(true); brokerAddrTable.setAccessible(true);
assertThat((Map)brokerAddrTable.get(routes)).isEmpty(); assertThat((Map) brokerAddrTable.get(routes)).isEmpty();
} }
private static RemotingCommand genSampleRegisterCmd(boolean reg) { private static RemotingCommand genSampleRegisterCmd(boolean reg) {
RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader(); RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
header.setBrokerName("broker"); header.setBrokerName("broker");
...@@ -235,7 +235,6 @@ public class DefaultRequestProcessorTest { ...@@ -235,7 +235,6 @@ public class DefaultRequestProcessorTest {
return request; return request;
} }
private static void setFinalStatic(Field field, Object newValue) throws Exception { private static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true); field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = Field.class.getDeclaredField("modifiers");
......
/*
* 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<String>(), 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册