|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.utils.concurrent; |
|
20 |
|
21 import java.util.Enumeration; |
|
22 import java.util.Vector; |
|
23 |
|
24 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
25 import com.nokia.mj.impl.utils.DebugUtils; |
|
26 |
|
27 import j2meunit.framework.Test; |
|
28 import j2meunit.framework.TestCase; |
|
29 import j2meunit.framework.TestMethod; |
|
30 import j2meunit.framework.TestSuite; |
|
31 |
|
32 /** |
|
33 * BufferedReader unit tests. |
|
34 */ |
|
35 public class ConcurrentTests extends TestCase implements InstallerMain |
|
36 { |
|
37 private int mMaxThreadId; |
|
38 private int mReleaseThreadId; |
|
39 private int mNextReleasedThreadId; |
|
40 private int mRunningThreadCount; |
|
41 private Semaphore mSem; |
|
42 private Object mLockEnd; |
|
43 |
|
44 // Begin j2meunit test framework setup |
|
45 public void installerMain(String[] args) |
|
46 { |
|
47 createDumperThread(false); |
|
48 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
49 |
|
50 |
|
51 suite.addTest(new ConcurrentTests("testLockBasic", new TestMethod() |
|
52 { |
|
53 public void run(TestCase tc) |
|
54 { |
|
55 ((ConcurrentTests)tc).testLockBasic(); |
|
56 } |
|
57 })); |
|
58 |
|
59 suite.addTest(new ConcurrentTests("testLockFair", new TestMethod() |
|
60 { |
|
61 public void run(TestCase tc) |
|
62 { |
|
63 ((ConcurrentTests)tc).testLockFair(); |
|
64 } |
|
65 })); |
|
66 |
|
67 |
|
68 suite.addTest(new ConcurrentTests("testLockFair2", new TestMethod() |
|
69 { |
|
70 public void run(TestCase tc) |
|
71 { |
|
72 ((ConcurrentTests)tc).testLockFair2(); |
|
73 } |
|
74 })); |
|
75 |
|
76 suite.addTest(new ConcurrentTests("testLockUnFair", new TestMethod() |
|
77 { |
|
78 public void run(TestCase tc) |
|
79 { |
|
80 ((ConcurrentTests)tc).testLockUnFair(); |
|
81 } |
|
82 })); |
|
83 |
|
84 suite.addTest(new ConcurrentTests("testLockUnFair2", new TestMethod() |
|
85 { |
|
86 public void run(TestCase tc) |
|
87 { |
|
88 ((ConcurrentTests)tc).testLockUnFair2(); |
|
89 } |
|
90 })); |
|
91 |
|
92 suite.addTest(new ConcurrentTests("testLockUnFair3", new TestMethod() |
|
93 { |
|
94 public void run(TestCase tc) |
|
95 { |
|
96 ((ConcurrentTests)tc).testLockUnFair3(); |
|
97 } |
|
98 })); |
|
99 |
|
100 |
|
101 suite.addTest(new ConcurrentTests("testCondition1", new TestMethod() |
|
102 { |
|
103 public void run(TestCase tc) |
|
104 { |
|
105 ((ConcurrentTests)tc).testCondition1(); |
|
106 } |
|
107 })); |
|
108 |
|
109 suite.addTest(new ConcurrentTests("testCondition10_10", new TestMethod() |
|
110 { |
|
111 public void run(TestCase tc) |
|
112 { |
|
113 ((ConcurrentTests)tc).testCondition10_10(); |
|
114 } |
|
115 })); |
|
116 |
|
117 |
|
118 suite.addTest(new ConcurrentTests("testCondition5_10", new TestMethod() |
|
119 { |
|
120 public void run(TestCase tc) |
|
121 { |
|
122 ((ConcurrentTests)tc).testCondition5_10(); |
|
123 } |
|
124 })); |
|
125 |
|
126 |
|
127 |
|
128 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
129 |
|
130 } |
|
131 |
|
132 public ConcurrentTests() |
|
133 { |
|
134 } |
|
135 |
|
136 public ConcurrentTests(String aTestName, TestMethod aTestMethod) |
|
137 { |
|
138 super(aTestName, aTestMethod); |
|
139 } |
|
140 |
|
141 private void createDumperThread(boolean doCreate) |
|
142 { |
|
143 if (doCreate) |
|
144 { |
|
145 new Thread(new Runnable(){ |
|
146 public void run() |
|
147 { |
|
148 threadSleep(5000); |
|
149 } |
|
150 }).start(); |
|
151 } |
|
152 } |
|
153 |
|
154 |
|
155 public void assertFalse(String aMsg, boolean aCondition) |
|
156 { |
|
157 assertTrue(aMsg, !aCondition); |
|
158 } |
|
159 |
|
160 // End j2meunit test framework setup |
|
161 |
|
162 protected void setUp() |
|
163 { |
|
164 } |
|
165 |
|
166 protected void tearDown() |
|
167 { |
|
168 } |
|
169 |
|
170 |
|
171 private synchronized void incThreadCount() |
|
172 { |
|
173 mRunningThreadCount++; |
|
174 } |
|
175 |
|
176 private synchronized void decThreadCount() |
|
177 { |
|
178 mRunningThreadCount--; |
|
179 } |
|
180 |
|
181 private Vector /*String*/ mTrace = new Vector(); |
|
182 |
|
183 private void trace(String str) |
|
184 { |
|
185 trace(str, false); |
|
186 } |
|
187 |
|
188 private void trace(String str, boolean doTrace) |
|
189 { |
|
190 if (doTrace) |
|
191 { |
|
192 //System.out.println(str); |
|
193 mTrace.addElement(str); |
|
194 } |
|
195 } |
|
196 |
|
197 |
|
198 private void printTrace() |
|
199 { |
|
200 Enumeration enumer = mTrace.elements(); |
|
201 while (enumer.hasMoreElements()) |
|
202 { |
|
203 String s = (String)enumer.nextElement(); |
|
204 System.out.println(s); |
|
205 } |
|
206 mTrace.removeAllElements(); |
|
207 } |
|
208 |
|
209 |
|
210 |
|
211 // Test cases |
|
212 |
|
213 /** LOCK TESTS*/ |
|
214 |
|
215 private void testLockBasic() |
|
216 { |
|
217 try |
|
218 { |
|
219 Semaphore sem = new Semaphore(1, true); |
|
220 sem.acquire(); |
|
221 sem.release(); |
|
222 sem.acquire(); |
|
223 sem.release(); |
|
224 sem.acquire(); |
|
225 sem.release(); |
|
226 sem = null; |
|
227 |
|
228 Semaphore sem2 = new Semaphore(1, false); |
|
229 sem2.acquire(); |
|
230 sem2.release(); |
|
231 sem2.acquire(); |
|
232 sem2.release(); |
|
233 sem2.acquire(); |
|
234 sem2.release(); |
|
235 } |
|
236 catch (Throwable t) |
|
237 { |
|
238 assertTrue(t.toString(), false); |
|
239 t.printStackTrace(); |
|
240 } |
|
241 } |
|
242 |
|
243 |
|
244 private void testLockFair() |
|
245 { |
|
246 mRunningThreadCount = 0; |
|
247 mNextReleasedThreadId = 1; |
|
248 mReleaseThreadId = 10; |
|
249 mMaxThreadId = 10; |
|
250 |
|
251 try |
|
252 { |
|
253 |
|
254 testLock(true); |
|
255 } |
|
256 catch (Throwable t) |
|
257 { |
|
258 assertTrue(t.toString(), false); |
|
259 t.printStackTrace(); |
|
260 } |
|
261 } |
|
262 |
|
263 private void testLockFair2() |
|
264 { |
|
265 mRunningThreadCount = 0; |
|
266 mNextReleasedThreadId = 1; |
|
267 mReleaseThreadId = 5; |
|
268 mMaxThreadId = 10; |
|
269 |
|
270 try |
|
271 { |
|
272 |
|
273 testLock(true); |
|
274 } |
|
275 catch (Throwable t) |
|
276 { |
|
277 assertTrue(t.toString(), false); |
|
278 t.printStackTrace(); |
|
279 } |
|
280 } |
|
281 |
|
282 private void testLockUnFair() |
|
283 { |
|
284 mRunningThreadCount = 0; |
|
285 mNextReleasedThreadId = 1; |
|
286 mReleaseThreadId = 10; |
|
287 mMaxThreadId = 10; |
|
288 |
|
289 try |
|
290 { |
|
291 |
|
292 testLock(false); |
|
293 } |
|
294 catch (Throwable t) |
|
295 { |
|
296 assertTrue(t.toString(), false); |
|
297 t.printStackTrace(); |
|
298 } |
|
299 } |
|
300 |
|
301 private void testLockUnFair2() |
|
302 { |
|
303 mRunningThreadCount = 0; |
|
304 mNextReleasedThreadId = 1; |
|
305 mReleaseThreadId = 5; |
|
306 mMaxThreadId = 10; |
|
307 |
|
308 try |
|
309 { |
|
310 |
|
311 testLock(false); |
|
312 } |
|
313 catch (Throwable t) |
|
314 { |
|
315 assertTrue(t.toString(), false); |
|
316 t.printStackTrace(); |
|
317 } |
|
318 } |
|
319 |
|
320 private void testLockUnFair3() |
|
321 { |
|
322 try |
|
323 { |
|
324 mSem = new Semaphore(1, false); |
|
325 mSem.acquire(); |
|
326 new Thread(new Runnable() |
|
327 { |
|
328 public void run() |
|
329 { |
|
330 threadSleep(100); |
|
331 mSem.release(); |
|
332 } |
|
333 }).start(); |
|
334 mSem.acquire(); |
|
335 mSem.release(); |
|
336 mSem.acquire(); |
|
337 new Thread(new Runnable() |
|
338 { |
|
339 public void run() |
|
340 { |
|
341 threadSleep(100); |
|
342 mSem.release(); |
|
343 } |
|
344 }).start(); |
|
345 mSem.acquire(); |
|
346 mSem.release(); |
|
347 } |
|
348 catch (Throwable t) |
|
349 { |
|
350 assertTrue(t.toString(), false); |
|
351 t.printStackTrace(); |
|
352 } |
|
353 } |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 private class LockTestThread extends Thread |
|
359 { |
|
360 private int mId; |
|
361 private boolean mCheck; |
|
362 |
|
363 private LockTestThread(int id, boolean check) |
|
364 { |
|
365 mId = id; |
|
366 mCheck = check; |
|
367 } |
|
368 public void run() |
|
369 { |
|
370 threadSleep(20); |
|
371 trace("run: "+mId); |
|
372 incThreadCount(); |
|
373 if (mId < mMaxThreadId) |
|
374 { |
|
375 // Need to create additional thread. |
|
376 trace("creating new thread: "+mId); |
|
377 new LockTestThread(mId+1, mCheck).start(); |
|
378 } |
|
379 if (mReleaseThreadId == mId) |
|
380 { |
|
381 synchronized (mLockEnd) |
|
382 { |
|
383 trace("notify: "+mId); |
|
384 mLockEnd.notify(); |
|
385 } |
|
386 } |
|
387 trace("Getting lock: "+mId); |
|
388 mSem.acquire(); |
|
389 trace("Got lock: "+mId); |
|
390 if (mCheck) |
|
391 { |
|
392 String errorTxt = "Incorrect release order. mId: "+ mId + |
|
393 ", next: " +mNextReleasedThreadId; |
|
394 assertTrue(errorTxt, mNextReleasedThreadId == mId); |
|
395 } |
|
396 mNextReleasedThreadId++; |
|
397 trace("unlock: "+mId); |
|
398 mSem.release(); |
|
399 decThreadCount(); |
|
400 trace("done: "+mId); |
|
401 } |
|
402 } |
|
403 |
|
404 private void threadSleep(int ms) |
|
405 { |
|
406 try |
|
407 { |
|
408 Thread.sleep(ms); |
|
409 } |
|
410 catch (Exception e) |
|
411 { |
|
412 } |
|
413 } |
|
414 private void testLock(boolean isFair) throws Exception |
|
415 { |
|
416 mSem = new Semaphore(1,isFair); |
|
417 mSem.acquire(); |
|
418 new LockTestThread(1, isFair).start(); |
|
419 mLockEnd = new Object(); |
|
420 synchronized (mLockEnd) |
|
421 { |
|
422 // Wait that all the threads has been started. |
|
423 trace("Wait()"); |
|
424 mLockEnd.wait(); |
|
425 trace("wait ok()"); |
|
426 } |
|
427 mSem.release(); |
|
428 trace("unlock after wait"); |
|
429 for (int i = 0; i < 5; ++i) |
|
430 { |
|
431 threadSleep(100); |
|
432 trace("Woke from sleep."); |
|
433 if (mRunningThreadCount == 0) |
|
434 { |
|
435 break; |
|
436 } |
|
437 } |
|
438 printTrace(); |
|
439 assertTrue("Threads still running: " + mRunningThreadCount, mRunningThreadCount == 0); |
|
440 } |
|
441 |
|
442 /** CONDITION TESTS*/ |
|
443 private ConditionObject mCondition; |
|
444 |
|
445 private void testCondition1() |
|
446 { |
|
447 mRunningThreadCount = 0; |
|
448 mNextReleasedThreadId = 1; |
|
449 mReleaseThreadId = 1; |
|
450 mMaxThreadId = 1; |
|
451 |
|
452 try |
|
453 { |
|
454 testCondtion(); |
|
455 } |
|
456 catch (Throwable t) |
|
457 { |
|
458 assertTrue(t.toString(), false); |
|
459 t.printStackTrace(); |
|
460 } |
|
461 } |
|
462 |
|
463 private void testCondition10_10() |
|
464 { |
|
465 mRunningThreadCount = 0; |
|
466 mNextReleasedThreadId = 1; |
|
467 mReleaseThreadId = 10; |
|
468 mMaxThreadId = 10; |
|
469 |
|
470 try |
|
471 { |
|
472 testCondtion(); |
|
473 } |
|
474 catch (Throwable t) |
|
475 { |
|
476 assertTrue(t.toString(), false); |
|
477 t.printStackTrace(); |
|
478 } |
|
479 } |
|
480 |
|
481 private void testCondition5_10() |
|
482 { |
|
483 mRunningThreadCount = 0; |
|
484 mNextReleasedThreadId = 1; |
|
485 mReleaseThreadId = 5; |
|
486 mMaxThreadId = 10; |
|
487 |
|
488 try |
|
489 { |
|
490 testCondtion(); |
|
491 } |
|
492 catch (Throwable t) |
|
493 { |
|
494 assertTrue(t.toString(), false); |
|
495 t.printStackTrace(); |
|
496 } |
|
497 } |
|
498 |
|
499 |
|
500 private class ConditionTestThread extends Thread |
|
501 { |
|
502 private int mId; |
|
503 |
|
504 private ConditionTestThread(int id) |
|
505 { |
|
506 mId = id; |
|
507 } |
|
508 public void run() |
|
509 { |
|
510 threadSleep(20); |
|
511 trace("run: "+mId); |
|
512 incThreadCount(); |
|
513 |
|
514 if (mReleaseThreadId == mId) |
|
515 { |
|
516 synchronized (mLockEnd) |
|
517 { |
|
518 trace("notify: "+mId); |
|
519 mLockEnd.notify(); |
|
520 } |
|
521 threadSleep(50); |
|
522 } |
|
523 |
|
524 if (mId < mMaxThreadId) |
|
525 { |
|
526 // Need to create additional thread. |
|
527 trace("creating new thread: "+mId); |
|
528 new ConditionTestThread(mId+1).start(); |
|
529 } |
|
530 |
|
531 trace("Going to await: "+mId); |
|
532 mCondition.await(); |
|
533 trace("out from await: "+mId); |
|
534 decThreadCount(); |
|
535 trace("done: "+mId); |
|
536 } |
|
537 } |
|
538 |
|
539 private void testCondtion() throws Exception |
|
540 { |
|
541 mCondition = new ConditionObject(); |
|
542 new ConditionTestThread(1).start(); |
|
543 |
|
544 mLockEnd = new Object(); |
|
545 synchronized (mLockEnd) |
|
546 { |
|
547 // Wait that all the threads has been started. |
|
548 trace("wait()-->"); |
|
549 mLockEnd.wait(); |
|
550 trace("<--wait()"); |
|
551 } |
|
552 |
|
553 assertTrue("Not correct amount of threads waiting: " + mRunningThreadCount, mRunningThreadCount == mReleaseThreadId); |
|
554 if (mMaxThreadId == 1) |
|
555 { |
|
556 threadSleep(100); |
|
557 } |
|
558 trace("before signal()"); |
|
559 mCondition.signal(); |
|
560 trace("after signal()"); |
|
561 for (int i = 0; i < 5; ++i) |
|
562 { |
|
563 threadSleep(100); |
|
564 trace("Woke from sleep."); |
|
565 if (mRunningThreadCount == 0) |
|
566 { |
|
567 break; |
|
568 } |
|
569 } |
|
570 printTrace(); |
|
571 assertTrue("Threads still running: " + mRunningThreadCount, mRunningThreadCount == 0); |
|
572 } |
|
573 } |