test to show that: https://issues.apache.org/jira/browse/AMQ-4677 is not an issue. Verifies that the LevelDB logs are cleared as messages are consumed.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1514502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2013-08-15 21:24:17 +00:00
parent 894d988c49
commit 717345f499
2 changed files with 183 additions and 0 deletions

View File

@ -746,6 +746,7 @@
<exclude>org/apache/activemq/bugs/AMQ3732Test.*</exclude>
<exclude>org/apache/activemq/bugs/AMQ3841Test.*</exclude>
<exclude>org/apache/activemq/bugs/AMQ4221Test.*</exclude>
<exclude>org/apache/activemq/bugs/AMQ4677Test.*</exclude>
<exclude>org/apache/activemq/bugs/ActiveMQSlowConsumerManualTest.*</exclude>
<exclude>org/apache/activemq/bugs/ConnectionPerMessageTest.*</exclude>
<exclude>org/apache/activemq/bugs/CraigsBugTest.*</exclude>

View File

@ -0,0 +1,182 @@
/**
* 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.activemq.bugs;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.jms.*;
import javax.management.ObjectName;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.leveldb.LevelDBStore;
import org.apache.activemq.leveldb.LevelDBStoreViewMBean;
import org.apache.activemq.util.Wait;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQ4677Test {
private static final transient Logger LOG = LoggerFactory.getLogger(AMQ4677Test.class);
private static BrokerService brokerService;
@Rule public TestName name = new TestName();
private File dataDirFile;
@Before
public void setUp() throws Exception {
dataDirFile = new File("target/LevelDBCleanupTest");
brokerService = new BrokerService();
brokerService.setBrokerName("LevelDBBroker");
brokerService.setPersistent(true);
brokerService.setUseJmx(true);
brokerService.setAdvisorySupport(false);
brokerService.setDeleteAllMessagesOnStartup(true);
brokerService.setDataDirectoryFile(dataDirFile);
LevelDBStore persistenceFactory = new LevelDBStore();
persistenceFactory.setDirectory(dataDirFile);
brokerService.setPersistenceAdapter(persistenceFactory);
brokerService.start();
brokerService.waitUntilStarted();
}
@After
public void tearDown() throws Exception {
brokerService.stop();
brokerService.waitUntilStopped();
}
@Test
public void testSendAndReceiveAllMessages() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://LevelDBBroker");
Connection connection = connectionFactory.createConnection();
connection.setClientID(getClass().getName());
connection.start();
final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(name.toString());
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
LevelDBStoreViewMBean levelDBView = getLevelDBStoreMBean();
assertNotNull(levelDBView);
levelDBView.compact();
final int SIZE = 6 * 1024 * 5;
final int MSG_COUNT = 60000;
final CountDownLatch done = new CountDownLatch(MSG_COUNT);
byte buffer[] = new byte[SIZE];
for (int i = 0; i < SIZE; ++i) {
buffer[i] = (byte) 128;
}
for (int i = 0; i < MSG_COUNT; ++i) {
BytesMessage message = session.createBytesMessage();
message.writeBytes(buffer);
producer.send(message);
if ((i % 1000) == 0) {
LOG.info("Sent message #{}", i);
session.commit();
}
}
session.commit();
LOG.info("Finished sending all messages.");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if ((done.getCount() % 1000) == 0) {
try {
LOG.info("Received message #{}", MSG_COUNT - done.getCount());
session.commit();
} catch (JMSException e) {
}
}
done.countDown();
}
});
done.await(10, TimeUnit.MINUTES);
session.commit();
LOG.info("Finished receiving all messages.");
LOG.info("Current number of logs {}", countLogFiles());
assertTrue("Should only have one log file left.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return countLogFiles() == 1;
}
}, TimeUnit.MINUTES.toMillis(5)));
levelDBView.compact();
LOG.info("Current number of logs {}", countLogFiles());
}
protected long countLogFiles() {
String[] logFiles = dataDirFile.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith("log")) {
return true;
}
return false;
}
});
return logFiles.length;
}
protected LevelDBStoreViewMBean getLevelDBStoreMBean() throws Exception {
ObjectName levelDbViewMBeanQuery = new ObjectName(
"org.apache.activemq:type=Broker,brokerName=LevelDBBroker,Service=PersistenceAdapter,InstanceName=LevelDB*");
Set<ObjectName> names = brokerService.getManagementContext().queryNames(null, levelDbViewMBeanQuery);
if (names.isEmpty() || names.size() > 1) {
throw new java.lang.IllegalStateException("Can't find levelDB store name.");
}
LevelDBStoreViewMBean proxy = (LevelDBStoreViewMBean) brokerService.getManagementContext()
.newProxyInstance(names.iterator().next(), LevelDBStoreViewMBean.class, true);
return proxy;
}
}