ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LockSupportTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.14 by jsr166, Wed Aug 25 00:07:03 2010 UTC

# Line 1 | Line 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.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 10 | Line 11 | import java.util.*;
11   import java.util.concurrent.*;
12   import java.util.concurrent.locks.*;
13  
14 < public class LockSupportTest extends TestCase{
14 <
14 > public class LockSupportTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run(suite());
17      }
18    
19
18      public static Test suite() {
19 <        return new TestSuite(LockSupportTest.class);
19 >        return new TestSuite(LockSupportTest.class);
20 >    }
21 >
22 >    /**
23 >     * park is released by unpark occurring after park
24 >     */
25 >    public void testPark() throws InterruptedException {
26 >        Thread t = new Thread(new CheckedRunnable() {
27 >            public void realRun() {
28 >                LockSupport.park();
29 >            }});
30 >
31 >        t.start();
32 >        Thread.sleep(SHORT_DELAY_MS);
33 >        LockSupport.unpark(t);
34 >        t.join();
35 >    }
36 >
37 >    /**
38 >     * park is released by unpark occurring before park
39 >     */
40 >    public void testPark2() throws InterruptedException {
41 >        Thread t = new Thread(new CheckedRunnable() {
42 >            public void realRun() throws InterruptedException {
43 >                Thread.sleep(SHORT_DELAY_MS);
44 >                LockSupport.park();
45 >            }});
46 >
47 >        t.start();
48 >        LockSupport.unpark(t);
49 >        t.join();
50 >    }
51 >
52 >    /**
53 >     * park is released by interrupt
54 >     */
55 >    public void testPark3() throws InterruptedException {
56 >        Thread t = new Thread(new CheckedRunnable() {
57 >            public void realRun() {
58 >                LockSupport.park();
59 >            }});
60 >
61 >        t.start();
62 >        Thread.sleep(SHORT_DELAY_MS);
63 >        t.interrupt();
64 >        t.join();
65      }
66  
67 <    public void testUnpark() {
68 <        Thread t = new Thread(new Runnable(){
69 <                public void run(){
70 <                    try{
71 <                        LockSupport.park();
72 <                    }catch(Exception e){
73 <                        fail("unexpected exception");
74 <                    }
75 <                }
76 <            });
77 <        t.start();
78 <        try{
79 <            LockSupport.unpark(t);
80 <            t.join();
81 <        }
82 <        catch(Exception e) {
83 <            fail("unexpected exception");
41 <        }
42 <    }
43 <
44 <    public void testParkNanos() {
45 <        Thread t = new Thread(new Runnable(){
46 <                public void run(){
47 <                    try{
48 <                        LockSupport.parkNanos(1000);
49 <                    }catch(Exception e){
50 <                        fail("unexpected exception");
51 <                    }
52 <                }
53 <            });
54 <        try{
55 <            t.start();
56 <            t.join();
57 <        }
58 <        catch(Exception e) {
59 <            fail("unexpected exception");
60 <        }
67 >    /**
68 >     * park returns if interrupted before park
69 >     */
70 >    public void testPark4() throws InterruptedException {
71 >        final ReentrantLock lock = new ReentrantLock();
72 >        lock.lock();
73 >        Thread t = new Thread(new CheckedRunnable() {
74 >            public void realRun() {
75 >                lock.lock();
76 >                LockSupport.park();
77 >            }});
78 >
79 >        t.start();
80 >        Thread.sleep(SHORT_DELAY_MS);
81 >        t.interrupt();
82 >        lock.unlock();
83 >        t.join();
84      }
85  
86 +    /**
87 +     * parkNanos times out if not unparked
88 +     */
89 +    public void testParkNanos() throws InterruptedException {
90 +        Thread t = new Thread(new CheckedRunnable() {
91 +            public void realRun() {
92 +                LockSupport.parkNanos(1000);
93 +            }});
94  
95 <    public void testParkUntil() {
96 <        Thread t = new Thread(new Runnable(){
66 <                public void run(){
67 <                    try{
68 <                        long d = new Date().getTime() + 100;
69 <                        LockSupport.parkUntil(d);
70 <                    }catch(Exception e){
71 <                        fail("unexpected exception");
72 <                    }
73 <                }
74 <            });
75 <        try{
76 <            t.start();
77 <            t.join();
78 <        }
79 <        catch(Exception e) {
80 <            fail("unexpected exception");
81 <        }
95 >        t.start();
96 >        t.join();
97      }
98  
99  
100 +    /**
101 +     * parkUntil times out if not unparked
102 +     */
103 +    public void testParkUntil() throws InterruptedException {
104 +        Thread t = new Thread(new CheckedRunnable() {
105 +            public void realRun() {
106 +                long d = new Date().getTime() + 100;
107 +                LockSupport.parkUntil(d);
108 +            }});
109 +
110 +        t.start();
111 +        t.join();
112 +    }
113   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines