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

Comparing jsr166/src/test/tck/ExchangerTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.4 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 9 | Line 9 | import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11  
12 < public class ExchangerTest extends TestCase{
12 > public class ExchangerTest extends JSR166TestCase {
13    
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
# Line 18 | Line 18 | public class ExchangerTest extends TestC
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(){
21 >    /**
22 >     * exchange exchanges objects across two threads
23 >     */
24 >    public void testExchange() {
25          final Exchanger e = new Exchanger();
26          Thread t1 = new Thread(new Runnable(){
27                  public void run(){
28 <                    try{
28 >                    try {
29                          Object v = e.exchange(one);
30 <                        assertEquals(v, two);
30 >                        threadAssertEquals(v, two);
31                          Object w = e.exchange(v);
32 <                        assertEquals(w, one);
33 <                    }catch(InterruptedException e){
34 <                        fail("unexpected exception");
32 >                        threadAssertEquals(w, one);
33 >                    } catch(InterruptedException e){
34 >                        threadUnexpectedException();
35                      }
36                  }
37              });
38          Thread t2 = new Thread(new Runnable(){
39                  public void run(){
40 <                    try{
40 >                    try {
41                          Object v = e.exchange(two);
42 <                        assertEquals(v, one);
42 >                        threadAssertEquals(v, one);
43                          Object w = e.exchange(v);
44 <                        assertEquals(w, two);
45 <                    }catch(InterruptedException e){
46 <                        fail("unexpected exception");
44 >                        threadAssertEquals(w, two);
45 >                    } catch(InterruptedException e){
46 >                        threadUnexpectedException();
47 >                    }
48 >                }
49 >            });
50 >        try {
51 >            t1.start();
52 >            t2.start();
53 >            t1.join();
54 >            t2.join();
55 >        } catch(InterruptedException ex) {
56 >            unexpectedException();
57 >        }
58 >    }
59 >
60 >    /**
61 >     * timed exchange exchanges objects across two threads
62 >     */
63 >    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 >                        threadUnexpectedException();
74 >                    } catch(TimeoutException toe) {
75 >                        threadUnexpectedException();
76 >                    }
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 >                        threadUnexpectedException();
88 >                    } catch(TimeoutException toe) {
89 >                        threadUnexpectedException();
90                      }
91                  }
92              });
# Line 57 | Line 96 | public class ExchangerTest extends TestC
96              t1.join();
97              t2.join();
98          } catch(InterruptedException ex) {
99 <            fail("unexpected exception");
99 >            unexpectedException();
100          }
101      }
102  
103 <    public void testExchange1_InterruptedException(){
103 >    /**
104 >     * interrupt during wait for exchange throws IE
105 >     */
106 >    public void testExchange_InterruptedException(){
107          final Exchanger e = new Exchanger();
108          Thread t = new Thread(new Runnable() {
109                  public void run(){
110 <                    try{
110 >                    try {
111                          e.exchange(one);
112 <                        fail("should throw");
113 <                    }catch(InterruptedException success){
112 >                        threadShouldThrow();
113 >                    } catch(InterruptedException success){
114                      }
115                  }
116              });
117 <        try{
117 >        try {
118              t.start();
119              Thread.sleep(SHORT_DELAY_MS);
120              t.interrupt();
121              t.join();
122          } catch(InterruptedException ex) {
123 <            fail("unexpected exception");
123 >            unexpectedException();
124          }
125      }
126  
127 <    public void testExchange2_InterruptedException(){
127 >    /**
128 >     * interrupt during wait for timed exchange throws IE
129 >     */
130 >    public void testTimedExchange_InterruptedException(){
131          final Exchanger e = new Exchanger();
132          Thread t = new Thread(new Runnable() {
133                  public void run(){
134 <                    try{
134 >                    try {
135                          e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
136 <                        fail("should throw");
136 >                        threadShouldThrow();
137                      } catch(InterruptedException success){
138                      } catch(Exception e2){
139 <                        fail("should throw IE");
139 >                        threadFail("should throw IE");
140                      }
141                  }
142              });
143 <        try{
143 >        try {
144              t.start();
145              t.interrupt();
146              t.join();
147          } catch(InterruptedException ex){
148 <            fail("unexpected exception");
148 >            unexpectedException();
149          }
150      }
151  
152 <    public void testExchange3_TimeOutException(){
152 >    /**
153 >     * timeout during wait for timed exchange throws TOE
154 >     */
155 >    public void testExchange_TimeOutException(){
156          final Exchanger e = new Exchanger();
157          Thread t = new Thread(new Runnable() {
158                  public void run(){
159 <                    try{
159 >                    try {
160                          e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
161 <                        fail("should throw");
162 <                    }catch(TimeoutException success){}
163 <                    catch(InterruptedException e2){
164 <                        fail("should throw TOE");
161 >                        threadShouldThrow();
162 >                    } catch(TimeoutException success){
163 >                    } catch(InterruptedException e2){
164 >                        threadFail("should throw TOE");
165                      }
166                  }
167              });
168 <        try{
168 >        try {
169              t.start();
170              t.join();
171          } catch(InterruptedException ex){
172 <            fail("unexpected exception");
172 >            unexpectedException();
173 >        }
174 >    }
175 >
176 >    /**
177 >     * If one exchanging thread is interrupted, another succeeds.
178 >     */
179 >    public void testReplacementAfterExchange() {
180 >        final Exchanger e = new Exchanger();
181 >        Thread t1 = new Thread(new Runnable(){
182 >                public void run(){
183 >                    try {
184 >                        Object v = e.exchange(one);
185 >                        threadAssertEquals(v, two);
186 >                        Object w = e.exchange(v);
187 >                        threadShouldThrow();
188 >                    } catch(InterruptedException success){
189 >                    }
190 >                }
191 >            });
192 >        Thread t2 = new Thread(new Runnable(){
193 >                public void run(){
194 >                    try {
195 >                        Object v = e.exchange(two);
196 >                        threadAssertEquals(v, one);
197 >                        Thread.sleep(SMALL_DELAY_MS);
198 >                        Object w = e.exchange(v);
199 >                        threadAssertEquals(w, three);
200 >                    } catch(InterruptedException e){
201 >                        threadUnexpectedException();
202 >                    }
203 >                }
204 >            });
205 >        Thread t3 = new Thread(new Runnable(){
206 >                public void run(){
207 >                    try {
208 >                        Thread.sleep(SMALL_DELAY_MS);
209 >                        Object w = e.exchange(three);
210 >                        threadAssertEquals(w, one);
211 >                    } catch(InterruptedException e){
212 >                        threadUnexpectedException();
213 >                    }
214 >                }
215 >            });
216 >
217 >        try {
218 >            t1.start();
219 >            t2.start();
220 >            t3.start();
221 >            Thread.sleep(SHORT_DELAY_MS);
222 >            t1.interrupt();
223 >            t1.join();
224 >            t2.join();
225 >            t3.join();
226 >        } catch(InterruptedException ex) {
227 >            unexpectedException();
228          }
229      }
230 +
231   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines