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.7 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.16 by dl, Fri May 6 11:22:07 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13  
14   public class ExchangerTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ExchangerTest.class);
20 >        return new TestSuite(ExchangerTest.class);
21      }
22  
23      /**
24       * exchange exchanges objects across two threads
25       */
26 <    public void testExchange() {
26 >    public void testExchange() throws InterruptedException {
27          final Exchanger e = new Exchanger();
28 <        Thread t1 = new Thread(new Runnable(){
29 <                public void run(){
30 <                    try {
31 <                        Object v = e.exchange(one);
32 <                        threadAssertEquals(v, two);
33 <                        Object w = e.exchange(v);
34 <                        threadAssertEquals(w, one);
35 <                    } catch (InterruptedException e){
36 <                        threadUnexpectedException();
37 <                    }
38 <                }
39 <            });
40 <        Thread t2 = new Thread(new Runnable(){
41 <                public void run(){
42 <                    try {
42 <                        Object v = e.exchange(two);
43 <                        threadAssertEquals(v, one);
44 <                        Object w = e.exchange(v);
45 <                        threadAssertEquals(w, two);
46 <                    } catch (InterruptedException e){
47 <                        threadUnexpectedException();
48 <                    }
49 <                }
50 <            });
51 <        try {
52 <            t1.start();
53 <            t2.start();
54 <            t1.join();
55 <            t2.join();
56 <        } catch (InterruptedException ex) {
57 <            unexpectedException();
58 <        }
28 >        Thread t1 = new Thread(new CheckedRunnable() {
29 >            public void realRun() throws InterruptedException {
30 >                assertSame(one, e.exchange(two));
31 >                assertSame(two, e.exchange(one));
32 >            }});
33 >        Thread t2 = new Thread(new CheckedRunnable() {
34 >            public void realRun() throws InterruptedException {
35 >                assertSame(two, e.exchange(one));
36 >                assertSame(one, e.exchange(two));
37 >            }});
38 >
39 >        t1.start();
40 >        t2.start();
41 >        t1.join();
42 >        t2.join();
43      }
44  
45      /**
46       * timed exchange exchanges objects across two threads
47       */
48 <    public void testTimedExchange() {
48 >    public void testTimedExchange() throws InterruptedException {
49          final Exchanger e = new Exchanger();
50 <        Thread t1 = new Thread(new Runnable(){
51 <                public void run(){
52 <                    try {
53 <                        Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
54 <                        threadAssertEquals(v, two);
55 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
56 <                        threadAssertEquals(w, one);
57 <                    } catch (InterruptedException e){
58 <                        threadUnexpectedException();
59 <                    } catch (TimeoutException toe) {
60 <                        threadUnexpectedException();
61 <                    }
62 <                }
63 <            });
64 <        Thread t2 = new Thread(new Runnable(){
81 <                public void run(){
82 <                    try {
83 <                        Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
84 <                        threadAssertEquals(v, one);
85 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
86 <                        threadAssertEquals(w, two);
87 <                    } catch (InterruptedException e){
88 <                        threadUnexpectedException();
89 <                    } catch (TimeoutException toe) {
90 <                        threadUnexpectedException();
91 <                    }
92 <                }
93 <            });
94 <        try {
95 <            t1.start();
96 <            t2.start();
97 <            t1.join();
98 <            t2.join();
99 <        } catch (InterruptedException ex) {
100 <            unexpectedException();
101 <        }
50 >        Thread t1 = new Thread(new CheckedRunnable() {
51 >            public void realRun() throws Exception {
52 >                assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS));
53 >                assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS));
54 >            }});
55 >        Thread t2 = new Thread(new CheckedRunnable() {
56 >            public void realRun() throws Exception {
57 >                assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS));
58 >                assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS));
59 >            }});
60 >
61 >        t1.start();
62 >        t2.start();
63 >        t1.join();
64 >        t2.join();
65      }
66  
67      /**
68       * interrupt during wait for exchange throws IE
69       */
70 <    public void testExchange_InterruptedException(){
70 >    public void testExchange_InterruptedException() throws InterruptedException {
71          final Exchanger e = new Exchanger();
72 <        Thread t = new Thread(new Runnable() {
73 <                public void run(){
74 <                    try {
75 <                        e.exchange(one);
76 <                        threadShouldThrow();
77 <                    } catch (InterruptedException success){
78 <                    }
79 <                }
80 <            });
118 <        try {
119 <            t.start();
120 <            Thread.sleep(SHORT_DELAY_MS);
121 <            t.interrupt();
122 <            t.join();
123 <        } catch (InterruptedException ex) {
124 <            unexpectedException();
125 <        }
72 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
73 >            public void realRun() throws InterruptedException {
74 >                e.exchange(one);
75 >            }});
76 >
77 >        t.start();
78 >        delay(SHORT_DELAY_MS);
79 >        t.interrupt();
80 >        t.join();
81      }
82  
83      /**
84       * interrupt during wait for timed exchange throws IE
85       */
86 <    public void testTimedExchange_InterruptedException(){
86 >    public void testTimedExchange_InterruptedException() throws InterruptedException {
87          final Exchanger e = new Exchanger();
88 <        Thread t = new Thread(new Runnable() {
89 <                public void run(){
90 <                    try {
91 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
92 <                        threadShouldThrow();
93 <                    } catch (InterruptedException success){
94 <                    } catch (Exception e2){
95 <                        threadFail("should throw IE");
96 <                    }
142 <                }
143 <            });
144 <        try {
145 <            t.start();
146 <            t.interrupt();
147 <            t.join();
148 <        } catch (InterruptedException ex){
149 <            unexpectedException();
150 <        }
88 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
89 >            public void realRun() throws Exception {
90 >                e.exchange(null, SMALL_DELAY_MS, MILLISECONDS);
91 >            }});
92 >
93 >        t.start();
94 >        delay(SHORT_DELAY_MS);
95 >        t.interrupt();
96 >        t.join();
97      }
98  
99      /**
100       * timeout during wait for timed exchange throws TOE
101       */
102 <    public void testExchange_TimeOutException(){
102 >    public void testExchange_TimeOutException() throws InterruptedException {
103          final Exchanger e = new Exchanger();
104 <        Thread t = new Thread(new Runnable() {
105 <                public void run(){
106 <                    try {
107 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
108 <                        threadShouldThrow();
109 <                    } catch (TimeoutException success){
110 <                    } catch (InterruptedException e2){
165 <                        threadFail("should throw TOE");
166 <                    }
167 <                }
168 <            });
169 <        try {
170 <            t.start();
171 <            t.join();
172 <        } catch (InterruptedException ex){
173 <            unexpectedException();
174 <        }
104 >        Thread t = new ThreadShouldThrow(TimeoutException.class) {
105 >            public void realRun() throws Exception {
106 >                e.exchange(null, SHORT_DELAY_MS, MILLISECONDS);
107 >            }};
108 >
109 >        t.start();
110 >        t.join();
111      }
112  
113      /**
114       * If one exchanging thread is interrupted, another succeeds.
115       */
116 <    public void testReplacementAfterExchange() {
116 >    public void testReplacementAfterExchange() throws InterruptedException {
117          final Exchanger e = new Exchanger();
118 <        Thread t1 = new Thread(new Runnable(){
119 <                public void run(){
120 <                    try {
121 <                        Object v = e.exchange(one);
122 <                        threadAssertEquals(v, two);
123 <                        Object w = e.exchange(v);
124 <                        threadShouldThrow();
125 <                    } catch (InterruptedException success){
126 <                    }
127 <                }
128 <            });
129 <        Thread t2 = new Thread(new Runnable(){
130 <                public void run(){
131 <                    try {
132 <                        Object v = e.exchange(two);
133 <                        threadAssertEquals(v, one);
134 <                        Thread.sleep(SMALL_DELAY_MS);
135 <                        Object w = e.exchange(v);
136 <                        threadAssertEquals(w, three);
137 <                    } catch (InterruptedException e){
138 <                        threadUnexpectedException();
139 <                    }
140 <                }
141 <            });
142 <        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 <        }
118 >        Thread t1 = new Thread(new CheckedInterruptedRunnable() {
119 >            public void realRun() throws InterruptedException {
120 >                assertSame(two, e.exchange(one));
121 >                e.exchange(two);
122 >            }});
123 >        Thread t2 = new Thread(new CheckedRunnable() {
124 >            public void realRun() throws InterruptedException {
125 >                assertSame(one, e.exchange(two));
126 >                delay(SMALL_DELAY_MS);
127 >                assertSame(three, e.exchange(one));
128 >            }});
129 >        Thread t3 = new Thread(new CheckedRunnable() {
130 >            public void realRun() throws InterruptedException {
131 >                delay(SMALL_DELAY_MS);
132 >                assertSame(one, e.exchange(three));
133 >            }});
134 >
135 >        t1.start();
136 >        t2.start();
137 >        t3.start();
138 >        delay(SHORT_DELAY_MS);
139 >        t1.interrupt();
140 >        t1.join();
141 >        t2.join();
142 >        t3.join();
143      }
144  
145   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines