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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines