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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.16 by dl, Fri May 6 11:22:07 2011 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/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
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 <  
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() throws InterruptedException {
27 >        final Exchanger e = new Exchanger();
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() throws InterruptedException {
49 >        final Exchanger e = new Exchanger();
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() throws InterruptedException {
71 >        final Exchanger e = new Exchanger();
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() throws InterruptedException {
87 >        final Exchanger e = new Exchanger();
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 <     *
100 >     * timeout during wait for timed exchange throws TOE
101       */
102 <    public void testExchange() {
102 >    public void testExchange_TimeOutException() throws InterruptedException {
103          final Exchanger e = new Exchanger();
104 <        Thread t1 = new Thread(new Runnable(){
105 <                public void run(){
106 <                    try {
107 <                        Object v = e.exchange(one);
108 <                        threadAssertEquals(v, two);
109 <                        Object w = e.exchange(v);
110 <                        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 {
41 <                        Object v = e.exchange(two);
42 <                        threadAssertEquals(v, one);
43 <                        Object w = e.exchange(v);
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 <     *
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 <            });
93 <        try {
94 <            t1.start();
95 <            t2.start();
96 <            t1.join();
97 <            t2.join();
98 <        } catch(InterruptedException ex) {
99 <            unexpectedException();
100 <        }
101 <    }
102 <
103 <    /**
104 <     *
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 {
111 <                        e.exchange(one);
112 <                        threadShouldThrow();
113 <                    } catch(InterruptedException success){
114 <                    }
115 <                }
116 <            });
117 <        try {
118 <            t.start();
119 <            Thread.sleep(SHORT_DELAY_MS);
120 <            t.interrupt();
121 <            t.join();
122 <        } catch(InterruptedException ex) {
123 <            unexpectedException();
124 <        }
125 <    }
126 <
127 <    /**
128 <     *
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 {
135 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
136 <                        threadShouldThrow();
137 <                    } catch(InterruptedException success){
138 <                    } catch(Exception e2){
139 <                        threadFail("should throw IE");
140 <                    }
141 <                }
142 <            });
143 <        try {
144 <            t.start();
145 <            t.interrupt();
146 <            t.join();
147 <        } catch(InterruptedException ex){
148 <            unexpectedException();
149 <        }
150 <    }
151 <
152 <    /**
153 <     *
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 {
160 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
161 <                        threadShouldThrow();
162 <                    } catch(TimeoutException success){
163 <                    } catch(InterruptedException e2){
164 <                        threadFail("should throw TOE");
165 <                    }
166 <                }
167 <            });
168 <        try {
169 <            t.start();
170 <            t.join();
171 <        } catch(InterruptedException ex){
172 <            unexpectedException();
173 <        }
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() throws InterruptedException {
117 +        final Exchanger e = new Exchanger();
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