ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.3
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +29 -14 lines
Log Message:
Documentation scaffolding

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 dl 1.2 public class ExchangerTest extends JSR166TestCase {
13 dl 1.1
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 dl 1.3 /**
22     *
23     */
24 dl 1.2 public void testExchange() {
25 dl 1.1 final Exchanger e = new Exchanger();
26     Thread t1 = new Thread(new Runnable(){
27     public void run(){
28 dl 1.2 try {
29 dl 1.1 Object v = e.exchange(one);
30 dl 1.2 threadAssertEquals(v, two);
31 dl 1.1 Object w = e.exchange(v);
32 dl 1.2 threadAssertEquals(w, one);
33     } catch(InterruptedException e){
34 dl 1.3 threadUnexpectedException();
35 dl 1.1 }
36     }
37     });
38     Thread t2 = new Thread(new Runnable(){
39     public void run(){
40 dl 1.2 try {
41 dl 1.1 Object v = e.exchange(two);
42 dl 1.2 threadAssertEquals(v, one);
43 dl 1.1 Object w = e.exchange(v);
44 dl 1.2 threadAssertEquals(w, two);
45     } catch(InterruptedException e){
46 dl 1.3 threadUnexpectedException();
47 dl 1.2 }
48     }
49     });
50     try {
51     t1.start();
52     t2.start();
53     t1.join();
54     t2.join();
55     } catch(InterruptedException ex) {
56 dl 1.3 unexpectedException();
57 dl 1.2 }
58     }
59    
60 dl 1.3 /**
61     *
62     */
63 dl 1.2 public void testTimedExchange() {
64     final Exchanger e = new Exchanger();
65     Thread t1 = new Thread(new Runnable(){
66     public void run(){
67     try {
68     Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
69     threadAssertEquals(v, two);
70     Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
71     threadAssertEquals(w, one);
72     } catch(InterruptedException e){
73 dl 1.3 threadUnexpectedException();
74 dl 1.2 } catch(TimeoutException toe) {
75 dl 1.3 threadUnexpectedException();
76 dl 1.2 }
77     }
78     });
79     Thread t2 = new Thread(new Runnable(){
80     public void run(){
81     try {
82     Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
83     threadAssertEquals(v, one);
84     Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
85     threadAssertEquals(w, two);
86     } catch(InterruptedException e){
87 dl 1.3 threadUnexpectedException();
88 dl 1.2 } catch(TimeoutException toe) {
89 dl 1.3 threadUnexpectedException();
90 dl 1.1 }
91     }
92     });
93     try {
94     t1.start();
95     t2.start();
96     t1.join();
97     t2.join();
98     } catch(InterruptedException ex) {
99 dl 1.3 unexpectedException();
100 dl 1.1 }
101     }
102    
103 dl 1.3 /**
104     *
105     */
106 dl 1.2 public void testExchange_InterruptedException(){
107 dl 1.1 final Exchanger e = new Exchanger();
108     Thread t = new Thread(new Runnable() {
109     public void run(){
110 dl 1.2 try {
111 dl 1.1 e.exchange(one);
112 dl 1.3 threadShouldThrow();
113 dl 1.2 } catch(InterruptedException success){
114 dl 1.1 }
115     }
116     });
117 dl 1.2 try {
118 dl 1.1 t.start();
119     Thread.sleep(SHORT_DELAY_MS);
120     t.interrupt();
121     t.join();
122     } catch(InterruptedException ex) {
123 dl 1.3 unexpectedException();
124 dl 1.1 }
125     }
126    
127 dl 1.3 /**
128     *
129     */
130 dl 1.2 public void testTimedExchange_InterruptedException(){
131 dl 1.1 final Exchanger e = new Exchanger();
132     Thread t = new Thread(new Runnable() {
133     public void run(){
134 dl 1.2 try {
135 dl 1.1 e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
136 dl 1.3 threadShouldThrow();
137 dl 1.1 } catch(InterruptedException success){
138     } catch(Exception e2){
139 dl 1.2 threadFail("should throw IE");
140 dl 1.1 }
141     }
142     });
143 dl 1.2 try {
144 dl 1.1 t.start();
145     t.interrupt();
146     t.join();
147     } catch(InterruptedException ex){
148 dl 1.3 unexpectedException();
149 dl 1.1 }
150     }
151    
152 dl 1.3 /**
153     *
154     */
155 dl 1.2 public void testExchange_TimeOutException(){
156 dl 1.1 final Exchanger e = new Exchanger();
157     Thread t = new Thread(new Runnable() {
158     public void run(){
159 dl 1.2 try {
160 dl 1.1 e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
161 dl 1.3 threadShouldThrow();
162 dl 1.2 } catch(TimeoutException success){
163     } catch(InterruptedException e2){
164     threadFail("should throw TOE");
165 dl 1.1 }
166     }
167     });
168 dl 1.2 try {
169 dl 1.1 t.start();
170     t.join();
171     } catch(InterruptedException ex){
172 dl 1.3 unexpectedException();
173 dl 1.1 }
174     }
175     }