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.2 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Sat Dec 27 19:26:43 2003 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 18 | Line 19 | public class ExchangerTest extends JSR16
19          return new TestSuite(ExchangerTest.class);
20      }
21  
22 +    /**
23 +     * exchange exchanges objects across two threads
24 +     */
25      public void testExchange() {
26          final Exchanger e = new Exchanger();
27          Thread t1 = new Thread(new Runnable(){
# Line 28 | Line 32 | public class ExchangerTest extends JSR16
32                          Object w = e.exchange(v);
33                          threadAssertEquals(w, one);
34                      } catch(InterruptedException e){
35 <                        threadFail("unexpected exception");
35 >                        threadUnexpectedException();
36                      }
37                  }
38              });
# Line 40 | Line 44 | public class ExchangerTest extends JSR16
44                          Object w = e.exchange(v);
45                          threadAssertEquals(w, two);
46                      } catch(InterruptedException e){
47 <                        threadFail("unexpected exception");
47 >                        threadUnexpectedException();
48                      }
49                  }
50              });
# Line 50 | Line 54 | public class ExchangerTest extends JSR16
54              t1.join();
55              t2.join();
56          } catch(InterruptedException ex) {
57 <            fail("unexpected exception");
57 >            unexpectedException();
58          }
59      }
60  
61 +    /**
62 +     * timed exchange exchanges objects across two threads
63 +     */
64      public void testTimedExchange() {
65          final Exchanger e = new Exchanger();
66          Thread t1 = new Thread(new Runnable(){
# Line 64 | Line 71 | public class ExchangerTest extends JSR16
71                          Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
72                          threadAssertEquals(w, one);
73                      } catch(InterruptedException e){
74 <                        threadFail("unexpected exception");
74 >                        threadUnexpectedException();
75                      } catch(TimeoutException toe) {
76 <                        threadFail("unexpected exception");
76 >                        threadUnexpectedException();
77                      }
78                  }
79              });
# Line 78 | Line 85 | public class ExchangerTest extends JSR16
85                          Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
86                          threadAssertEquals(w, two);
87                      } catch(InterruptedException e){
88 <                        threadFail("unexpected exception");
88 >                        threadUnexpectedException();
89                      } catch(TimeoutException toe) {
90 <                        threadFail("unexpected exception");
90 >                        threadUnexpectedException();
91                      }
92                  }
93              });
# Line 90 | Line 97 | public class ExchangerTest extends JSR16
97              t1.join();
98              t2.join();
99          } catch(InterruptedException ex) {
100 <            fail("unexpected exception");
100 >            unexpectedException();
101          }
102      }
103  
104 +    /**
105 +     * interrupt during wait for exchange throws IE
106 +     */
107      public void testExchange_InterruptedException(){
108          final Exchanger e = new Exchanger();
109          Thread t = new Thread(new Runnable() {
110                  public void run(){
111                      try {
112                          e.exchange(one);
113 <                        threadFail("should throw");
113 >                        threadShouldThrow();
114                      } catch(InterruptedException success){
115                      }
116                  }
# Line 111 | Line 121 | public class ExchangerTest extends JSR16
121              t.interrupt();
122              t.join();
123          } catch(InterruptedException ex) {
124 <            fail("unexpected exception");
124 >            unexpectedException();
125          }
126      }
127  
128 +    /**
129 +     * interrupt during wait for timed exchange throws IE
130 +     */
131      public void testTimedExchange_InterruptedException(){
132          final Exchanger e = new Exchanger();
133          Thread t = new Thread(new Runnable() {
134                  public void run(){
135                      try {
136                          e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
137 <                        threadFail("should throw");
137 >                        threadShouldThrow();
138                      } catch(InterruptedException success){
139                      } catch(Exception e2){
140                          threadFail("should throw IE");
# Line 133 | Line 146 | public class ExchangerTest extends JSR16
146              t.interrupt();
147              t.join();
148          } catch(InterruptedException ex){
149 <            fail("unexpected exception");
149 >            unexpectedException();
150          }
151      }
152  
153 +    /**
154 +     * timeout during wait for timed exchange throws TOE
155 +     */
156      public void testExchange_TimeOutException(){
157          final Exchanger e = new Exchanger();
158          Thread t = new Thread(new Runnable() {
159                  public void run(){
160                      try {
161                          e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
162 <                        threadFail("should throw");
162 >                        threadShouldThrow();
163                      } catch(TimeoutException success){
164                      } catch(InterruptedException e2){
165                          threadFail("should throw TOE");
# Line 154 | Line 170 | public class ExchangerTest extends JSR16
170              t.start();
171              t.join();
172          } catch(InterruptedException ex){
173 <            fail("unexpected exception");
173 >            unexpectedException();
174          }
175      }
176 +
177 +    /**
178 +     * If one exchanging thread is interrupted, another succeeds.
179 +     */
180 +    public void testReplacementAfterExchange() {
181 +        final Exchanger e = new Exchanger();
182 +        Thread t1 = new Thread(new Runnable(){
183 +                public void run(){
184 +                    try {
185 +                        Object v = e.exchange(one);
186 +                        threadAssertEquals(v, two);
187 +                        Object w = e.exchange(v);
188 +                        threadShouldThrow();
189 +                    } catch(InterruptedException success){
190 +                    }
191 +                }
192 +            });
193 +        Thread t2 = new Thread(new Runnable(){
194 +                public void run(){
195 +                    try {
196 +                        Object v = e.exchange(two);
197 +                        threadAssertEquals(v, one);
198 +                        Thread.sleep(SMALL_DELAY_MS);
199 +                        Object w = e.exchange(v);
200 +                        threadAssertEquals(w, three);
201 +                    } catch(InterruptedException e){
202 +                        threadUnexpectedException();
203 +                    }
204 +                }
205 +            });
206 +        Thread t3 = new Thread(new Runnable(){
207 +                public void run(){
208 +                    try {
209 +                        Thread.sleep(SMALL_DELAY_MS);
210 +                        Object w = e.exchange(three);
211 +                        threadAssertEquals(w, one);
212 +                    } catch(InterruptedException e){
213 +                        threadUnexpectedException();
214 +                    }
215 +                }
216 +            });
217 +
218 +        try {
219 +            t1.start();
220 +            t2.start();
221 +            t3.start();
222 +            Thread.sleep(SHORT_DELAY_MS);
223 +            t1.interrupt();
224 +            t1.join();
225 +            t2.join();
226 +            t3.join();
227 +        } catch(InterruptedException ex) {
228 +            unexpectedException();
229 +        }
230 +    }
231 +
232   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines