ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:54 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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    
12     public class ExchangerTest extends TestCase{
13    
14     public static void main(String[] args) {
15     junit.textui.TestRunner.run (suite());
16     }
17     public static Test suite() {
18     return new TestSuite(ExchangerTest.class);
19     }
20    
21     private static long SHORT_DELAY_MS = 100;
22     private static long MEDIUM_DELAY_MS = 1000;
23     private static long LONG_DELAY_MS = 10000;
24    
25     private final static Integer one = new Integer(1);
26     private final static Integer two = new Integer(2);
27    
28     public void testExchange(){
29     final Exchanger e = new Exchanger();
30     Thread t1 = new Thread(new Runnable(){
31     public void run(){
32     try{
33     Object v = e.exchange(one);
34     assertEquals(v, two);
35     Object w = e.exchange(v);
36     assertEquals(w, one);
37     }catch(InterruptedException e){
38     fail("unexpected exception");
39     }
40     }
41     });
42     Thread t2 = new Thread(new Runnable(){
43     public void run(){
44     try{
45     Object v = e.exchange(two);
46     assertEquals(v, one);
47     Object w = e.exchange(v);
48     assertEquals(w, two);
49     }catch(InterruptedException e){
50     fail("unexpected exception");
51     }
52     }
53     });
54     try {
55     t1.start();
56     t2.start();
57     t1.join();
58     t2.join();
59     } catch(InterruptedException ex) {
60     fail("unexpected exception");
61     }
62     }
63    
64     public void testExchange1_InterruptedException(){
65     final Exchanger e = new Exchanger();
66     Thread t = new Thread(new Runnable() {
67     public void run(){
68     try{
69     e.exchange(one);
70     fail("should throw");
71     }catch(InterruptedException success){
72     }
73     }
74     });
75     try{
76     t.start();
77     Thread.sleep(SHORT_DELAY_MS);
78     t.interrupt();
79     t.join();
80     } catch(InterruptedException ex) {
81     fail("unexpected exception");
82     }
83     }
84    
85     public void testExchange2_InterruptedException(){
86     final Exchanger e = new Exchanger();
87     Thread t = new Thread(new Runnable() {
88     public void run(){
89     try{
90     e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
91     fail("should throw");
92     } catch(InterruptedException success){
93     } catch(Exception e2){
94     fail("should throw IE");
95     }
96     }
97     });
98     try{
99     t.start();
100     t.interrupt();
101     t.join();
102     } catch(InterruptedException ex){
103     fail("unexpected exception");
104     }
105     }
106    
107     public void testExchange3_TimeOutException(){
108     final Exchanger e = new Exchanger();
109     Thread t = new Thread(new Runnable() {
110     public void run(){
111     try{
112     e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
113     fail("should throw");
114     }catch(TimeoutException success){}
115     catch(InterruptedException e2){
116     fail("should throw TOE");
117     }
118     }
119     });
120     try{
121     t.start();
122     t.join();
123     } catch(InterruptedException ex){
124     fail("unexpected exception");
125     }
126     }
127     }