1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.region; |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.lock.UpgradeException; |
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.transaction.DummyTransactionManager; |
21 |
| |
22 |
| import javax.naming.Context; |
23 |
| import javax.naming.InitialContext; |
24 |
| import javax.transaction.UserTransaction; |
25 |
| import java.util.ArrayList; |
26 |
| import java.util.Properties; |
27 |
| import java.util.Random; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class LocalConcurrentTest extends TestCase |
37 |
| { |
38 |
| static PojoCache cache_; |
39 |
| Properties p_; |
40 |
| String oldFactory_ = null; |
41 |
| final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
42 |
| static ArrayList nodeList_; |
43 |
| static final int depth_ = 2; |
44 |
| static final int children_ = 2; |
45 |
| static final int MAX_LOOP = 100; |
46 |
| static final int SLEEP_TIME = 50; |
47 |
| static Exception thread_ex = null; |
48 |
| UserTransaction tx_ = null; |
49 |
| |
50 |
1
| public LocalConcurrentTest(String name)
|
51 |
| { |
52 |
1
| super(name);
|
53 |
| } |
54 |
| |
55 |
1
| public void setUp() throws Exception
|
56 |
| { |
57 |
1
| super.setUp();
|
58 |
1
| oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
59 |
1
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
60 |
1
| DummyTransactionManager.getInstance();
|
61 |
1
| if (p_ == null)
|
62 |
| { |
63 |
1
| p_ = new Properties();
|
64 |
1
| p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
|
65 |
| } |
66 |
| |
67 |
1
| tx_ = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
|
68 |
| |
69 |
1
| initCaches();
|
70 |
1
| nodeList_ = nodeGen(depth_, children_);
|
71 |
| |
72 |
1
| log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
|
73 |
| } |
74 |
| |
75 |
1
| public void tearDown() throws Exception
|
76 |
| { |
77 |
1
| super.tearDown();
|
78 |
1
| thread_ex = null;
|
79 |
1
| DummyTransactionManager.destroy();
|
80 |
1
| destroyCaches();
|
81 |
| |
82 |
1
| if (oldFactory_ != null)
|
83 |
| { |
84 |
0
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
|
85 |
0
| oldFactory_ = null;
|
86 |
| } |
87 |
| |
88 |
| } |
89 |
| |
90 |
1
| void initCaches() throws Exception
|
91 |
| { |
92 |
1
| boolean toStart = false;
|
93 |
1
| cache_ = PojoCacheFactory.createCache("META-INF/local-service.xml", toStart);
|
94 |
1
| cache_.start();
|
95 |
| } |
96 |
| |
97 |
1
| void destroyCaches() throws Exception
|
98 |
| { |
99 |
1
| cache_.stop();
|
100 |
1
| cache_ = null;
|
101 |
| } |
102 |
| |
103 |
1
| public void testAll_RWLock() throws Exception
|
104 |
| { |
105 |
1
| try
|
106 |
| { |
107 |
1
| all();
|
108 |
| } |
109 |
| catch (UpgradeException ue) |
110 |
| { |
111 |
0
| log("Upgrade exception. Can ingore for repeatable read. " + ue);
|
112 |
| } |
113 |
| catch (Exception ex) |
114 |
| { |
115 |
0
| log("Exception: " + ex);
|
116 |
0
| throw ex;
|
117 |
| } |
118 |
| } |
119 |
| |
120 |
1
| private void all() throws Exception
|
121 |
| { |
122 |
1
| RunThread t1 = new RunThread(1, "t1");
|
123 |
1
| RunThread t2 = new RunThread(2, "t2");
|
124 |
1
| RunThread t3 = new RunThread(3, "t3");
|
125 |
1
| RunThread t4 = new RunThread(4, "t4");
|
126 |
| |
127 |
1
| t1.start();
|
128 |
1
| TestingUtil.sleepThread(100);
|
129 |
1
| t2.start();
|
130 |
1
| TestingUtil.sleepThread(100);
|
131 |
1
| t3.start();
|
132 |
1
| TestingUtil.sleepThread(100);
|
133 |
1
| t4.start();
|
134 |
| |
135 |
1
| t1.join(60000);
|
136 |
1
| t2.join(60000);
|
137 |
1
| t3.join(60000);
|
138 |
1
| t4.join(60000);
|
139 |
| |
140 |
1
| if (thread_ex != null)
|
141 |
0
| throw thread_ex;
|
142 |
| } |
143 |
| |
144 |
| class RunThread extends Thread |
145 |
| { |
146 |
| final int seed_; |
147 |
| Random random_; |
148 |
| Person person_; |
149 |
| |
150 |
4
| public RunThread(int seed, String threadName)
|
151 |
| { |
152 |
4
| super(threadName);
|
153 |
4
| seed_ = seed;
|
154 |
4
| random_ = new Random(seed);
|
155 |
| } |
156 |
| |
157 |
400
| private void createPerson()
|
158 |
| { |
159 |
400
| person_ = new Person();
|
160 |
400
| person_.setName("Ben");
|
161 |
400
| person_.setAge(18);
|
162 |
400
| ArrayList<String> lang = new ArrayList<String>();
|
163 |
400
| lang.add("English");
|
164 |
400
| lang.add("French");
|
165 |
400
| lang.add("Mandarin");
|
166 |
400
| person_.setLanguages(lang);
|
167 |
400
| Address addr = new Address();
|
168 |
400
| addr.setZip(95123);
|
169 |
400
| addr.setStreet("Almeria");
|
170 |
400
| addr.setCity("San Jose");
|
171 |
400
| person_.setAddress(addr);
|
172 |
| } |
173 |
| |
174 |
4
| public void run()
|
175 |
| { |
176 |
4
| try
|
177 |
| { |
178 |
4
| cache_.getCache().getRegion(Fqn.fromString(Thread.currentThread().getName()), true);
|
179 |
4
| _run();
|
180 |
| } |
181 |
| catch (Exception e) |
182 |
| { |
183 |
0
| thread_ex = e;
|
184 |
| } |
185 |
| } |
186 |
| |
187 |
| |
188 |
| |
189 |
4
| public void _run() throws Exception
|
190 |
| { |
191 |
4
| for (int loop = 0; loop < MAX_LOOP; loop++)
|
192 |
| { |
193 |
400
| createPerson();
|
194 |
400
| op1();
|
195 |
| } |
196 |
| } |
197 |
| |
198 |
| |
199 |
400
| private void op1()
|
200 |
| { |
201 |
400
| int i = random_.nextInt(nodeList_.size() - 1);
|
202 |
58
| if (i == 0) return;
|
203 |
342
| String node = (String) nodeList_.get(i) + "/aop";
|
204 |
342
| cache_.attach(node, person_);
|
205 |
342
| TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));
|
206 |
342
| TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));
|
207 |
342
| cache_.detach(node);
|
208 |
| } |
209 |
| } |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
1
| private ArrayList nodeGen(int depth, int children)
|
217 |
| { |
218 |
1
| ArrayList<String> strList = new ArrayList<String>();
|
219 |
1
| ArrayList<String> oldList = new ArrayList<String>();
|
220 |
1
| ArrayList<String> newList = new ArrayList<String>();
|
221 |
| |
222 |
| |
223 |
1
| String str = Thread.currentThread().getName();
|
224 |
1
| oldList.add(str);
|
225 |
1
| newList.add(str);
|
226 |
1
| strList.add(str);
|
227 |
| |
228 |
1
| while (depth > 0)
|
229 |
| { |
230 |
| |
231 |
2
| newList = new ArrayList<String>();
|
232 |
2
| for (int i = 0; i < oldList.size(); i++)
|
233 |
| { |
234 |
3
| for (int j = 0; j < children; j++)
|
235 |
| { |
236 |
6
| String tmp = (String) oldList.get(i);
|
237 |
6
| tmp += Integer.toString(j);
|
238 |
6
| if (depth != 1)
|
239 |
| { |
240 |
2
| tmp += "/";
|
241 |
| } |
242 |
| |
243 |
6
| newList.add(tmp);
|
244 |
| } |
245 |
| } |
246 |
2
| strList.addAll(newList);
|
247 |
2
| oldList = newList;
|
248 |
2
| depth--;
|
249 |
| } |
250 |
| |
251 |
| |
252 |
1
| for (int i = 0; i < strList.size(); i++)
|
253 |
| { |
254 |
7
| if (strList.get(i).equals("/"))
|
255 |
| { |
256 |
0
| strList.remove(i);
|
257 |
0
| break;
|
258 |
| } |
259 |
| } |
260 |
1
| log("Nodes generated: " + strList.size());
|
261 |
1
| return strList;
|
262 |
| } |
263 |
| |
264 |
1
| public static Test suite() throws Exception
|
265 |
| { |
266 |
1
| return new TestSuite(org.jboss.cache.pojo.region.LocalConcurrentTest.class);
|
267 |
| } |
268 |
| |
269 |
2
| private static void log(String str)
|
270 |
| { |
271 |
2
| System.out.println("Thread: " + Thread.currentThread() + ": " + str);
|
272 |
| |
273 |
| } |
274 |
| |
275 |
| } |