Skip to content

Commit

Permalink
Complete roketmq unit test (apache#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
githublaohu authored Nov 16, 2022
1 parent b9a5f30 commit e840915
Show file tree
Hide file tree
Showing 22 changed files with 1,476 additions and 181 deletions.
36 changes: 28 additions & 8 deletions dubbo-registry-extensions/dubbo-registry-nameservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,34 @@
<name>dubbo-registry-nameservice</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powermock.version>2.0.9</powermock.version>
<rocketmq.version>4.9.3</rocketmq.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-api</artifactId>
Expand All @@ -50,12 +70,12 @@
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.3</version>
<version>${rocketmq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-tools</artifactId>
<version>4.9.3</version>
<version>${rocketmq.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.registry.nameservice;


Expand All @@ -25,7 +26,6 @@
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.support.FailbackRegistry;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.TopicConfig;
Expand All @@ -41,7 +41,6 @@
import org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl;
import org.apache.rocketmq.tools.admin.MQAdminExt;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -100,26 +99,24 @@ public Thread newThread(Runnable r) {
return new Thread(r, "dubbo-registry-nameservice");
}
});
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
NameServiceRegistry.this.initBeasInfo();

if (consumerRegistryInfoWrapperMap.isEmpty()) {
return;
}
for (Entry<URL, RegistryInfoWrapper> e : consumerRegistryInfoWrapperMap.entrySet()) {
List<URL> urls = new ArrayList<URL>();
NameServiceRegistry.this.pullRoute(e.getValue().serviceName, e.getKey(), urls);
e.getValue().listener.notify(urls);
}
} catch (Exception e) {
String exeptionInfo = String.format("ScheduledTask pullRoute exception , cause %s ", e.getMessage());
logger.error(exeptionInfo, e);
}
scheduledExecutorService.scheduleAtFixedRate(this::run, 1000 * 10, 3000 * 10, TimeUnit.MILLISECONDS);
}

private void run() {
try {
this.initBeasInfo();
if (consumerRegistryInfoWrapperMap.isEmpty()) {
return;
}
for (Entry<URL, RegistryInfoWrapper> e : consumerRegistryInfoWrapperMap.entrySet()) {
List<URL> urls = new ArrayList<URL>();
this.pullRoute(e.getValue().serviceName, e.getKey(), urls);
e.getValue().listener.notify(urls);
}
}, 1000 * 10, 3000 * 10, TimeUnit.MILLISECONDS);
} catch (Exception e) {
String exeptionInfo = String.format("ScheduledTask pullRoute exception , cause %s ", e.getMessage());
logger.error(exeptionInfo, e);
}
}

private void initBeasInfo() throws Exception {
Expand All @@ -145,20 +142,27 @@ private ServiceName createServiceName(URL url) {
}

private void createTopic(ServiceName serviceName) {
if (!this.topicList.getTopicList().contains(serviceName.getValue())) {
try {
TopicConfig topicConfig = new TopicConfig(serviceName.getValue());
topicConfig.setReadQueueNums(2);
topicConfig.setWriteQueueNums(2);
for (Entry<String, BrokerData> entry : clusterInfo.getBrokerAddrTable().entrySet()) {
this.mqAdminExt.createAndUpdateTopicConfig(entry.getValue().selectBrokerAddr(), topicConfig);
if (this.isNotRoute) {
return;
}
if (this.topicList.getTopicList().contains(serviceName.getValue())) {
return;
}
try {
TopicConfig topicConfig = new TopicConfig(serviceName.getValue());
topicConfig.setReadQueueNums(2);
topicConfig.setWriteQueueNums(2);
for (Entry<String, BrokerData> entry : clusterInfo.getBrokerAddrTable().entrySet()) {
for (String brokerAddr : entry.getValue().getBrokerAddrs().values()) {
this.mqAdminExt.createAndUpdateTopicConfig(brokerAddr, topicConfig);
}
} catch (Exception e) {
String exceptionInfo = String.format("create topic fial, topic name is %s , cause %s", serviceName.getValue(), e.getMessage());
logger.error(exceptionInfo, e);
throw new RuntimeException(exceptionInfo, e);
}
} catch (Exception e) {
String exceptionInfo = String.format("create topic fial, topic name is %s , cause %s", serviceName.getValue(), e.getMessage());
logger.error(exceptionInfo, e);
throw new RuntimeException(exceptionInfo, e);
}

}

@Override
Expand All @@ -179,7 +183,7 @@ public void doUnregister(URL url) {
@Override
public void doSubscribe(URL url, NotifyListener listener) {
if (Objects.equals(url.getCategory(),
org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY)) {
org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY)) {
return;
}
ServiceName serviceName = this.createServiceName(url);
Expand All @@ -189,7 +193,8 @@ public void doSubscribe(URL url, NotifyListener listener) {
return;
}
} catch (InterruptedException | MQBrokerException | RemotingException | MQClientException e) {
String exceptionInfo = String.format("query topic consume fial, topic name is %s , url is %s , cause %s", serviceName.getValue(), url, e.getMessage());
String exceptionInfo =
String.format("query topic consume fial, topic name is %s , url is %s , cause %s", serviceName.getValue(), url, e.getMessage());
logger.error(exceptionInfo, e);
throw new RuntimeException(exceptionInfo, e);
}
Expand Down Expand Up @@ -221,7 +226,8 @@ void pullRoute(ServiceName serviceName, URL url, List<URL> urls) {
}
}
} catch (Exception e) {
String exceptionInfo = String.format("query topic route fial, topic name is %s , url is %s , cause %s", serviceName.getValue(), url, e.getMessage());
String exceptionInfo =
String.format("query topic route fial, topic name is %s , url is %s , cause %s", serviceName.getValue(), url, e.getMessage());
logger.error(exceptionInfo, e);
throw new RuntimeException(exceptionInfo, e);
}
Expand All @@ -235,7 +241,9 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
private class RegistryInfoWrapper {

private NotifyListener listener;

private ServiceName serviceName;

public RegistryInfoWrapper() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.registry.nameservice;

import org.apache.dubbo.common.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.registry.nameservice;

import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
Expand All @@ -23,13 +24,13 @@
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY;
import static org.apache.dubbo.common.utils.StringUtils.isBlank;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;

import java.util.Arrays;
import java.util.Objects;
import java.util.zip.CRC32;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;

public class ServiceName {

public static final String DEFAULT_PARAM_VALUE = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.dubbo.registry.nameservice;

import org.apache.dubbo.registry.Registry;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(value = {NameServiceRegistryFactory.class})
public class NameServiceRegistryFactoryTest {

@Mock
private NameServiceRegistry registry;

@Test
public void createRegistryTest() throws Exception {

PowerMockito.whenNew(NameServiceRegistry.class).withAnyArguments().thenReturn(registry);
NameServiceRegistryFactory registryFactory = new NameServiceRegistryFactory();
Registry registry = registryFactory.createRegistry(null);
Assert.assertEquals(registry, this.registry);
}
}
Loading

0 comments on commit e840915

Please sign in to comment.