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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.17 by jsr166, Sun May 15 16:58:16 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  
12 public class ExchangerTest extends TestCase{
13  
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 <    private static long SHORT_DELAY_MS = 100;
24 <    private static long MEDIUM_DELAY_MS = 1000;
25 <    private static long LONG_DELAY_MS = 10000;
26 <    
27 <    private final static Integer one = new Integer(1);
28 <    private final static Integer two = new Integer(2);
29 <
30 <    public void testExchange(){
31 <        final Exchanger e = new Exchanger();
32 <        Thread t1 = new Thread(new Runnable(){
33 <                public void run(){
34 <                    try{
35 <                        Object v = e.exchange(one);
36 <                        assertEquals(v, two);
37 <                        Object w = e.exchange(v);
38 <                        assertEquals(w, one);
39 <                    }catch(InterruptedException e){
40 <                        fail("unexpected exception");
41 <                    }
42 <                }
43 <            });
44 <        Thread t2 = new Thread(new Runnable(){
45 <                public void run(){
46 <                    try{
47 <                        Object v = e.exchange(two);
48 <                        assertEquals(v, one);
49 <                        Object w = e.exchange(v);
50 <                        assertEquals(w, two);
51 <                    }catch(InterruptedException e){
52 <                        fail("unexpected exception");
53 <                    }
54 <                }
55 <            });
56 <        try {
57 <            t1.start();
58 <            t2.start();
59 <            t1.join();
60 <            t2.join();
61 <        } catch(InterruptedException ex) {
62 <            fail("unexpected exception");
63 <        }
64 <    }
65 <
66 <    public void testExchange1_InterruptedException(){
67 <        final Exchanger e = new Exchanger();
68 <        Thread t = new Thread(new Runnable() {
69 <                public void run(){
70 <                    try{
71 <                        e.exchange(one);
72 <                        fail("should throw");
73 <                    }catch(InterruptedException success){
74 <                    }
75 <                }
76 <            });
77 <        try{
78 <            t.start();
79 <            Thread.sleep(SHORT_DELAY_MS);
80 <            t.interrupt();
81 <            t.join();
82 <        } catch(InterruptedException ex) {
83 <            fail("unexpected exception");
84 <        }
85 <    }
86 <
87 <    public void testExchange2_InterruptedException(){
88 <        final Exchanger e = new Exchanger();
89 <        Thread t = new Thread(new Runnable() {
90 <                public void run(){
91 <                    try{
92 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
93 <                        fail("should throw");
94 <                    } catch(InterruptedException success){
95 <                    } catch(Exception e2){
96 <                        fail("should throw IE");
97 <                    }
98 <                }
99 <            });
100 <        try{
101 <            t.start();
102 <            t.interrupt();
103 <            t.join();
104 <        } catch(InterruptedException ex){
105 <            fail("unexpected exception");
106 <        }
107 <    }
108 <
109 <    public void testExchange3_TimeOutException(){
110 <        final Exchanger e = new Exchanger();
109 <        Thread t = new Thread(new Runnable() {
110 <                public void run(){
111 <                    try{
112 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
113 <                        fail("should throw");
114 <                    }catch(TimeoutException success){}
115 <                    catch(InterruptedException e2){
116 <                        fail("should throw TOE");
117 <                    }
23 >    /**
24 >     * exchange exchanges objects across two threads
25 >     */
26 >    public void testExchange() {
27 >        final Exchanger e = new Exchanger();
28 >        Thread t1 = newStartedThread(new CheckedRunnable() {
29 >            public void realRun() throws InterruptedException {
30 >                assertSame(one, e.exchange(two));
31 >                assertSame(two, e.exchange(one));
32 >            }});
33 >        Thread t2 = newStartedThread(new CheckedRunnable() {
34 >            public void realRun() throws InterruptedException {
35 >                assertSame(two, e.exchange(one));
36 >                assertSame(one, e.exchange(two));
37 >            }});
38 >
39 >        awaitTermination(t1);
40 >        awaitTermination(t2);
41 >    }
42 >
43 >    /**
44 >     * timed exchange exchanges objects across two threads
45 >     */
46 >    public void testTimedExchange() {
47 >        final Exchanger e = new Exchanger();
48 >        Thread t1 = newStartedThread(new CheckedRunnable() {
49 >            public void realRun() throws Exception {
50 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
51 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
52 >            }});
53 >        Thread t2 = newStartedThread(new CheckedRunnable() {
54 >            public void realRun() throws Exception {
55 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
56 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
57 >            }});
58 >
59 >        awaitTermination(t1);
60 >        awaitTermination(t2);
61 >    }
62 >
63 >    /**
64 >     * interrupt during wait for exchange throws IE
65 >     */
66 >    public void testExchange_InterruptedException() {
67 >        final Exchanger e = new Exchanger();
68 >        final CountDownLatch threadStarted = new CountDownLatch(1);
69 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
70 >            public void realRun() throws InterruptedException {
71 >                threadStarted.countDown();
72 >                e.exchange(one);
73 >            }});
74 >
75 >        await(threadStarted);
76 >        t.interrupt();
77 >        awaitTermination(t);
78 >    }
79 >
80 >    /**
81 >     * interrupt during wait for timed exchange throws IE
82 >     */
83 >    public void testTimedExchange_InterruptedException() {
84 >        final Exchanger e = new Exchanger();
85 >        final CountDownLatch threadStarted = new CountDownLatch(1);
86 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
87 >            public void realRun() throws Exception {
88 >                threadStarted.countDown();
89 >                e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
90 >            }});
91 >
92 >        await(threadStarted);
93 >        t.interrupt();
94 >        awaitTermination(t);
95 >    }
96 >
97 >    /**
98 >     * timeout during wait for timed exchange throws TimeoutException
99 >     */
100 >    public void testExchange_TimeoutException() {
101 >        final Exchanger e = new Exchanger();
102 >        Thread t = newStartedThread(new CheckedRunnable() {
103 >            public void realRun() throws Exception {
104 >                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 <        try{
114 <            t.start();
115 <            t.join();
116 <        } catch(InterruptedException ex){
117 <            fail("unexpected exception");
118 <        }
112 >            }});
113 >
114 >        awaitTermination(t);
115 >    }
116 >
117 >    /**
118 >     * If one exchanging thread is interrupted, another succeeds.
119 >     */
120 >    public void testReplacementAfterExchange() throws InterruptedException {
121 >        final Exchanger e = new Exchanger();
122 >        final CountDownLatch exchanged = new CountDownLatch(2);
123 >        final CountDownLatch interrupted = new CountDownLatch(1);
124 >        Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
125 >            public void realRun() throws InterruptedException {
126 >                assertSame(two, e.exchange(one));
127 >                exchanged.countDown();
128 >                e.exchange(two);
129 >            }});
130 >        Thread t2 = newStartedThread(new CheckedRunnable() {
131 >            public void realRun() throws InterruptedException {
132 >                assertSame(one, e.exchange(two));
133 >                exchanged.countDown();
134 >                interrupted.await();
135 >                assertSame(three, e.exchange(one));
136 >            }});
137 >        Thread t3 = newStartedThread(new CheckedRunnable() {
138 >            public void realRun() throws InterruptedException {
139 >                interrupted.await();
140 >                assertSame(one, e.exchange(three));
141 >            }});
142 >
143 >        await(exchanged);
144 >        t1.interrupt();
145 >        interrupted.countDown();
146 >        awaitTermination(t1);
147 >        awaitTermination(t2);
148 >        awaitTermination(t3);
149      }
150 +
151   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines