Skip to content

Commit 712dec5

Browse files
stevenschewzhouxinyu
authored andcommitted
[ROCKETMQ-54] Polish unit tests for rocketmq-namesrv
1 parent 881aef5 commit 712dec5

File tree

5 files changed

+195
-7
lines changed

5 files changed

+195
-7
lines changed

namesrv/src/test/java/org/apache/rocketmq/namesrv/NamesrvControllerTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import static org.assertj.core.api.Assertions.assertThat;
2424

2525
public class NamesrvControllerTest {
26-
private final int RESTART_NUM = 2;
26+
private final static int RESTARTNUM = 2;
2727

2828
/**
2929
* Tests if the controller can be properly stopped and started.
@@ -32,7 +32,7 @@ public class NamesrvControllerTest {
3232
*/
3333
@Test
3434
public void testRestart() throws Exception {
35-
for (int i = 0; i < RESTART_NUM; i++) {
35+
for (int i = 0; i < RESTARTNUM; i++) {
3636
NamesrvController namesrvController = new NamesrvController(
3737
new NamesrvConfig(),
3838
new NettyServerConfig()
@@ -43,5 +43,4 @@ public void testRestart() throws Exception {
4343
namesrvController.shutdown();
4444
}
4545
}
46-
4746
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.rocketmq.namesrv.processor;
19+
20+
import io.netty.channel.ChannelHandlerContext;
21+
import java.lang.reflect.Field;
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import org.apache.rocketmq.client.ClientConfig;
26+
import org.apache.rocketmq.client.exception.MQClientException;
27+
import org.apache.rocketmq.client.impl.MQClientAPIImpl;
28+
import org.apache.rocketmq.client.impl.MQClientManager;
29+
import org.apache.rocketmq.client.impl.factory.MQClientInstance;
30+
import org.apache.rocketmq.common.namesrv.NamesrvConfig;
31+
import org.apache.rocketmq.common.protocol.ResponseCode;
32+
import org.apache.rocketmq.common.protocol.route.BrokerData;
33+
import org.apache.rocketmq.common.protocol.route.TopicRouteData;
34+
import org.apache.rocketmq.namesrv.NamesrvController;
35+
import org.apache.rocketmq.remoting.CommandCustomHeader;
36+
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
37+
import org.apache.rocketmq.remoting.exception.RemotingException;
38+
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
39+
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
40+
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
41+
import org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl;
42+
import org.junit.After;
43+
import org.junit.Before;
44+
import org.junit.Test;
45+
46+
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.mockito.ArgumentMatchers.anyLong;
48+
import static org.mockito.ArgumentMatchers.anyString;
49+
import static org.mockito.Mockito.mock;
50+
import static org.mockito.Mockito.when;
51+
52+
public class ClusterTestRequestProcessorTest {
53+
private ClusterTestRequestProcessor clusterTestProcessor;
54+
private DefaultMQAdminExtImpl defaultMQAdminExtImpl;
55+
private MQClientInstance mqClientInstance = MQClientManager.getInstance().getAndCreateMQClientInstance(new ClientConfig());
56+
private MQClientAPIImpl mQClientAPIImpl;
57+
private ChannelHandlerContext ctx;
58+
59+
@Before
60+
public void init() throws NoSuchFieldException, IllegalAccessException, RemotingException, MQClientException, InterruptedException {
61+
NamesrvController namesrvController = new NamesrvController(
62+
new NamesrvConfig(),
63+
new NettyServerConfig()
64+
);
65+
66+
clusterTestProcessor = new ClusterTestRequestProcessor(namesrvController, "default-producer");
67+
mQClientAPIImpl = mock(MQClientAPIImpl.class);
68+
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
69+
defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);
70+
ctx = mock(ChannelHandlerContext.class);
71+
72+
Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
73+
field.setAccessible(true);
74+
field.set(defaultMQAdminExtImpl, mqClientInstance);
75+
field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
76+
field.setAccessible(true);
77+
field.set(mqClientInstance, mQClientAPIImpl);
78+
field = ClusterTestRequestProcessor.class.getDeclaredField("adminExt");
79+
field.setAccessible(true);
80+
field.set(clusterTestProcessor, defaultMQAdminExt);
81+
82+
TopicRouteData topicRouteData = new TopicRouteData();
83+
List<BrokerData> brokerDatas = new ArrayList<>();
84+
HashMap<Long, String> brokerAddrs = new HashMap<>();
85+
brokerAddrs.put(1234l, "127.0.0.1:10911");
86+
BrokerData brokerData = new BrokerData();
87+
brokerData.setCluster("default-cluster");
88+
brokerData.setBrokerName("default-broker");
89+
brokerData.setBrokerAddrs(brokerAddrs);
90+
brokerDatas.add(brokerData);
91+
topicRouteData.setBrokerDatas(brokerDatas);
92+
when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);
93+
}
94+
95+
@After
96+
public void terminate() {
97+
}
98+
99+
@Test
100+
public void testGetRouteInfoByTopic() throws RemotingCommandException {
101+
RemotingCommand request = RemotingCommand.createRequestCommand(12, new CommandCustomHeader() {
102+
@Override public void checkFields() throws RemotingCommandException {
103+
104+
}
105+
});
106+
RemotingCommand remoting = clusterTestProcessor.getRouteInfoByTopic(ctx, request);
107+
assertThat(remoting.getCode()).isEqualTo(ResponseCode.TOPIC_NOT_EXIST);
108+
assertThat(remoting.getBody()).isNull();
109+
assertThat(remoting.getRemark()).isNotNull();
110+
}
111+
112+
}

namesrv/src/test/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessorTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import static org.mockito.Mockito.when;
4646

4747
public class DefaultRequestProcessorTest {
48-
/** Test Target */
4948
private DefaultRequestProcessor defaultRequestProcessor;
5049

5150
private NamesrvController namesrvController;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.rocketmq.namesrv.routeinfo;
19+
20+
import org.apache.rocketmq.common.namesrv.NamesrvConfig;
21+
import org.apache.rocketmq.namesrv.NamesrvController;
22+
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
23+
import org.junit.AfterClass;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
27+
public class BrokerHousekeepingServiceTest {
28+
private static BrokerHousekeepingService brokerHousekeepingService;
29+
30+
@BeforeClass
31+
public static void setup() {
32+
NamesrvController namesrvController = new NamesrvController(
33+
new NamesrvConfig(),
34+
new NettyServerConfig()
35+
);
36+
brokerHousekeepingService = new BrokerHousekeepingService(namesrvController);
37+
}
38+
39+
@AfterClass
40+
public static void terminate() {
41+
42+
}
43+
44+
@Test
45+
public void testOnChannelClose() {
46+
brokerHousekeepingService.onChannelClose("127.0.0.1:9876", null);
47+
}
48+
49+
@Test
50+
public void testOnChannelException() {
51+
brokerHousekeepingService.onChannelException("127.0.0.1:9876", null);
52+
}
53+
54+
@Test
55+
public void testOnChannelIdle() {
56+
brokerHousekeepingService.onChannelException("127.0.0.1:9876", null);
57+
}
58+
59+
}

namesrv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/RouteInfoManagerTest.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import io.netty.channel.Channel;
2020
import java.util.ArrayList;
21+
import java.util.concurrent.ConcurrentHashMap;
22+
import org.apache.rocketmq.common.TopicConfig;
2123
import org.apache.rocketmq.common.namesrv.RegisterBrokerResult;
2224
import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper;
2325
import org.apache.rocketmq.common.protocol.route.TopicRouteData;
26+
import org.junit.After;
2427
import org.junit.Assert;
2528
import org.junit.Before;
2629
import org.junit.Test;
@@ -30,11 +33,18 @@
3033

3134
public class RouteInfoManagerTest {
3235

33-
private RouteInfoManager routeInfoManager;
36+
private static RouteInfoManager routeInfoManager;
3437

3538
@Before
3639
public void setup() {
3740
routeInfoManager = new RouteInfoManager();
41+
testRegisterBroker();
42+
}
43+
44+
@After
45+
public void terminate() {
46+
routeInfoManager.printAllPeriodically();
47+
routeInfoManager.unregisterBroker("default-cluster", "127.0.0.1:10911", "default-broker", 1234);
3848
}
3949

4050
@Test
@@ -52,7 +62,16 @@ public void testGetAllTopicList() {
5262

5363
@Test
5464
public void testRegisterBroker() {
55-
TopicConfigSerializeWrapper topicConfigSerializeWrapper = mock(TopicConfigSerializeWrapper.class);
65+
TopicConfigSerializeWrapper topicConfigSerializeWrapper = new TopicConfigSerializeWrapper();
66+
ConcurrentHashMap<String, TopicConfig> topicConfigConcurrentHashMap = new ConcurrentHashMap<>();
67+
TopicConfig topicConfig = new TopicConfig();
68+
topicConfig.setWriteQueueNums(8);
69+
topicConfig.setTopicName("unit-test");
70+
topicConfig.setPerm(6);
71+
topicConfig.setReadQueueNums(8);
72+
topicConfig.setOrder(false);
73+
topicConfigConcurrentHashMap.put("unit-test", topicConfig);
74+
topicConfigSerializeWrapper.setTopicConfigTable(topicConfigConcurrentHashMap);
5675
Channel channel = mock(Channel.class);
5776
RegisterBrokerResult registerBrokerResult = routeInfoManager.registerBroker("default-cluster", "127.0.0.1:10911", "default-broker", 1234, "127.0.0.1:1001",
5877
topicConfigSerializeWrapper, new ArrayList<String>(), channel);
@@ -61,7 +80,7 @@ public void testRegisterBroker() {
6180

6281
@Test
6382
public void testWipeWritePermOfBrokerByLock() {
64-
int result = routeInfoManager.wipeWritePermOfBrokerByLock("default-broker-name");
83+
int result = routeInfoManager.wipeWritePermOfBrokerByLock("default-broker");
6584
assertThat(result).isEqualTo(0);
6685
}
6786

0 commit comments

Comments
 (0)