ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.19
Committed: Sun May 22 20:05:57 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +1 -1 lines
Log Message:
Fix flaky test testReplacementAfterExchange

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 jsr166 1.15 * http://creativecommons.org/publicdomain/zero/1.0/
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.14 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.17 public void testExchange() {
27 dl 1.1 final Exchanger e = new Exchanger();
28 jsr166 1.17 Thread t1 = newStartedThread(new CheckedRunnable() {
29 jsr166 1.9 public void realRun() throws InterruptedException {
30 jsr166 1.13 assertSame(one, e.exchange(two));
31     assertSame(two, e.exchange(one));
32 jsr166 1.9 }});
33 jsr166 1.17 Thread t2 = newStartedThread(new CheckedRunnable() {
34 jsr166 1.9 public void realRun() throws InterruptedException {
35 jsr166 1.13 assertSame(two, e.exchange(one));
36     assertSame(one, e.exchange(two));
37 jsr166 1.9 }});
38    
39 jsr166 1.17 awaitTermination(t1);
40     awaitTermination(t2);
41 dl 1.2 }
42    
43 dl 1.3 /**
44 dl 1.4 * timed exchange exchanges objects across two threads
45 dl 1.3 */
46 jsr166 1.17 public void testTimedExchange() {
47 dl 1.2 final Exchanger e = new Exchanger();
48 jsr166 1.17 Thread t1 = newStartedThread(new CheckedRunnable() {
49 jsr166 1.9 public void realRun() throws Exception {
50 jsr166 1.17 assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
51     assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
52 jsr166 1.9 }});
53 jsr166 1.17 Thread t2 = newStartedThread(new CheckedRunnable() {
54 jsr166 1.9 public void realRun() throws Exception {
55 jsr166 1.17 assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
56     assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
57 jsr166 1.9 }});
58    
59 jsr166 1.17 awaitTermination(t1);
60     awaitTermination(t2);
61 dl 1.1 }
62    
63 dl 1.3 /**
64 dl 1.4 * interrupt during wait for exchange throws IE
65 dl 1.3 */
66 jsr166 1.17 public void testExchange_InterruptedException() {
67 dl 1.1 final Exchanger e = new Exchanger();
68 jsr166 1.17 final CountDownLatch threadStarted = new CountDownLatch(1);
69     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
70 jsr166 1.9 public void realRun() throws InterruptedException {
71 jsr166 1.17 threadStarted.countDown();
72 jsr166 1.9 e.exchange(one);
73     }});
74    
75 jsr166 1.17 await(threadStarted);
76 jsr166 1.9 t.interrupt();
77 jsr166 1.17 awaitTermination(t);
78 dl 1.1 }
79    
80 dl 1.3 /**
81 dl 1.4 * interrupt during wait for timed exchange throws IE
82 dl 1.3 */
83 jsr166 1.17 public void testTimedExchange_InterruptedException() {
84 dl 1.1 final Exchanger e = new Exchanger();
85 jsr166 1.17 final CountDownLatch threadStarted = new CountDownLatch(1);
86     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
87 jsr166 1.9 public void realRun() throws Exception {
88 jsr166 1.17 threadStarted.countDown();
89     e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
90 jsr166 1.9 }});
91    
92 jsr166 1.17 await(threadStarted);
93 jsr166 1.9 t.interrupt();
94 jsr166 1.17 awaitTermination(t);
95 dl 1.1 }
96    
97 dl 1.3 /**
98 jsr166 1.17 * timeout during wait for timed exchange throws TimeoutException
99 dl 1.3 */
100 jsr166 1.17 public void testExchange_TimeoutException() {
101 dl 1.1 final Exchanger e = new Exchanger();
102 jsr166 1.17 Thread t = newStartedThread(new CheckedRunnable() {
103 jsr166 1.9 public void realRun() throws Exception {
104 jsr166 1.17 long timeoutMillis = SHORT_DELAY_MS;
105     long startTime = System.nanoTime();
106     try {
107     e.exchange(null, timeoutMillis, MILLISECONDS);
108     shouldThrow();
109     } catch (TimeoutException success) {
110     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
111     }
112     }});
113 jsr166 1.9
114 jsr166 1.17 awaitTermination(t);
115 dl 1.1 }
116 dl 1.4
117     /**
118     * If one exchanging thread is interrupted, another succeeds.
119     */
120 jsr166 1.18 public void testReplacementAfterExchange() {
121 dl 1.4 final Exchanger e = new Exchanger();
122 jsr166 1.17 final CountDownLatch exchanged = new CountDownLatch(2);
123     final CountDownLatch interrupted = new CountDownLatch(1);
124     Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
125 jsr166 1.9 public void realRun() throws InterruptedException {
126 jsr166 1.13 assertSame(two, e.exchange(one));
127 jsr166 1.17 exchanged.countDown();
128 jsr166 1.13 e.exchange(two);
129 jsr166 1.9 }});
130 jsr166 1.17 Thread t2 = newStartedThread(new CheckedRunnable() {
131 jsr166 1.9 public void realRun() throws InterruptedException {
132 jsr166 1.13 assertSame(one, e.exchange(two));
133 jsr166 1.17 exchanged.countDown();
134     interrupted.await();
135 jsr166 1.13 assertSame(three, e.exchange(one));
136 jsr166 1.9 }});
137 jsr166 1.17 Thread t3 = newStartedThread(new CheckedRunnable() {
138 jsr166 1.9 public void realRun() throws InterruptedException {
139 jsr166 1.17 interrupted.await();
140 jsr166 1.13 assertSame(one, e.exchange(three));
141 jsr166 1.9 }});
142    
143 jsr166 1.17 await(exchanged);
144 jsr166 1.9 t1.interrupt();
145 jsr166 1.19 awaitTermination(t1);
146 jsr166 1.17 interrupted.countDown();
147     awaitTermination(t2);
148     awaitTermination(t3);
149 dl 1.4 }
150    
151 dl 1.1 }