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