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