ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.12
Committed: Mon Nov 30 08:31:09 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +1 -0 lines
Log Message:
replace absolute waits with _DELAY_MS; 1000 => 1000L; short delay after starting a thread before interrupting it

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.5 * 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 jsr166 1.6 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.11 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.1
14 dl 1.2 public class ExchangerTest extends JSR166TestCase {
15 jsr166 1.6
16 dl 1.1 public static void main(String[] args) {
17 jsr166 1.10 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
19     public static Test suite() {
20 jsr166 1.10 return new TestSuite(ExchangerTest.class);
21 dl 1.1 }
22    
23 dl 1.3 /**
24 dl 1.4 * exchange exchanges objects across two threads
25 dl 1.3 */
26 jsr166 1.9 public void testExchange() throws InterruptedException {
27 dl 1.1 final Exchanger e = new Exchanger();
28 jsr166 1.10 Thread t1 = new Thread(new CheckedRunnable() {
29 jsr166 1.9 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 jsr166 1.10 Thread t2 = new Thread(new CheckedRunnable() {
36 jsr166 1.9 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 dl 1.2 }
48    
49 dl 1.3 /**
50 dl 1.4 * timed exchange exchanges objects across two threads
51 dl 1.3 */
52 jsr166 1.9 public void testTimedExchange() throws InterruptedException {
53 dl 1.2 final Exchanger e = new Exchanger();
54 jsr166 1.10 Thread t1 = new Thread(new CheckedRunnable() {
55 jsr166 1.9 public void realRun() throws Exception {
56 jsr166 1.11 Object v = e.exchange(one, SHORT_DELAY_MS, MILLISECONDS);
57 jsr166 1.9 threadAssertEquals(v, two);
58 jsr166 1.11 Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS);
59 jsr166 1.9 threadAssertEquals(w, one);
60     }});
61 jsr166 1.10 Thread t2 = new Thread(new CheckedRunnable() {
62 jsr166 1.9 public void realRun() throws Exception {
63 jsr166 1.11 Object v = e.exchange(two, SHORT_DELAY_MS, MILLISECONDS);
64 jsr166 1.9 threadAssertEquals(v, one);
65 jsr166 1.11 Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS);
66 jsr166 1.9 threadAssertEquals(w, two);
67     }});
68    
69     t1.start();
70     t2.start();
71     t1.join();
72     t2.join();
73 dl 1.1 }
74    
75 dl 1.3 /**
76 dl 1.4 * interrupt during wait for exchange throws IE
77 dl 1.3 */
78 jsr166 1.9 public void testExchange_InterruptedException() throws InterruptedException {
79 dl 1.1 final Exchanger e = new Exchanger();
80 jsr166 1.10 Thread t = new Thread(new CheckedInterruptedRunnable() {
81 jsr166 1.9 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 dl 1.1 }
90    
91 dl 1.3 /**
92 dl 1.4 * interrupt during wait for timed exchange throws IE
93 dl 1.3 */
94 jsr166 1.9 public void testTimedExchange_InterruptedException() throws InterruptedException {
95 dl 1.1 final Exchanger e = new Exchanger();
96 jsr166 1.10 Thread t = new Thread(new CheckedInterruptedRunnable() {
97 jsr166 1.9 public void realRun() throws Exception {
98 jsr166 1.11 e.exchange(null, MEDIUM_DELAY_MS, MILLISECONDS);
99 jsr166 1.9 }});
100    
101     t.start();
102 jsr166 1.12 Thread.sleep(SHORT_DELAY_MS);
103 jsr166 1.9 t.interrupt();
104     t.join();
105 dl 1.1 }
106    
107 dl 1.3 /**
108 dl 1.4 * timeout during wait for timed exchange throws TOE
109 dl 1.3 */
110 jsr166 1.9 public void testExchange_TimeOutException() throws InterruptedException {
111 dl 1.1 final Exchanger e = new Exchanger();
112 jsr166 1.10 Thread t = new Thread(new CheckedRunnable() {
113 jsr166 1.9 public void realRun() throws Exception {
114     try {
115 jsr166 1.11 e.exchange(null, SHORT_DELAY_MS, MILLISECONDS);
116 jsr166 1.9 threadShouldThrow();
117     } catch (TimeoutException success) {}
118     }});
119    
120     t.start();
121     t.join();
122 dl 1.1 }
123 dl 1.4
124     /**
125     * If one exchanging thread is interrupted, another succeeds.
126     */
127 jsr166 1.9 public void testReplacementAfterExchange() throws InterruptedException {
128 dl 1.4 final Exchanger e = new Exchanger();
129 jsr166 1.10 Thread t1 = new Thread(new CheckedInterruptedRunnable() {
130 jsr166 1.9 public void realRun() throws InterruptedException {
131     Object v = e.exchange(one);
132     threadAssertEquals(v, two);
133     Object w = e.exchange(v);
134     }});
135 jsr166 1.10 Thread t2 = new Thread(new CheckedRunnable() {
136 jsr166 1.9 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 jsr166 1.10 Thread t3 = new Thread(new CheckedRunnable() {
144 jsr166 1.9 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 dl 1.4 }
159    
160 dl 1.1 }