forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ROCKETMQ-54] Polish unit tests for rocketmq-namesrv
- Loading branch information
1 parent
881aef5
commit 712dec5
Showing
5 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
.../src/test/java/org/apache/rocketmq/namesrv/processor/ClusterTestRequestProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...rv/src/test/java/org/apache/rocketmq/namesrv/routeinfo/BrokerHousekeepingServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters