[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/memory ...
Ben Wang
bwang at jboss.com
Mon Jul 17 10:41:28 EDT 2006
User: bwang
Date: 06/07/17 10:41:28
Added: tests-50/functional/org/jboss/cache/pojo/memory
ReplicatedTest.java
Removed: tests-50/functional/org/jboss/cache/pojo/memory
ReplicatedAopTest.java
Log:
Name changes
Revision Changes Path
1.1 date: 2006/07/17 14:41:28; author: bwang; state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/memory/ReplicatedTest.java
Index: ReplicatedTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.memory;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.TestingUtil;
import org.jboss.cache.pojo.test.Address;
import org.jboss.cache.pojo.test.Person;
import org.jboss.cache.pojo.test.SerializedAddress;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
/**
* @author Ben Wang
*/
public class ReplicatedTest extends TestCase
{
Log log_ = LogFactory.getLog(ReplicatedTest.class);
PojoCache cache_;
PojoCache cache1_;
public ReplicatedTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
String configFile = "META-INF/replSync-service.xml";
cache_ = PojoCacheFactory.createInstance(configFile);
cache_.start();
cache1_ = PojoCacheFactory.createInstance(configFile);
cache1_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
cache1_.stop();
}
// public void testDummy() {}
/**
* Test replication with classloaders.
*
* @throws Exception
*/
public void testCLLeakageBasic() throws Exception
{
SerializedAddress add = new SerializedAddress();
add.setCity("Taipei");
ClassLoader cla = getClassLoader();
WeakReference refa = new WeakReference(cla);
cache_.getCache().registerClassLoader("/aop", cla);
ClassLoader clb = getClassLoader();
WeakReference refb = new WeakReference(clb);
cache1_.getCache().registerClassLoader("/aop", clb);
cache_.getCache().put("/aop", "add", add);
TestingUtil.sleepThread(100);
try
{
Object ben = cache1_.getCache().get("/aop", "add");
assertEquals(add.toString(), ben.toString());
ben = null;
} catch (Exception ex)
{
fail("Test fails with exception " + ex);
}
cache_.getCache().remove("/aop", "add");
ClassLoader clc = getClassLoader();
cla = null;
clb = null;
cache_.getCache().registerClassLoader("/aop", clc); // replace cla
cache1_.getCache().registerClassLoader("/aop", clc); // replace cla
System.gc(); // force gc
Thread.currentThread().sleep(1000);
assertNull("Classloader should be gced ", refa.get());
assertNull("Classloader should be gced ", refb.get());
}
private static void forceOutOfMemoryError() throws Exception
{
ArrayList list = new ArrayList();
try
{
long i = 0;
while (true)
{
list.add("BigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBig" + (i++));
}
}
catch (Throwable ignored)
{
}
list.clear();
list = null;
System.gc();
Thread.currentThread().sleep(1000);
}
/**
* Test replication with classloaders.
*
* @throws Exception
*/
public void testCLLeakage() throws Exception
{
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
ClassLoader cla = getClassLoader();
WeakReference refa = new WeakReference(cla);
cache_.getCache().registerClassLoader("/aop", cla);
ClassLoader clb = getClassLoader();
cache1_.getCache().registerClassLoader("/aop", clb);
WeakReference refb = new WeakReference(clb);
cache_.attach("/aop", p);
TestingUtil.sleepThread(100);
try
{
Object ben = cache1_.find("/aop");
assertEquals(p.toString(), ben.toString());
ben = null;
} catch (Exception ex)
{
fail("Test fails with exception " + ex);
}
cache_.detach("/aop");
ClassLoader clc = getClassLoader();
cache_.getCache().registerClassLoader("/aop", clc); // replace cla
cache1_.getCache().registerClassLoader("/aop", clc); // replace cla
cla = null;
clb = null;
forceOutOfMemoryError();
assertNull("Classloader should be gced ", refa.get());
assertNull("Classloader should be gced ", refb.get());
}
protected ClassLoader getClassLoader() throws Exception
{
String[] includesClasses = {"org.jboss.cache.aop.test.Person",
"org.jboss.cache.aop.test.Address"};
String [] excludesClasses = {};
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(ReplicatedTest.suite());
}
}
More information about the jboss-cvs-commits
mailing list