1 4 package com.tc.util.concurrent; 5 6 import com.tc.util.TCTimeoutException; 7 8 import junit.framework.TestCase; 9 10 15 public class TCFutureTest extends TestCase { 16 17 public void testTimeout() { 18 testTimeout(new TCFuture()); 19 20 Object lock = new Object (); 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 } catch (InterruptedException 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 lock = new Object (); 42 final TCFuture f1 = new TCFuture(lock); 43 testResultSet(f1); 44 } 45 46 public void testResultSet(final TCFuture f1) { 47 48 final Object val = new Object (); 49 50 Runnable run = new Runnable () { 51 public void run() { 52 try { 53 Thread.sleep(1000); 54 } catch (Throwable t) { 55 f1.setException(new Throwable ()); 57 } 58 f1.set(val); 59 } 60 }; 61 62 new Thread (run).start(); 63 try { 64 Object rv = f1.get(); 65 assertTrue(rv == val); 66 } catch (InterruptedException 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 lock = new Object (); 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 ()); 84 85 for (int i = 0; i < 50; i++) { 86 try { 87 f1.set(new Object ()); 88 fail(); 89 } catch (IllegalStateException ise) { 90 } 92 } 93 } 94 95 public void testSetAfterCancel() { 96 testSetAfterCancel(new TCFuture()); 97 98 final TCFuture f1 = new TCFuture(new Object ()); 99 testSetAfterCancel(f1); 100 } 101 102 public void testSetAfterCancel(final TCFuture f1) { 103 104 f1.cancel(); 105 106 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 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 } catch (TCExceptionResultException e) { 146 fail(e.getMessage()); 147 } catch (TCTimeoutException e) { 148 fail(e.getMessage()); 149 } 150 151 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 174 public void testSetNullException(final TCFuture f1) { 175 try { 176 f1.setException(null); 177 fail(); 178 } catch (IllegalArgumentException iae) { 179 } 181 } 182 183 public void testExceptionAfterSet() { 184 testExceptionAfterSet(new TCFuture()); 185 186 testExceptionAfterSet(new TCFuture(new Object ())); 187 188 } 189 190 public void testExceptionAfterSet(final TCFuture f1) { 191 f1.set(new Object ()); 192 193 Throwable t = new Throwable ("throw me"); 194 195 try { 196 f1.setException(t); 197 fail(); 198 } catch (IllegalStateException ise) { 199 } 201 } 202 203 public void testExceptionAfterCancel() { 204 testExceptionAfterCancel(new TCFuture()); 205 testExceptionAfterCancel(new TCFuture(new Object ())); 206 } 207 208 public void testExceptionAfterCancel(final TCFuture f1) { 209 f1.cancel(); 210 211 Throwable t = new Throwable ("throw me"); 212 213 f1.setException(t); 215 } 216 217 public void testExceptionResult() { 218 testExceptionResult(new TCFuture()); 219 testExceptionResult(new TCFuture(new Object ())); 220 } 221 222 public void testExceptionResult(final TCFuture f1) { 223 Throwable t = new Throwable ("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 e) { 232 fail(e.getMessage()); 233 } catch (TCExceptionResultException e) { 234 Throwable thrown = e.getCause(); 235 assertTrue(thrown == t); 236 } 237 } 238 } 239 | Popular Tags |