Skip to content

Commit

Permalink
[ROCKETMQ-254]Fix logger appender unit tests which cost too long
Browse files Browse the repository at this point in the history
Author: lindzh <[email protected]>

Closes apache#141 from lindzh/logger_appender_test.
  • Loading branch information
lindzh authored and lollipopjin committed Aug 11, 2017
1 parent bcc65e5 commit aa1c757
Show file tree
Hide file tree
Showing 25 changed files with 181 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
package org.apache.rocketmq.broker;

import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
import org.apache.rocketmq.store.config.MessageStoreConfig;
import org.junit.After;
import org.junit.Test;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

public class BrokerControllerTest {
Expand All @@ -44,4 +48,9 @@ public void testBrokerRestart() throws Exception {
brokerController.shutdown();
}
}

@After
public void destory(){
UtilAll.deleteFile(new File(new MessageStoreConfig().getStorePathRootDir()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.rocketmq.broker.filter;

import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.filter.ExpressionType;
import org.apache.rocketmq.common.filter.FilterAPI;
import org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData;
Expand Down Expand Up @@ -232,7 +233,7 @@ public void testPersist() {
assertThat(filterData.isDead()).isTrue();
assertThat(filterData.getCompiledExpression()).isNotNull();
} finally {
deleteDirectory("./unit_test");
UtilAll.deleteFile(new File("./unit_test"));
}
}

Expand Down Expand Up @@ -269,23 +270,8 @@ public void testPersist_clean() {

assertThat(topicData).isNullOrEmpty();
} finally {
deleteDirectory("./unit_test");
UtilAll.deleteFile(new File("./unit_test"));
}
}

protected void deleteDirectory(String rootPath) {
File file = new File(rootPath);
deleteFile(file);
}

protected void deleteFile(File file) {
File[] subFiles = file.listFiles();
if (subFiles != null) {
for (File sub : subFiles) {
deleteFile(sub);
}
}

file.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.rocketmq.broker.filter;

import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.filter.ExpressionType;
import org.apache.rocketmq.common.message.MessageDecoder;
import org.apache.rocketmq.common.message.MessageExt;
Expand Down Expand Up @@ -174,22 +175,6 @@ protected List<MessageExtBrokerInner> putMsg(DefaultMessageStore master, int top
return msgs;
}

protected void deleteDirectory(String rootPath) {
File file = new File(rootPath);
deleteFile(file);
}

protected void deleteFile(File file) {
File[] subFiles = file.listFiles();
if (subFiles != null) {
for (File sub : subFiles) {
deleteFile(sub);
}
}

file.delete();
}

protected List<MessageExtBrokerInner> filtered(List<MessageExtBrokerInner> msgs, ConsumerFilterData filterData) {
List<MessageExtBrokerInner> filteredMsgs = new ArrayList<MessageExtBrokerInner>();

Expand Down Expand Up @@ -301,7 +286,7 @@ public void testGetMessage_withFilterBitMapAndConsumerChanged() {
} finally {
master.shutdown();
master.destroy();
deleteDirectory(storePath);
UtilAll.deleteFile(new File(storePath));
}
}

Expand Down Expand Up @@ -386,7 +371,7 @@ public void testGetMessage_withFilterBitMap() {
} finally {
master.shutdown();
master.destroy();
deleteDirectory(storePath);
UtilAll.deleteFile(new File(storePath));
}
}
}
15 changes: 15 additions & 0 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,19 @@ public static byte[] getIP() {
throw new RuntimeException("Can not get local ip", e);
}
}

public static void deleteFile(File file) {
if (!file.exists()) {
return;
}
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
deleteFile(file1);
}
file.delete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.rocketmq.logappender.common;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
Expand All @@ -39,29 +40,34 @@ public class ProducerInstance {

public static final String DEFAULT_GROUP = "rocketmq_appender";

private static ConcurrentHashMap<String, MQProducer> producerMap = new ConcurrentHashMap<String, MQProducer>();
private ConcurrentHashMap<String, MQProducer> producerMap = new ConcurrentHashMap<String, MQProducer>();

private static String genKey(String nameServerAddress, String group) {
private static ProducerInstance instance = new ProducerInstance();

public static ProducerInstance getProducerInstance() {
return instance;
}

private String genKey(String nameServerAddress, String group) {
return nameServerAddress + "_" + group;
}


public static MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
if (group == null) {
public MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
if (StringUtils.isBlank(group)) {
group = DEFAULT_GROUP;
}

String genKey = genKey(nameServerAddress, group);
MQProducer p = producerMap.get(genKey);
MQProducer p = getProducerInstance().producerMap.get(genKey);
if (p != null) {
return p;
}

DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group);
defaultMQProducer.setNamesrvAddr(nameServerAddress);
MQProducer beforeProducer = null;
//cas put producer
beforeProducer = producerMap.putIfAbsent(genKey, defaultMQProducer);
beforeProducer = getProducerInstance().producerMap.putIfAbsent(genKey, defaultMQProducer);
if (beforeProducer != null) {
return beforeProducer;
}
Expand All @@ -70,22 +76,22 @@ public static MQProducer getInstance(String nameServerAddress, String group) thr
}


public static void removeAndClose(String nameServerAddress, String group) {
public void removeAndClose(String nameServerAddress, String group) {
if (group == null) {
group = DEFAULT_GROUP;
}
String genKey = genKey(nameServerAddress, group);
MQProducer producer = producerMap.remove(genKey);
MQProducer producer = getProducerInstance().producerMap.remove(genKey);

if (producer != null) {
producer.shutdown();
}
}

public static void closeAll() {
Set<Map.Entry<String, MQProducer>> entries = producerMap.entrySet();
public void closeAll() {
Set<Map.Entry<String, MQProducer>> entries = getProducerInstance().producerMap.entrySet();
for (Map.Entry<String, MQProducer> entry : entries) {
producerMap.remove(entry.getKey());
getProducerInstance().producerMap.remove(entry.getKey());
entry.getValue().shutdown();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void activateOptions() {
return;
}
try {
producer = ProducerInstance.getInstance(nameServerAddress, producerGroup);
producer = ProducerInstance.getProducerInstance().getInstance(nameServerAddress, producerGroup);
} catch (Exception e) {
LogLog.error("activateOptions nameserver:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public synchronized void close() {
this.closed = true;

try {
ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup);
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
LogLog.error("Closing RocketmqLog4jAppender [" + name + "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Se
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getInstance(this.nameServerAddress, this.producerGroup);
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public void append(LogEvent event) {
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup);
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void start() {
return;
}
try {
producer = ProducerInstance.getInstance(nameServerAddress, producerGroup);
producer = ProducerInstance.getProducerInstance().getInstance(nameServerAddress, producerGroup);
} catch (Exception e) {
addError("Starting RocketmqLogbackAppender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
Expand All @@ -119,7 +119,7 @@ public synchronized void stop() {
this.started = false;

try {
ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup);
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
addError("Closeing RocketmqLogbackAppender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
Expand Down
Loading

0 comments on commit aa1c757

Please sign in to comment.