1 10 11 package org.mule.tck.model; 12 13 import org.apache.commons.lang.exception.ExceptionUtils; 14 import org.mule.impl.MuleDescriptor; 15 import org.mule.tck.AbstractMuleTestCase; 16 import org.mule.tck.testmodels.fruit.Orange; 17 import org.mule.umo.UMODescriptor; 18 import org.mule.umo.lifecycle.InitialisationException; 19 import org.mule.umo.model.UMOPoolFactory; 20 import org.mule.util.ObjectPool; 21 22 28 public abstract class AbstractPoolTestCase extends AbstractMuleTestCase 29 { 30 public static final byte FAIL_WHEN_EXHAUSTED = 0; 31 public static final byte GROW_WHEN_EXHAUSTED = 1; 32 public static final byte BLOCK_WHEN_EXHAUSTED = 2; 33 34 public static final byte DEFAULT_POOL_SIZE = 3; 35 public static final long DEFAULT_WAIT = 1500; 36 37 public AbstractPoolTestCase() 38 { 39 super(); 40 41 } 42 43 protected void doSetUp() throws Exception 44 { 45 getManager(true); 47 } 48 49 public void testCreatePool() throws Exception 50 { 51 52 MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName()); 53 ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED); 54 55 assertNotNull(pool); 56 assertEquals(0, pool.getSize()); 57 58 Object borrowed = pool.borrowObject(); 59 assertNotNull(borrowed); 60 assertEquals(1, pool.getSize()); 61 pool.returnObject(borrowed); 62 63 borrowed = pool.borrowObject(); 64 assertNotNull(borrowed); 65 assertEquals(1, pool.getSize()); 66 Object borrowed2 = pool.borrowObject(); 67 assertNotNull(borrowed2); 68 assertEquals(2, pool.getSize()); 69 } 70 71 public void testFailOnExhaust() throws Exception 72 { 73 74 ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED); 75 Object borrowed = null; 76 77 for (int i = 0; i < pool.getMaxSize(); i++) 78 { 79 borrowed = pool.borrowObject(); 80 assertNotNull(borrowed); 81 assertEquals(pool.getSize(), i + 1); 82 } 83 84 try 85 { 86 borrowed = pool.borrowObject(); 87 fail("Should throw an Exception"); 88 } 89 catch (Exception e) 90 { 91 } 93 } 94 95 public void testBlockExpiryOnExhaust() throws Exception 96 { 97 ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), 98 BLOCK_WHEN_EXHAUSTED); 99 Object borrowed = null; 100 101 assertEquals(0, pool.getSize()); 102 borrowed = pool.borrowObject(); 103 assertNotNull(borrowed); 104 borrowed = pool.borrowObject(); 105 assertNotNull(borrowed); 106 borrowed = pool.borrowObject(); 107 assertNotNull(borrowed); 108 assertEquals(3, pool.getSize()); 109 110 try 113 { 114 borrowed = pool.borrowObject(); 115 fail("Should throw an Exception"); 116 } 117 catch (Exception e) 118 { 119 } 126 } 127 128 public void testBlockOnExhaust() throws Exception 129 { 130 ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), 131 BLOCK_WHEN_EXHAUSTED); 132 Object borrowed = null; 133 134 assertEquals(0, pool.getSize()); 135 136 borrowed = pool.borrowObject(); 137 borrowed = pool.borrowObject(); 138 assertEquals(2, pool.getSize()); 139 140 long borrowerWait = 500; 143 Borrower borrower = new Borrower(pool, borrowerWait); 144 borrower.start(); 145 try 147 { 148 Thread.sleep(200); 149 } 150 catch (InterruptedException e) 151 { 152 } 154 155 borrowed = pool.borrowObject(); 156 163 assertNotNull(borrowed); 164 } 165 166 public void testGrowOnExhaust() throws Exception 167 { 168 ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), GROW_WHEN_EXHAUSTED); 169 170 Object borrowed = pool.borrowObject(); 171 borrowed = pool.borrowObject(); 172 borrowed = pool.borrowObject(); 173 assertEquals(3, pool.getSize()); 174 assertEquals(3, pool.getMaxSize()); 175 176 borrowed = pool.borrowObject(); 178 assertNotNull(borrowed); 179 180 assertEquals(4, pool.getSize()); 181 } 182 183 public void testClearPool() throws Exception 184 { 185 186 ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED); 187 188 Object borrowed = pool.borrowObject(); 189 assertEquals(1, pool.getSize()); 190 pool.returnObject(borrowed); 191 192 pool.clearPool(); 193 assertEquals(0, pool.getSize()); 194 195 borrowed = pool.borrowObject(); 196 assertEquals(1, pool.getSize()); 197 } 198 199 public void testCreateFromFactory() throws Exception 200 { 201 UMODescriptor descriptor = getTestDescriptor("orange", Orange.class.getName()); 202 UMOPoolFactory factory = getPoolFactory(); 203 ObjectPool pool = factory.createPool(descriptor); 204 assertNotNull(pool); 205 } 206 207 public void testPoolLifecycle() throws Exception 208 { 209 MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName()); 210 ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED); 211 212 assertNotNull(pool); 213 assertEquals(0, pool.getSize()); 214 215 } 216 217 private class Borrower extends Thread 218 { 219 private ObjectPool pool; 220 private long time; 221 222 public Borrower(ObjectPool pool, long time) 223 { 224 super("Borrower"); 225 if (pool == null) 226 { 227 throw new IllegalArgumentException ("Pool cannot be null"); 228 } 229 this.pool = pool; 230 if (time < 500) 231 { 232 time = 500; 233 } 234 this.time = time; 235 } 236 237 242 public void run() 243 { 244 try 245 { 246 Object object = pool.borrowObject(); 247 try 248 { 249 sleep(time); 250 } 251 catch (InterruptedException e) 252 { 253 } 255 pool.returnObject(object); 256 } 257 catch (Exception e) 258 { 259 fail("Borrower thread failed:\n" + ExceptionUtils.getStackTrace(e)); 260 } 261 } 262 263 } 264 265 public abstract ObjectPool createPool(MuleDescriptor descriptor, byte action) 266 throws InitialisationException; 267 268 public abstract UMOPoolFactory getPoolFactory(); 269 } 270 | Popular Tags |