changeset 76 | 4ad59aaee882 |
parent 26 | dc7c549001d5 |
child 83 | 26b2b12093af |
69:773449708c84 | 76:4ad59aaee882 |
---|---|
19 package com.nokia.mj.impl.utils.concurrent; |
19 package com.nokia.mj.impl.utils.concurrent; |
20 |
20 |
21 import java.util.Enumeration; |
21 import java.util.Enumeration; |
22 import java.util.Vector; |
22 import java.util.Vector; |
23 |
23 |
24 import com.nokia.mj.impl.installer.utils.InstallerMain; |
24 import com.nokia.mj.impl.rt.test.UnitTestSuiteCreator; |
25 import com.nokia.mj.impl.utils.DebugUtils; |
|
26 |
25 |
27 import j2meunit.framework.Test; |
26 import j2meunit.framework.Test; |
28 import j2meunit.framework.TestCase; |
27 import j2meunit.framework.TestCase; |
29 import j2meunit.framework.TestMethod; |
28 import j2meunit.framework.TestMethod; |
30 import j2meunit.framework.TestSuite; |
29 import j2meunit.framework.TestSuite; |
31 |
30 |
32 /** |
31 /** |
33 * BufferedReader unit tests. |
32 * BufferedReader unit tests. |
34 */ |
33 */ |
35 public class ConcurrentTests extends TestCase implements InstallerMain |
34 public class ConcurrentTests extends TestCase implements UnitTestSuiteCreator |
36 { |
35 { |
37 private int mMaxThreadId; |
36 private int mMaxThreadId; |
38 private int mReleaseThreadId; |
37 private int mReleaseThreadId; |
39 private int mNextReleasedThreadId; |
38 private int mNextReleasedThreadId; |
40 private int mRunningThreadCount; |
39 private int mRunningThreadCount; |
41 private Semaphore mSem; |
40 private Semaphore mSem; |
42 private Object mLockEnd; |
41 private Object mLockEnd; |
43 |
42 |
44 // Begin j2meunit test framework setup |
43 // Begin j2meunit test framework setup |
45 public void installerMain(String[] args) |
44 public TestSuite createTestSuite(String[] args) |
46 { |
45 { |
47 createDumperThread(false); |
46 createDumperThread(false); |
48 TestSuite suite = new TestSuite(this.getClass().getName()); |
47 TestSuite suite = new TestSuite(this.getClass().getName()); |
49 |
48 |
50 |
49 |
121 { |
120 { |
122 ((ConcurrentTests)tc).testCondition5_10(); |
121 ((ConcurrentTests)tc).testCondition5_10(); |
123 } |
122 } |
124 })); |
123 })); |
125 |
124 |
126 |
125 return suite; |
127 |
|
128 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
129 |
126 |
130 } |
127 } |
131 |
128 |
132 public ConcurrentTests() |
129 public ConcurrentTests() |
133 { |
130 { |
140 |
137 |
141 private void createDumperThread(boolean doCreate) |
138 private void createDumperThread(boolean doCreate) |
142 { |
139 { |
143 if (doCreate) |
140 if (doCreate) |
144 { |
141 { |
145 new Thread(new Runnable(){ |
142 new Thread(new Runnable() |
143 { |
|
146 public void run() |
144 public void run() |
147 { |
145 { |
148 threadSleep(5000); |
146 threadSleep(5000); |
149 } |
147 } |
150 }).start(); |
148 }).start(); |
187 |
185 |
188 private void trace(String str, boolean doTrace) |
186 private void trace(String str, boolean doTrace) |
189 { |
187 { |
190 if (doTrace) |
188 if (doTrace) |
191 { |
189 { |
192 //System.out.println(str); |
190 //System.out.println(str); |
193 mTrace.addElement(str); |
191 mTrace.addElement(str); |
194 } |
192 } |
195 } |
193 } |
196 |
194 |
197 |
195 |
198 private void printTrace() |
196 private void printTrace() |
248 mReleaseThreadId = 10; |
246 mReleaseThreadId = 10; |
249 mMaxThreadId = 10; |
247 mMaxThreadId = 10; |
250 |
248 |
251 try |
249 try |
252 { |
250 { |
253 |
251 |
254 testLock(true); |
252 testLock(true); |
255 } |
253 } |
256 catch (Throwable t) |
254 catch (Throwable t) |
257 { |
255 { |
258 assertTrue(t.toString(), false); |
256 assertTrue(t.toString(), false); |
267 mReleaseThreadId = 5; |
265 mReleaseThreadId = 5; |
268 mMaxThreadId = 10; |
266 mMaxThreadId = 10; |
269 |
267 |
270 try |
268 try |
271 { |
269 { |
272 |
270 |
273 testLock(true); |
271 testLock(true); |
274 } |
272 } |
275 catch (Throwable t) |
273 catch (Throwable t) |
276 { |
274 { |
277 assertTrue(t.toString(), false); |
275 assertTrue(t.toString(), false); |
286 mReleaseThreadId = 10; |
284 mReleaseThreadId = 10; |
287 mMaxThreadId = 10; |
285 mMaxThreadId = 10; |
288 |
286 |
289 try |
287 try |
290 { |
288 { |
291 |
289 |
292 testLock(false); |
290 testLock(false); |
293 } |
291 } |
294 catch (Throwable t) |
292 catch (Throwable t) |
295 { |
293 { |
296 assertTrue(t.toString(), false); |
294 assertTrue(t.toString(), false); |
305 mReleaseThreadId = 5; |
303 mReleaseThreadId = 5; |
306 mMaxThreadId = 10; |
304 mMaxThreadId = 10; |
307 |
305 |
308 try |
306 try |
309 { |
307 { |
310 |
308 |
311 testLock(false); |
309 testLock(false); |
312 } |
310 } |
313 catch (Throwable t) |
311 catch (Throwable t) |
314 { |
312 { |
315 assertTrue(t.toString(), false); |
313 assertTrue(t.toString(), false); |
357 |
355 |
358 private class LockTestThread extends Thread |
356 private class LockTestThread extends Thread |
359 { |
357 { |
360 private int mId; |
358 private int mId; |
361 private boolean mCheck; |
359 private boolean mCheck; |
362 |
360 |
363 private LockTestThread(int id, boolean check) |
361 private LockTestThread(int id, boolean check) |
364 { |
362 { |
365 mId = id; |
363 mId = id; |
366 mCheck = check; |
364 mCheck = check; |
367 } |
365 } |
368 public void run() |
366 public void run() |
369 { |
367 { |
370 threadSleep(20); |
368 threadSleep(20); |
371 trace("run: "+mId); |
369 trace("run: "+mId); |
372 incThreadCount(); |
370 incThreadCount(); |
373 if (mId < mMaxThreadId) |
371 if (mId < mMaxThreadId) |
387 trace("Getting lock: "+mId); |
385 trace("Getting lock: "+mId); |
388 mSem.acquire(); |
386 mSem.acquire(); |
389 trace("Got lock: "+mId); |
387 trace("Got lock: "+mId); |
390 if (mCheck) |
388 if (mCheck) |
391 { |
389 { |
392 String errorTxt = "Incorrect release order. mId: "+ mId + |
390 String errorTxt = "Incorrect release order. mId: "+ mId + |
393 ", next: " +mNextReleasedThreadId; |
391 ", next: " +mNextReleasedThreadId; |
394 assertTrue(errorTxt, mNextReleasedThreadId == mId); |
392 assertTrue(errorTxt, mNextReleasedThreadId == mId); |
395 } |
393 } |
396 mNextReleasedThreadId++; |
394 mNextReleasedThreadId++; |
397 trace("unlock: "+mId); |
395 trace("unlock: "+mId); |
498 |
496 |
499 |
497 |
500 private class ConditionTestThread extends Thread |
498 private class ConditionTestThread extends Thread |
501 { |
499 { |
502 private int mId; |
500 private int mId; |
503 |
501 |
504 private ConditionTestThread(int id) |
502 private ConditionTestThread(int id) |
505 { |
503 { |
506 mId = id; |
504 mId = id; |
507 } |
505 } |
508 public void run() |
506 public void run() |
509 { |
507 { |
510 threadSleep(20); |
508 threadSleep(20); |
511 trace("run: "+mId); |
509 trace("run: "+mId); |
512 incThreadCount(); |
510 incThreadCount(); |
513 |
511 |