ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.2
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +69 -36 lines
Log Message:
New base class JSR166TestCase

File Contents

# User Rev Content
1 dl 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.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11    
12 dl 1.2 public class ExchangerTest extends JSR166TestCase {
13 dl 1.1
14     public static void main(String[] args) {
15     junit.textui.TestRunner.run (suite());
16     }
17     public static Test suite() {
18     return new TestSuite(ExchangerTest.class);
19     }
20    
21 dl 1.2 public void testExchange() {
22 dl 1.1 final Exchanger e = new Exchanger();
23     Thread t1 = new Thread(new Runnable(){
24     public void run(){
25 dl 1.2 try {
26 dl 1.1 Object v = e.exchange(one);
27 dl 1.2 threadAssertEquals(v, two);
28 dl 1.1 Object w = e.exchange(v);
29 dl 1.2 threadAssertEquals(w, one);
30     } catch(InterruptedException e){
31     threadFail("unexpected exception");
32 dl 1.1 }
33     }
34     });
35     Thread t2 = new Thread(new Runnable(){
36     public void run(){
37 dl 1.2 try {
38 dl 1.1 Object v = e.exchange(two);
39 dl 1.2 threadAssertEquals(v, one);
40 dl 1.1 Object w = e.exchange(v);
41 dl 1.2 threadAssertEquals(w, two);
42     } catch(InterruptedException e){
43     threadFail("unexpected exception");
44     }
45     }
46     });
47     try {
48     t1.start();
49     t2.start();
50     t1.join();
51     t2.join();
52     } catch(InterruptedException ex) {
53     fail("unexpected exception");
54     }
55     }
56    
57     public void testTimedExchange() {
58     final Exchanger e = new Exchanger();
59     Thread t1 = new Thread(new Runnable(){
60     public void run(){
61     try {
62     Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
63     threadAssertEquals(v, two);
64     Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
65     threadAssertEquals(w, one);
66     } catch(InterruptedException e){
67     threadFail("unexpected exception");
68     } catch(TimeoutException toe) {
69     threadFail("unexpected exception");
70     }
71     }
72     });
73     Thread t2 = new Thread(new Runnable(){
74     public void run(){
75     try {
76     Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
77     threadAssertEquals(v, one);
78     Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
79     threadAssertEquals(w, two);
80     } catch(InterruptedException e){
81     threadFail("unexpected exception");
82     } catch(TimeoutException toe) {
83     threadFail("unexpected exception");
84 dl 1.1 }
85     }
86     });
87     try {
88     t1.start();
89     t2.start();
90     t1.join();
91     t2.join();
92     } catch(InterruptedException ex) {
93     fail("unexpected exception");
94     }
95     }
96    
97 dl 1.2 public void testExchange_InterruptedException(){
98 dl 1.1 final Exchanger e = new Exchanger();
99     Thread t = new Thread(new Runnable() {
100     public void run(){
101 dl 1.2 try {
102 dl 1.1 e.exchange(one);
103 dl 1.2 threadFail("should throw");
104     } catch(InterruptedException success){
105 dl 1.1 }
106     }
107     });
108 dl 1.2 try {
109 dl 1.1 t.start();
110     Thread.sleep(SHORT_DELAY_MS);
111     t.interrupt();
112     t.join();
113     } catch(InterruptedException ex) {
114     fail("unexpected exception");
115     }
116     }
117    
118 dl 1.2 public void testTimedExchange_InterruptedException(){
119 dl 1.1 final Exchanger e = new Exchanger();
120     Thread t = new Thread(new Runnable() {
121     public void run(){
122 dl 1.2 try {
123 dl 1.1 e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
124 dl 1.2 threadFail("should throw");
125 dl 1.1 } catch(InterruptedException success){
126     } catch(Exception e2){
127 dl 1.2 threadFail("should throw IE");
128 dl 1.1 }
129     }
130     });
131 dl 1.2 try {
132 dl 1.1 t.start();
133     t.interrupt();
134     t.join();
135     } catch(InterruptedException ex){
136     fail("unexpected exception");
137     }
138     }
139    
140 dl 1.2 public void testExchange_TimeOutException(){
141 dl 1.1 final Exchanger e = new Exchanger();
142     Thread t = new Thread(new Runnable() {
143     public void run(){
144 dl 1.2 try {
145 dl 1.1 e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
146 dl 1.2 threadFail("should throw");
147     } catch(TimeoutException success){
148     } catch(InterruptedException e2){
149     threadFail("should throw TOE");
150 dl 1.1 }
151     }
152     });
153 dl 1.2 try {
154 dl 1.1 t.start();
155     t.join();
156     } catch(InterruptedException ex){
157     fail("unexpected exception");
158     }
159     }
160     }