ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.21
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +3 -1 lines
Log Message:
use serialClone in serialization tests; update imports

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 jsr166 1.21 import java.util.concurrent.CountDownLatch;
12     import java.util.concurrent.Exchanger;
13     import java.util.concurrent.TimeoutException;
14 jsr166 1.11 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 dl 1.1
16 dl 1.2 public class ExchangerTest extends JSR166TestCase {
17 jsr166 1.6
18 dl 1.1 public static void main(String[] args) {
19 jsr166 1.14 junit.textui.TestRunner.run(suite());
20 dl 1.1 }
21     public static Test suite() {
22 jsr166 1.10 return new TestSuite(ExchangerTest.class);
23 dl 1.1 }
24    
25 dl 1.3 /**
26 dl 1.4 * exchange exchanges objects across two threads
27 dl 1.3 */
28 jsr166 1.17 public void testExchange() {
29 dl 1.1 final Exchanger e = new Exchanger();
30 jsr166 1.17 Thread t1 = newStartedThread(new CheckedRunnable() {
31 jsr166 1.9 public void realRun() throws InterruptedException {
32 jsr166 1.13 assertSame(one, e.exchange(two));
33     assertSame(two, e.exchange(one));
34 jsr166 1.9 }});
35 jsr166 1.17 Thread t2 = newStartedThread(new CheckedRunnable() {
36 jsr166 1.9 public void realRun() throws InterruptedException {
37 jsr166 1.13 assertSame(two, e.exchange(one));
38     assertSame(one, e.exchange(two));
39 jsr166 1.9 }});
40    
41 jsr166 1.17 awaitTermination(t1);
42     awaitTermination(t2);
43 dl 1.2 }
44    
45 dl 1.3 /**
46 dl 1.4 * timed exchange exchanges objects across two threads
47 dl 1.3 */
48 jsr166 1.17 public void testTimedExchange() {
49 dl 1.2 final Exchanger e = new Exchanger();
50 jsr166 1.17 Thread t1 = newStartedThread(new CheckedRunnable() {
51 jsr166 1.9 public void realRun() throws Exception {
52 jsr166 1.17 assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
53     assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
54 jsr166 1.9 }});
55 jsr166 1.17 Thread t2 = newStartedThread(new CheckedRunnable() {
56 jsr166 1.9 public void realRun() throws Exception {
57 jsr166 1.17 assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
58     assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
59 jsr166 1.9 }});
60    
61 jsr166 1.17 awaitTermination(t1);
62     awaitTermination(t2);
63 dl 1.1 }
64    
65 dl 1.3 /**
66 dl 1.4 * interrupt during wait for exchange throws IE
67 dl 1.3 */
68 jsr166 1.17 public void testExchange_InterruptedException() {
69 dl 1.1 final Exchanger e = new Exchanger();
70 jsr166 1.17 final CountDownLatch threadStarted = new CountDownLatch(1);
71     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
72 jsr166 1.9 public void realRun() throws InterruptedException {
73 jsr166 1.17 threadStarted.countDown();
74 jsr166 1.9 e.exchange(one);
75     }});
76    
77 jsr166 1.17 await(threadStarted);
78 jsr166 1.9 t.interrupt();
79 jsr166 1.17 awaitTermination(t);
80 dl 1.1 }
81    
82 dl 1.3 /**
83 dl 1.4 * interrupt during wait for timed exchange throws IE
84 dl 1.3 */
85 jsr166 1.17 public void testTimedExchange_InterruptedException() {
86 dl 1.1 final Exchanger e = new Exchanger();
87 jsr166 1.17 final CountDownLatch threadStarted = new CountDownLatch(1);
88     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
89 jsr166 1.9 public void realRun() throws Exception {
90 jsr166 1.17 threadStarted.countDown();
91     e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
92 jsr166 1.9 }});
93    
94 jsr166 1.17 await(threadStarted);
95 jsr166 1.9 t.interrupt();
96 jsr166 1.17 awaitTermination(t);
97 dl 1.1 }
98    
99 dl 1.3 /**
100 jsr166 1.17 * timeout during wait for timed exchange throws TimeoutException
101 dl 1.3 */
102 jsr166 1.17 public void testExchange_TimeoutException() {
103 dl 1.1 final Exchanger e = new Exchanger();
104 jsr166 1.17 Thread t = newStartedThread(new CheckedRunnable() {
105 jsr166 1.9 public void realRun() throws Exception {
106 jsr166 1.17 long startTime = System.nanoTime();
107     try {
108 jsr166 1.20 e.exchange(null, timeoutMillis(), MILLISECONDS);
109 jsr166 1.17 shouldThrow();
110 jsr166 1.20 } catch (TimeoutException success) {}
111     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
112 jsr166 1.17 }});
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 }