KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > SemaphoreTest


1 package org.sapia.ubik.net;
2
3 import junit.framework.TestCase;
4
5
6 /**
7  * @author Yanick Duchesne
8  * <dl>
9  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
10  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
11  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
12  * </dl>
13  */

14 public class SemaphoreTest extends TestCase {
15   /**
16    * Constructor for SemaphoreTest.
17    * @param arg0
18    */

19   public SemaphoreTest(String JavaDoc arg0) {
20     super(arg0);
21   }
22
23   public void testAcquire() throws Exception JavaDoc {
24     Semaphore s = new Semaphore(3);
25     s.acquireFor(new Runnable JavaDoc() {
26         public void run() {
27         }
28       });
29     s.acquireFor(new Runnable JavaDoc() {
30         public void run() {
31         }
32       });
33     s.acquireFor(new Runnable JavaDoc() {
34         public void run() {
35         }
36       });
37
38     try {
39       s.acquireFor(new Runnable JavaDoc() {
40           public void run() {
41           }
42         });
43       throw new Exception JavaDoc("Thread creation should not have been authorized");
44     } catch (MaxThreadReachedException e) {
45       // ok
46
}
47   }
48
49   public void testRelease() throws Exception JavaDoc {
50     Semaphore s = new Semaphore(3);
51     Thread JavaDoc t;
52     t = s.acquireFor(new Runnable JavaDoc() {
53           public void run() {
54           }
55         });
56     t.start();
57     Thread.sleep(1000);
58     s.acquireFor(new Runnable JavaDoc() {
59         public void run() {
60         }
61       });
62     s.acquireFor(new Runnable JavaDoc() {
63         public void run() {
64         }
65       });
66     s.acquireFor(new Runnable JavaDoc() {
67         public void run() {
68         }
69       });
70   }
71 }
72
Popular Tags