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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines