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.12 by jsr166, Mon Nov 30 08:31:09 2009 UTC

# 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 {
43 <                        Object v = e.exchange(two);
44 <                        threadAssertEquals(v, one);
45 <                        Object w = e.exchange(v);
46 <                        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 >                Object v = e.exchange(one);
31 >                threadAssertEquals(v, two);
32 >                Object w = e.exchange(v);
33 >                threadAssertEquals(w, one);
34 >            }});
35 >        Thread t2 = new Thread(new CheckedRunnable() {
36 >            public void realRun() throws InterruptedException {
37 >                Object v = e.exchange(two);
38 >                threadAssertEquals(v, one);
39 >                Object w = e.exchange(v);
40 >                threadAssertEquals(w, two);
41 >            }});
42 >
43 >        t1.start();
44 >        t2.start();
45 >        t1.join();
46 >        t2.join();
47      }
48  
49      /**
50       * timed exchange exchanges objects across two threads
51       */
52 <    public void testTimedExchange() {
52 >    public void testTimedExchange() throws InterruptedException {
53          final Exchanger e = new Exchanger();
54 <        Thread t1 = new Thread(new Runnable(){
55 <                public void run(){
56 <                    try {
57 <                        Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
58 <                        threadAssertEquals(v, two);
59 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
60 <                        threadAssertEquals(w, one);
61 <                    } catch (InterruptedException e){
62 <                        threadUnexpectedException();
63 <                    } catch (TimeoutException toe) {
64 <                        threadUnexpectedException();
65 <                    }
66 <                }
67 <            });
68 <        Thread t2 = new Thread(new Runnable(){
69 <                public void run(){
70 <                    try {
71 <                        Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
72 <                        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 <        }
54 >        Thread t1 = new Thread(new CheckedRunnable() {
55 >            public void realRun() throws Exception {
56 >                Object v = e.exchange(one, SHORT_DELAY_MS, MILLISECONDS);
57 >                threadAssertEquals(v, two);
58 >                Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS);
59 >                threadAssertEquals(w, one);
60 >            }});
61 >        Thread t2 = new Thread(new CheckedRunnable() {
62 >            public void realRun() throws Exception {
63 >                Object v = e.exchange(two, SHORT_DELAY_MS, MILLISECONDS);
64 >                threadAssertEquals(v, one);
65 >                Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS);
66 >                threadAssertEquals(w, two);
67 >            }});
68 >
69 >        t1.start();
70 >        t2.start();
71 >        t1.join();
72 >        t2.join();
73      }
74  
75      /**
76       * interrupt during wait for exchange throws IE
77       */
78 <    public void testExchange_InterruptedException(){
78 >    public void testExchange_InterruptedException() throws InterruptedException {
79          final Exchanger e = new Exchanger();
80 <        Thread t = new Thread(new Runnable() {
81 <                public void run(){
82 <                    try {
83 <                        e.exchange(one);
84 <                        threadShouldThrow();
85 <                    } catch (InterruptedException success){
86 <                    }
87 <                }
88 <            });
118 <        try {
119 <            t.start();
120 <            Thread.sleep(SHORT_DELAY_MS);
121 <            t.interrupt();
122 <            t.join();
123 <        } catch (InterruptedException ex) {
124 <            unexpectedException();
125 <        }
80 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
81 >            public void realRun() throws InterruptedException {
82 >                e.exchange(one);
83 >            }});
84 >
85 >        t.start();
86 >        Thread.sleep(SHORT_DELAY_MS);
87 >        t.interrupt();
88 >        t.join();
89      }
90  
91      /**
92       * interrupt during wait for timed exchange throws IE
93       */
94 <    public void testTimedExchange_InterruptedException(){
94 >    public void testTimedExchange_InterruptedException() throws InterruptedException {
95          final Exchanger e = new Exchanger();
96 <        Thread t = new Thread(new Runnable() {
97 <                public void run(){
98 <                    try {
99 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
100 <                        threadShouldThrow();
101 <                    } catch (InterruptedException success){
102 <                    } catch (Exception e2){
103 <                        threadFail("should throw IE");
104 <                    }
142 <                }
143 <            });
144 <        try {
145 <            t.start();
146 <            t.interrupt();
147 <            t.join();
148 <        } catch (InterruptedException ex){
149 <            unexpectedException();
150 <        }
96 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
97 >            public void realRun() throws Exception {
98 >                e.exchange(null, MEDIUM_DELAY_MS, MILLISECONDS);
99 >            }});
100 >
101 >        t.start();
102 >        Thread.sleep(SHORT_DELAY_MS);
103 >        t.interrupt();
104 >        t.join();
105      }
106  
107      /**
108       * timeout during wait for timed exchange throws TOE
109       */
110 <    public void testExchange_TimeOutException(){
110 >    public void testExchange_TimeOutException() throws InterruptedException {
111          final Exchanger e = new Exchanger();
112 <        Thread t = new Thread(new Runnable() {
113 <                public void run(){
114 <                    try {
115 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
116 <                        threadShouldThrow();
117 <                    } catch (TimeoutException success){
118 <                    } catch (InterruptedException e2){
119 <                        threadFail("should throw TOE");
120 <                    }
121 <                }
168 <            });
169 <        try {
170 <            t.start();
171 <            t.join();
172 <        } catch (InterruptedException ex){
173 <            unexpectedException();
174 <        }
112 >        Thread t = new Thread(new CheckedRunnable() {
113 >            public void realRun() throws Exception {
114 >                try {
115 >                    e.exchange(null, SHORT_DELAY_MS, MILLISECONDS);
116 >                    threadShouldThrow();
117 >                } catch (TimeoutException success) {}
118 >            }});
119 >
120 >        t.start();
121 >        t.join();
122      }
123  
124      /**
125       * If one exchanging thread is interrupted, another succeeds.
126       */
127 <    public void testReplacementAfterExchange() {
127 >    public void testReplacementAfterExchange() throws InterruptedException {
128          final Exchanger e = new Exchanger();
129 <        Thread t1 = new Thread(new Runnable(){
130 <                public void run(){
131 <                    try {
132 <                        Object v = e.exchange(one);
133 <                        threadAssertEquals(v, two);
134 <                        Object w = e.exchange(v);
135 <                        threadShouldThrow();
136 <                    } catch (InterruptedException success){
137 <                    }
138 <                }
139 <            });
140 <        Thread t2 = new Thread(new Runnable(){
141 <                public void run(){
142 <                    try {
143 <                        Object v = e.exchange(two);
144 <                        threadAssertEquals(v, one);
145 <                        Thread.sleep(SMALL_DELAY_MS);
146 <                        Object w = e.exchange(v);
147 <                        threadAssertEquals(w, three);
148 <                    } catch (InterruptedException e){
149 <                        threadUnexpectedException();
150 <                    }
151 <                }
152 <            });
153 <        Thread t3 = new Thread(new Runnable(){
154 <                public void run(){
155 <                    try {
156 <                        Thread.sleep(SMALL_DELAY_MS);
157 <                        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 <        }
129 >        Thread t1 = new Thread(new CheckedInterruptedRunnable() {
130 >            public void realRun() throws InterruptedException {
131 >                Object v = e.exchange(one);
132 >                threadAssertEquals(v, two);
133 >                Object w = e.exchange(v);
134 >            }});
135 >        Thread t2 = new Thread(new CheckedRunnable() {
136 >            public void realRun() throws InterruptedException {
137 >                Object v = e.exchange(two);
138 >                threadAssertEquals(v, one);
139 >                Thread.sleep(SMALL_DELAY_MS);
140 >                Object w = e.exchange(v);
141 >                threadAssertEquals(w, three);
142 >            }});
143 >        Thread t3 = new Thread(new CheckedRunnable() {
144 >            public void realRun() throws InterruptedException {
145 >                Thread.sleep(SMALL_DELAY_MS);
146 >                Object w = e.exchange(three);
147 >                threadAssertEquals(w, one);
148 >            }});
149 >
150 >        t1.start();
151 >        t2.start();
152 >        t3.start();
153 >        Thread.sleep(SHORT_DELAY_MS);
154 >        t1.interrupt();
155 >        t1.join();
156 >        t2.join();
157 >        t3.join();
158      }
159  
160   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines