mirror of
https://github.com/apache/rocketmq.git
synced 2026-10-30 18:44:38 +08:00
[ROCKETMQ-54] Polish unit tests for rocketmq-namesrv
This commit is contained in:
@@ -23,7 +23,7 @@ import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NamesrvControllerTest {
|
||||
private final int RESTART_NUM = 2;
|
||||
private final static int RESTARTNUM = 2;
|
||||
|
||||
/**
|
||||
* Tests if the controller can be properly stopped and started.
|
||||
@@ -32,7 +32,7 @@ public class NamesrvControllerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRestart() throws Exception {
|
||||
for (int i = 0; i < RESTART_NUM; i++) {
|
||||
for (int i = 0; i < RESTARTNUM; i++) {
|
||||
NamesrvController namesrvController = new NamesrvController(
|
||||
new NamesrvConfig(),
|
||||
new NettyServerConfig()
|
||||
@@ -43,5 +43,4 @@ public class NamesrvControllerTest {
|
||||
namesrvController.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.processor;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.client.ClientConfig;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.client.impl.MQClientAPIImpl;
|
||||
import org.apache.rocketmq.client.impl.MQClientManager;
|
||||
import org.apache.rocketmq.client.impl.factory.MQClientInstance;
|
||||
import org.apache.rocketmq.common.namesrv.NamesrvConfig;
|
||||
import org.apache.rocketmq.common.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.common.protocol.route.BrokerData;
|
||||
import org.apache.rocketmq.common.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.namesrv.NamesrvController;
|
||||
import org.apache.rocketmq.remoting.CommandCustomHeader;
|
||||
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
|
||||
import org.apache.rocketmq.remoting.exception.RemotingException;
|
||||
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
|
||||
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
|
||||
import org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ClusterTestRequestProcessorTest {
|
||||
private ClusterTestRequestProcessor clusterTestProcessor;
|
||||
private DefaultMQAdminExtImpl defaultMQAdminExtImpl;
|
||||
private MQClientInstance mqClientInstance = MQClientManager.getInstance().getAndCreateMQClientInstance(new ClientConfig());
|
||||
private MQClientAPIImpl mQClientAPIImpl;
|
||||
private ChannelHandlerContext ctx;
|
||||
|
||||
@Before
|
||||
public void init() throws NoSuchFieldException, IllegalAccessException, RemotingException, MQClientException, InterruptedException {
|
||||
NamesrvController namesrvController = new NamesrvController(
|
||||
new NamesrvConfig(),
|
||||
new NettyServerConfig()
|
||||
);
|
||||
|
||||
clusterTestProcessor = new ClusterTestRequestProcessor(namesrvController, "default-producer");
|
||||
mQClientAPIImpl = mock(MQClientAPIImpl.class);
|
||||
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
|
||||
defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);
|
||||
ctx = mock(ChannelHandlerContext.class);
|
||||
|
||||
Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
|
||||
field.setAccessible(true);
|
||||
field.set(defaultMQAdminExtImpl, mqClientInstance);
|
||||
field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
|
||||
field.setAccessible(true);
|
||||
field.set(mqClientInstance, mQClientAPIImpl);
|
||||
field = ClusterTestRequestProcessor.class.getDeclaredField("adminExt");
|
||||
field.setAccessible(true);
|
||||
field.set(clusterTestProcessor, defaultMQAdminExt);
|
||||
|
||||
TopicRouteData topicRouteData = new TopicRouteData();
|
||||
List<BrokerData> brokerDatas = new ArrayList<>();
|
||||
HashMap<Long, String> brokerAddrs = new HashMap<>();
|
||||
brokerAddrs.put(1234l, "127.0.0.1:10911");
|
||||
BrokerData brokerData = new BrokerData();
|
||||
brokerData.setCluster("default-cluster");
|
||||
brokerData.setBrokerName("default-broker");
|
||||
brokerData.setBrokerAddrs(brokerAddrs);
|
||||
brokerDatas.add(brokerData);
|
||||
topicRouteData.setBrokerDatas(brokerDatas);
|
||||
when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);
|
||||
}
|
||||
|
||||
@After
|
||||
public void terminate() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRouteInfoByTopic() throws RemotingCommandException {
|
||||
RemotingCommand request = RemotingCommand.createRequestCommand(12, new CommandCustomHeader() {
|
||||
@Override public void checkFields() throws RemotingCommandException {
|
||||
|
||||
}
|
||||
});
|
||||
RemotingCommand remoting = clusterTestProcessor.getRouteInfoByTopic(ctx, request);
|
||||
assertThat(remoting.getCode()).isEqualTo(ResponseCode.TOPIC_NOT_EXIST);
|
||||
assertThat(remoting.getBody()).isNull();
|
||||
assertThat(remoting.getRemark()).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
-1
@@ -45,7 +45,6 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class DefaultRequestProcessorTest {
|
||||
/** Test Target */
|
||||
private DefaultRequestProcessor defaultRequestProcessor;
|
||||
|
||||
private NamesrvController namesrvController;
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 org.apache.rocketmq.common.namesrv.NamesrvConfig;
|
||||
import org.apache.rocketmq.namesrv.NamesrvController;
|
||||
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BrokerHousekeepingServiceTest {
|
||||
private static BrokerHousekeepingService brokerHousekeepingService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
NamesrvController namesrvController = new NamesrvController(
|
||||
new NamesrvConfig(),
|
||||
new NettyServerConfig()
|
||||
);
|
||||
brokerHousekeepingService = new BrokerHousekeepingService(namesrvController);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void terminate() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnChannelClose() {
|
||||
brokerHousekeepingService.onChannelClose("127.0.0.1:9876", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnChannelException() {
|
||||
brokerHousekeepingService.onChannelException("127.0.0.1:9876", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnChannelIdle() {
|
||||
brokerHousekeepingService.onChannelException("127.0.0.1:9876", null);
|
||||
}
|
||||
|
||||
}
|
||||
+22
-3
@@ -18,9 +18,12 @@ package org.apache.rocketmq.namesrv.routeinfo;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.rocketmq.common.TopicConfig;
|
||||
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.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -30,11 +33,18 @@ import static org.mockito.Mockito.mock;
|
||||
|
||||
public class RouteInfoManagerTest {
|
||||
|
||||
private RouteInfoManager routeInfoManager;
|
||||
private static RouteInfoManager routeInfoManager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
routeInfoManager = new RouteInfoManager();
|
||||
testRegisterBroker();
|
||||
}
|
||||
|
||||
@After
|
||||
public void terminate() {
|
||||
routeInfoManager.printAllPeriodically();
|
||||
routeInfoManager.unregisterBroker("default-cluster", "127.0.0.1:10911", "default-broker", 1234);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,7 +62,16 @@ public class RouteInfoManagerTest {
|
||||
|
||||
@Test
|
||||
public void testRegisterBroker() {
|
||||
TopicConfigSerializeWrapper topicConfigSerializeWrapper = mock(TopicConfigSerializeWrapper.class);
|
||||
TopicConfigSerializeWrapper topicConfigSerializeWrapper = new TopicConfigSerializeWrapper();
|
||||
ConcurrentHashMap<String, TopicConfig> topicConfigConcurrentHashMap = new ConcurrentHashMap<>();
|
||||
TopicConfig topicConfig = new TopicConfig();
|
||||
topicConfig.setWriteQueueNums(8);
|
||||
topicConfig.setTopicName("unit-test");
|
||||
topicConfig.setPerm(6);
|
||||
topicConfig.setReadQueueNums(8);
|
||||
topicConfig.setOrder(false);
|
||||
topicConfigConcurrentHashMap.put("unit-test", topicConfig);
|
||||
topicConfigSerializeWrapper.setTopicConfigTable(topicConfigConcurrentHashMap);
|
||||
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);
|
||||
@@ -61,7 +80,7 @@ public class RouteInfoManagerTest {
|
||||
|
||||
@Test
|
||||
public void testWipeWritePermOfBrokerByLock() {
|
||||
int result = routeInfoManager.wipeWritePermOfBrokerByLock("default-broker-name");
|
||||
int result = routeInfoManager.wipeWritePermOfBrokerByLock("default-broker");
|
||||
assertThat(result).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user