KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > concurrent > TCFutureTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.concurrent;
5
6 import com.tc.util.TCTimeoutException;
7
8 import junit.framework.TestCase;
9
10 /**
11  * Test cases for TCFuture
12  *
13  * @author teck
14  */

15 public class TCFutureTest extends TestCase {
16
17   public void testTimeout() {
18     testTimeout(new TCFuture());
19
20     Object JavaDoc lock = new Object JavaDoc();
21     final TCFuture f1 = new TCFuture(lock);
22     testTimeout(f1);
23   }
24
25   public void testTimeout(TCFuture f1) {
26
27     try {
28       f1.get(500);
29       fail("timeout didn't happen");
30     } catch (TCTimeoutException e) {
31       // expected
32
} catch (InterruptedException JavaDoc e) {
33       fail(e.getMessage());
34     } catch (TCExceptionResultException e) {
35       fail(e.getMessage());
36     }
37   }
38
39   public void testResultSet() {
40     testResultSet(new TCFuture());
41     Object JavaDoc lock = new Object JavaDoc();
42     final TCFuture f1 = new TCFuture(lock);
43     testResultSet(f1);
44   }
45
46   public void testResultSet(final TCFuture f1) {
47
48     final Object JavaDoc val = new Object JavaDoc();
49
50     Runnable JavaDoc run = new Runnable JavaDoc() {
51       public void run() {
52         try {
53           Thread.sleep(1000);
54         } catch (Throwable JavaDoc t) {
55           // this should case the test case to fail
56
f1.setException(new Throwable JavaDoc());
57         }
58         f1.set(val);
59       }
60     };
61
62     new Thread JavaDoc(run).start();
63     try {
64       Object JavaDoc rv = f1.get();
65       assertTrue(rv == val);
66     } catch (InterruptedException JavaDoc e) {
67       fail(e.getMessage());
68     } catch (TCExceptionResultException e) {
69       fail(e.getMessage());
70     }
71   }
72
73   public void testSetMulti() {
74     testSetMulti(new TCFuture());
75
76     Object JavaDoc lock = new Object JavaDoc();
77     final TCFuture f1 = new TCFuture(lock);
78     testSetMulti(f1);
79   }
80
81   public void testSetMulti(final TCFuture f1) {
82
83     f1.set(new Object JavaDoc());
84
85     for (int i = 0; i < 50; i++) {
86       try {
87         f1.set(new Object JavaDoc());
88         fail();
89       } catch (IllegalStateException JavaDoc ise) {
90         // expected
91
}
92     }
93   }
94
95   public void testSetAfterCancel() {
96     testSetAfterCancel(new TCFuture());
97
98     final TCFuture f1 = new TCFuture(new Object JavaDoc());
99     testSetAfterCancel(f1);
100   }
101
102   public void testSetAfterCancel(final TCFuture f1) {
103
104     f1.cancel();
105
106     // this should only produce a warning log message
107
f1.set(new Object JavaDoc());
108   }
109
110   public void testCancelAfterSet() {
111     testCancelAfterSet(new TCFuture());
112
113     final TCFuture f1 = new TCFuture(new Object JavaDoc());
114     testCancelAfterSet(f1);
115   }
116
117   public void testCancelAfterSet(final TCFuture f1) {
118
119     f1.set(new Object JavaDoc());
120
121     // this should only produce a warning log message
122
f1.cancel();
123   }
124
125   public void testCancel() {
126     testCancel(new TCFuture());
127     
128     testCancel(new TCFuture(new Object JavaDoc()));
129     
130   }
131   
132   public void testCancel(final TCFuture f1) {
133     Runnable JavaDoc run = new Runnable JavaDoc() {
134       public void run() {
135         f1.cancel();
136       }
137     };
138
139     new Thread JavaDoc(run).start();
140     try {
141       f1.get(5000);
142       fail("future wasn't cancelled");
143     } catch (InterruptedException JavaDoc e) {
144       // expected
145
} catch (TCExceptionResultException e) {
146       fail(e.getMessage());
147     } catch (TCTimeoutException e) {
148       fail(e.getMessage());
149     }
150
151     // should have no effect to cancel the future again
152
f1.cancel();
153   }
154
155   public void testSetNull() throws Exception JavaDoc {
156     testSetNull(new TCFuture());
157     
158     testSetNull(new TCFuture(new Object JavaDoc()));
159   }
160   
161   public void testSetNull(final TCFuture f1) throws Exception JavaDoc {
162     f1.set(null);
163
164     assertTrue(f1.get(100) == null);
165   }
166
167   public void testSetNullException() {
168     testSetNullException(new TCFuture());
169     
170     testSetNullException(new TCFuture(new Object JavaDoc()));
171   }
172   
173   
174   public void testSetNullException(final TCFuture f1) {
175     try {
176       f1.setException(null);
177       fail();
178     } catch (IllegalArgumentException JavaDoc iae) {
179       // expected
180
}
181   }
182
183   public void testExceptionAfterSet() {
184     testExceptionAfterSet(new TCFuture());
185     
186     testExceptionAfterSet(new TCFuture(new Object JavaDoc()));
187     
188   }
189   
190   public void testExceptionAfterSet(final TCFuture f1) {
191     f1.set(new Object JavaDoc());
192
193     Throwable JavaDoc t = new Throwable JavaDoc("throw me");
194
195     try {
196       f1.setException(t);
197       fail();
198     } catch (IllegalStateException JavaDoc ise) {
199       // expected
200
}
201   }
202
203   public void testExceptionAfterCancel() {
204     testExceptionAfterCancel(new TCFuture());
205     testExceptionAfterCancel(new TCFuture(new Object JavaDoc()));
206   }
207   
208   public void testExceptionAfterCancel(final TCFuture f1) {
209   f1.cancel();
210
211     Throwable JavaDoc t = new Throwable JavaDoc("throw me");
212
213     // should just log a warning
214
f1.setException(t);
215   }
216
217   public void testExceptionResult() {
218     testExceptionResult(new TCFuture());
219     testExceptionResult(new TCFuture(new Object JavaDoc()));
220   }
221   
222   public void testExceptionResult(final TCFuture f1) {
223     Throwable JavaDoc t = new Throwable JavaDoc("throw me");
224     f1.setException(t);
225
226     try {
227       f1.get(1000);
228       fail("exception not thrown");
229     } catch (TCTimeoutException e) {
230       fail(e.getMessage());
231     } catch (InterruptedException JavaDoc e) {
232       fail(e.getMessage());
233     } catch (TCExceptionResultException e) {
234       Throwable JavaDoc thrown = e.getCause();
235       assertTrue(thrown == t);
236     }
237   }
238 }
239
Popular Tags