001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util.concurrent;
005:
006: import com.tc.util.TCTimeoutException;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * Test cases for TCFuture
012: *
013: * @author teck
014: */
015: public class TCFutureTest extends TestCase {
016:
017: public void testTimeout() {
018: testTimeout(new TCFuture());
019:
020: Object lock = new Object();
021: final TCFuture f1 = new TCFuture(lock);
022: testTimeout(f1);
023: }
024:
025: public void testTimeout(TCFuture f1) {
026:
027: try {
028: f1.get(500);
029: fail("timeout didn't happen");
030: } catch (TCTimeoutException e) {
031: // expected
032: } catch (InterruptedException e) {
033: fail(e.getMessage());
034: } catch (TCExceptionResultException e) {
035: fail(e.getMessage());
036: }
037: }
038:
039: public void testResultSet() {
040: testResultSet(new TCFuture());
041: Object lock = new Object();
042: final TCFuture f1 = new TCFuture(lock);
043: testResultSet(f1);
044: }
045:
046: public void testResultSet(final TCFuture f1) {
047:
048: final Object val = new Object();
049:
050: Runnable run = new Runnable() {
051: public void run() {
052: try {
053: Thread.sleep(1000);
054: } catch (Throwable t) {
055: // this should case the test case to fail
056: f1.setException(new Throwable());
057: }
058: f1.set(val);
059: }
060: };
061:
062: new Thread(run).start();
063: try {
064: Object rv = f1.get();
065: assertTrue(rv == val);
066: } catch (InterruptedException e) {
067: fail(e.getMessage());
068: } catch (TCExceptionResultException e) {
069: fail(e.getMessage());
070: }
071: }
072:
073: public void testSetMulti() {
074: testSetMulti(new TCFuture());
075:
076: Object lock = new Object();
077: final TCFuture f1 = new TCFuture(lock);
078: testSetMulti(f1);
079: }
080:
081: public void testSetMulti(final TCFuture f1) {
082:
083: f1.set(new Object());
084:
085: for (int i = 0; i < 50; i++) {
086: try {
087: f1.set(new Object());
088: fail();
089: } catch (IllegalStateException ise) {
090: // expected
091: }
092: }
093: }
094:
095: public void testSetAfterCancel() {
096: testSetAfterCancel(new TCFuture());
097:
098: final TCFuture f1 = new TCFuture(new Object());
099: 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());
108: }
109:
110: public void testCancelAfterSet() {
111: testCancelAfterSet(new TCFuture());
112:
113: final TCFuture f1 = new TCFuture(new Object());
114: testCancelAfterSet(f1);
115: }
116:
117: public void testCancelAfterSet(final TCFuture f1) {
118:
119: f1.set(new Object());
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()));
129:
130: }
131:
132: public void testCancel(final TCFuture f1) {
133: Runnable run = new Runnable() {
134: public void run() {
135: f1.cancel();
136: }
137: };
138:
139: new Thread(run).start();
140: try {
141: f1.get(5000);
142: fail("future wasn't cancelled");
143: } catch (InterruptedException 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 {
156: testSetNull(new TCFuture());
157:
158: testSetNull(new TCFuture(new Object()));
159: }
160:
161: public void testSetNull(final TCFuture f1) throws Exception {
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()));
171: }
172:
173: public void testSetNullException(final TCFuture f1) {
174: try {
175: f1.setException(null);
176: fail();
177: } catch (IllegalArgumentException iae) {
178: // expected
179: }
180: }
181:
182: public void testExceptionAfterSet() {
183: testExceptionAfterSet(new TCFuture());
184:
185: testExceptionAfterSet(new TCFuture(new Object()));
186:
187: }
188:
189: public void testExceptionAfterSet(final TCFuture f1) {
190: f1.set(new Object());
191:
192: Throwable t = new Throwable("throw me");
193:
194: try {
195: f1.setException(t);
196: fail();
197: } catch (IllegalStateException ise) {
198: // expected
199: }
200: }
201:
202: public void testExceptionAfterCancel() {
203: testExceptionAfterCancel(new TCFuture());
204: testExceptionAfterCancel(new TCFuture(new Object()));
205: }
206:
207: public void testExceptionAfterCancel(final TCFuture f1) {
208: f1.cancel();
209:
210: Throwable t = new Throwable("throw me");
211:
212: // should just log a warning
213: f1.setException(t);
214: }
215:
216: public void testExceptionResult() {
217: testExceptionResult(new TCFuture());
218: testExceptionResult(new TCFuture(new Object()));
219: }
220:
221: public void testExceptionResult(final TCFuture f1) {
222: Throwable t = new Throwable("throw me");
223: f1.setException(t);
224:
225: try {
226: f1.get(1000);
227: fail("exception not thrown");
228: } catch (TCTimeoutException e) {
229: fail(e.getMessage());
230: } catch (InterruptedException e) {
231: fail(e.getMessage());
232: } catch (TCExceptionResultException e) {
233: Throwable thrown = e.getCause();
234: assertTrue(thrown == t);
235: }
236: }
237: }
|