ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.4
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.3: +82 -5 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11     import java.util.concurrent.locks.*;
12    
13 dl 1.2 public class LockSupportTest extends JSR166TestCase{
14 dl 1.1 public static void main(String[] args) {
15     junit.textui.TestRunner.run (suite());
16     }
17     public static Test suite() {
18     return new TestSuite(LockSupportTest.class);
19     }
20    
21 dl 1.3 /**
22 dl 1.4 * park is released by unpark occuring after park
23 dl 1.3 */
24 dl 1.4 public void testPark() {
25 dl 1.3 Thread t = new Thread(new Runnable() {
26     public void run() {
27     try {
28 dl 1.1 LockSupport.park();
29 dl 1.3 } catch(Exception e){
30     threadUnexpectedException();
31 dl 1.1 }
32     }
33     });
34 dl 1.3 try {
35 dl 1.4 t.start();
36     Thread.sleep(SHORT_DELAY_MS);
37 dl 1.1 LockSupport.unpark(t);
38     t.join();
39     }
40     catch(Exception e) {
41 dl 1.3 unexpectedException();
42 dl 1.1 }
43     }
44    
45 dl 1.3 /**
46 dl 1.4 * park is released by unpark occuring before park
47     */
48     public void testPark2() {
49     Thread t = new Thread(new Runnable() {
50     public void run() {
51     try {
52     Thread.sleep(SHORT_DELAY_MS);
53     LockSupport.park();
54     } catch(Exception e){
55     threadUnexpectedException();
56     }
57     }
58     });
59     try {
60     t.start();
61     LockSupport.unpark(t);
62     t.join();
63     }
64     catch(Exception e) {
65     unexpectedException();
66     }
67     }
68    
69     /**
70     * park is released by interrupt
71     */
72     public void testPark3() {
73     Thread t = new Thread(new Runnable() {
74     public void run() {
75     try {
76     LockSupport.park();
77     threadAssertTrue(Thread.interrupted());
78     } catch(Exception e){
79     threadUnexpectedException();
80     }
81     }
82     });
83     try {
84     t.start();
85     Thread.sleep(SHORT_DELAY_MS);
86     t.interrupt();
87     t.join();
88     }
89     catch(Exception e) {
90     unexpectedException();
91     }
92     }
93    
94     /**
95     * park returns if interrupted before park
96     */
97     public void testPark4() {
98     final ReentrantLock lock = new ReentrantLock();
99     lock.lock();
100     Thread t = new Thread(new Runnable() {
101     public void run() {
102     try {
103     lock.lock();
104     LockSupport.park();
105     } catch(Exception e){
106     threadUnexpectedException();
107     }
108     }
109     });
110     try {
111     t.start();
112     t.interrupt();
113     lock.unlock();
114     t.join();
115     }
116     catch(Exception e) {
117     unexpectedException();
118     }
119     }
120    
121     /**
122     * parkNanos times out if not unparked
123 dl 1.3 */
124 dl 1.1 public void testParkNanos() {
125 dl 1.3 Thread t = new Thread(new Runnable() {
126     public void run() {
127     try {
128 dl 1.1 LockSupport.parkNanos(1000);
129 dl 1.3 } catch(Exception e){
130     threadUnexpectedException();
131 dl 1.1 }
132     }
133     });
134 dl 1.3 try {
135 dl 1.1 t.start();
136     t.join();
137     }
138     catch(Exception e) {
139 dl 1.3 unexpectedException();
140 dl 1.1 }
141     }
142    
143    
144 dl 1.3 /**
145 dl 1.4 * parkUntil times out if not unparked
146 dl 1.3 */
147 dl 1.1 public void testParkUntil() {
148 dl 1.3 Thread t = new Thread(new Runnable() {
149     public void run() {
150     try {
151 dl 1.1 long d = new Date().getTime() + 100;
152     LockSupport.parkUntil(d);
153 dl 1.3 } catch(Exception e){
154     threadUnexpectedException();
155 dl 1.1 }
156     }
157     });
158 dl 1.3 try {
159 dl 1.1 t.start();
160     t.join();
161     }
162     catch(Exception e) {
163 dl 1.3 unexpectedException();
164 dl 1.1 }
165     }
166     }