1 7 8 package org.jboss.cache.pojo.memory; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.PojoCache; 16 import org.jboss.cache.pojo.PojoCacheFactory; 17 import org.jboss.cache.pojo.TestingUtil; 18 import org.jboss.cache.pojo.test.Address; 19 import org.jboss.cache.pojo.test.Person; 20 import org.jboss.cache.pojo.test.SerializedAddress; 21 import org.jboss.cache.Fqn; 22 23 import java.lang.ref.WeakReference ; 24 import java.util.ArrayList ; 25 26 29 30 public class ReplicatedTest extends TestCase 31 { 32 Log log_ = LogFactory.getLog(ReplicatedTest.class); 33 PojoCache cache_; 34 PojoCache cache1_; 35 36 public ReplicatedTest(String name) 37 { 38 super(name); 39 } 40 41 protected void setUp() throws Exception 42 { 43 super.setUp(); 44 String configFile = "META-INF/replSync-service.xml"; 45 boolean toStart = false; 46 cache_ = PojoCacheFactory.createCache(configFile, toStart); 47 cache_.start(); 48 cache1_ = PojoCacheFactory.createCache(configFile, toStart); 49 cache1_.start(); 50 } 51 52 protected void tearDown() throws Exception 53 { 54 super.tearDown(); 55 cache_.stop(); 56 cache1_.stop(); 57 } 58 59 61 66 public void testCLLeakageBasic() throws Exception 67 { 68 SerializedAddress add = new SerializedAddress(); 69 add.setCity("Taipei"); 70 71 ClassLoader cla = getClassLoader(); 72 WeakReference refa = new WeakReference (cla); 73 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla); 74 ClassLoader clb = getClassLoader(); 75 WeakReference refb = new WeakReference (clb); 76 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb); 77 78 Fqn fqn = new Fqn("/aop"); 79 cache_.getCache().put(new Fqn("/aop"), "add", add); 80 81 TestingUtil.sleepThread(100); 82 try 83 { 84 Object ben = cache1_.getCache().get(fqn, "add"); 85 assertEquals(add.toString(), ben.toString()); 86 ben = null; 87 } catch (Exception ex) 88 { 89 fail("Test fails with exception " + ex); 90 } 91 92 cache_.getCache().remove(fqn, "add"); 93 94 ClassLoader clc = getClassLoader(); 95 cla = null; 96 clb = null; 97 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc); 98 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc); 99 System.gc(); Thread.sleep(1000); 101 assertNull("Classloader should be gced ", refa.get()); 102 assertNull("Classloader should be gced ", refb.get()); 103 } 104 105 private static void forceOutOfMemoryError() throws Exception 106 { 107 ArrayList list = new ArrayList (); 108 try 109 { 110 111 long i = 0; 112 while (true) 113 { 114 list.add("BigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBig" + (i++)); 115 } 116 } 117 catch (Throwable ignored) 118 { 119 } 120 list.clear(); 121 list = null; 122 System.gc(); 123 Thread.sleep(1000); 124 } 125 126 131 public void testCLLeakage() throws Exception 132 { 133 Person p = new Person(); 134 p.setName("Ben"); 135 Address add = new Address(); 136 add.setCity("Taipei"); 137 138 ClassLoader cla = getClassLoader(); 139 WeakReference refa = new WeakReference (cla); 140 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla); 141 ClassLoader clb = getClassLoader(); 142 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb); 143 WeakReference refb = new WeakReference (clb); 144 145 cache_.attach("/aop", p); 146 147 TestingUtil.sleepThread(100); 148 try 149 { 150 Object ben = cache1_.find("/aop"); 151 assertEquals(p.toString(), ben.toString()); 152 ben = null; 153 } catch (Exception ex) 154 { 155 fail("Test fails with exception " + ex); 156 } 157 158 cache_.detach("/aop"); 159 ClassLoader clc = getClassLoader(); 160 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc); 161 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc); 162 cla = null; 163 clb = null; 164 forceOutOfMemoryError(); 165 166 assertNull("Classloader should be gced ", refa.get()); 167 assertNull("Classloader should be gced ", refb.get()); 168 } 169 170 protected ClassLoader getClassLoader() throws Exception 171 { 172 String [] includesClasses = {"org.jboss.cache.aop.test.Person", 173 "org.jboss.cache.aop.test.Address"}; 174 String [] excludesClasses = {}; 175 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 176 return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl); 177 } 178 179 public static Test suite() throws Exception 180 { 181 return new TestSuite(ReplicatedTest.class); 182 } 183 184 185 public static void main(String [] args) throws Exception 186 { 187 junit.textui.TestRunner.run(ReplicatedTest.suite()); 188 } 189 190 } 191 | Popular Tags |