/**
*
* Copyright 2004 Protique Ltd
*
* Licensed 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.activemq.service;
import javax.jms.DeliveryMode;
import junit.framework.TestCase;
import org.activemq.broker.BrokerClientStub;
import org.activemq.io.util.MemoryBoundedObjectManager;
import org.activemq.io.util.MemoryBoundedQueueManager;
import org.activemq.message.ActiveMQMessage;
import org.activemq.message.ActiveMQQueue;
import org.activemq.service.boundedvm.TransientQueueBoundedMessageManager;
/**
* MemoryBoundedQueueTest
*
* @version $Revision: 1.1.1.1 $
*/
public class TransientQueueGarbageCollectionTest extends TestCase {
private static final int MESSAGE_SIZE = 1024;
private MemoryBoundedObjectManager memoryManager;
private MemoryBoundedQueueManager queueManager;
private TransientQueueBoundedMessageManager transientQueueMCM;
protected BrokerClientStub client;
public void testGCNoSubscribers() throws Exception {
ActiveMQMessage msg = createMessage();
runGCTest(msg);
}
public void testGCExpiredMessage() throws Exception {
ActiveMQMessage msg = createMessage();
msg.setJMSExpiration(1);
Thread.sleep(2); // make sure we wait for the message to expire
runGCTest(msg);
}
protected String getDestination() {
return getClass().getName() + "." + getName();
}
protected ActiveMQMessage createMessage() {
ActiveMQMessage msg = new ActiveMQMessage();
msg.setJMSDestination(new ActiveMQQueue(getDestination()));
msg.setMemoryUsage(MESSAGE_SIZE);
msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
return msg;
}
protected void runGCTest(ActiveMQMessage msg) throws Exception {
// now lets dispatch a message
System.out.println("Queue capacity before: " + this.queueManager.getCurrentCapacity());
this.transientQueueMCM.sendMessage(this.client, msg);
System.out.println("Queue capacity after: " + this.queueManager.getCurrentCapacity());
assertTrue("Memory must be full by now", this.memoryManager.isFull());
assertTrue("Total memory used must be equal to " + MESSAGE_SIZE, this.memoryManager.getTotalMemoryUsedSize() >= MESSAGE_SIZE);
// allow some time for garbage collection
Thread.sleep(2000);
assertTrue("Memory must have been freed by now", this.memoryManager.getTotalMemoryUsedSize() == 0);
}
public void setUp() throws Exception {
// setup memory space for just 1 message
this.memoryManager = new MemoryBoundedObjectManager("testmanager", MESSAGE_SIZE);
assertEquals("Total memory used must be zero", 0, this.memoryManager.getTotalMemoryUsedSize());
this.queueManager = new MemoryBoundedQueueManager(this.memoryManager);
//this.queue = this.queueManager.getMemoryBoundedQueue(QUEUE_NAME);
this.transientQueueMCM = new TransientQueueBoundedMessageManager(this.queueManager,null,null);
this.transientQueueMCM.start();
this.client = new BrokerClientStub();
}
public void tearDown() throws Exception {
this.transientQueueMCM.stop();
super.tearDown();
}
}