1 64 65 package com.jcorporate.expresso.core.db.tests; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBConnectionPool; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.db.exception.PoolFullException; 71 import com.jcorporate.expresso.core.misc.DateTime; 72 import com.jcorporate.expresso.services.test.ExpressoTestCase; 73 import com.jcorporate.expresso.services.test.TestSystemInitializer; 74 import junit.framework.TestSuite; 75 76 77 83 84 public class DBConnectionPoolTest extends ExpressoTestCase { 85 static final int NUM_STRESS_THREADS = 60; 86 static final int NUM_ITERATIONS = 20; 87 88 93 public DBConnectionPoolTest(String testName) 94 throws Exception { 95 super(testName); 96 } 97 98 public static void main(String [] args) 99 throws Exception { 100 junit.textui.TestRunner.run(suite()); 101 } 102 103 public static junit.framework.Test suite() 104 throws Exception { 105 return new TestSuite(DBConnectionPoolTest.class); 106 } 107 108 public void tearDown() { 109 try { 110 DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()).disconnectAll(); 111 } catch (DBException ex) { 112 } 113 } 114 115 118 public void testConnectionPool() { 119 try { 120 System.out.println("\nBegin connection pool tests:" + 121 DateTime.getDateTimeString()); 122 123 DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()); 124 int maxConnections = myPool.getMaxConnections(); 125 DBConnection connections[] = new DBConnection[maxConnections]; 126 System.out.println("Maximum connections in the pool is: " + maxConnections); 127 128 for (int i = 0; i <= 5000; i++) { 129 for (int j = 0; j < maxConnections; j++) { 130 connections[j] = myPool.getConnection("Test " + j); 131 } 132 133 for (int j = 0; j < maxConnections; j++) { 134 myPool.release(connections[j]); 135 } 136 } 137 138 System.out.println("End connection pool tests:" + 139 DateTime.getDateTimeString()); 140 } catch (DBException ex) { 141 ex.printStackTrace(); 142 fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage()); 143 } 144 145 } 146 147 151 public void testNewRelease() { 152 try { 153 System.out.println("\nBegin new connection pool tests:" + 154 DateTime.getDateTimeString()); 155 156 DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()); 157 int maxConnections = myPool.getMaxConnections(); 158 DBConnection connections[] = new DBConnection[maxConnections]; 159 System.out.println("Maximum connections in the pool is: " + maxConnections); 160 161 for (int i = 0; i <= 100; i++) { 162 for (int j = 0; j < maxConnections; j++) { 163 connections[j] = myPool.getConnection("Test " + j); 164 connections[j].execute("SELECT COUNT(*) FROM CONTROLLERSECURITY"); 165 } 166 167 168 for (int j = 0; j < maxConnections; j++) { 169 myPool.release(connections[j]); 170 } 171 } 172 173 System.out.println("End new connection pool tests:" + 174 DateTime.getDateTimeString()); 175 } catch (DBException ex) { 176 ex.printStackTrace(); 177 fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage()); 178 } 179 180 } 181 182 187 public void testFullPool() { 188 try { 189 DBConnectionPool myPool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()); 190 int maxConnections = myPool.getMaxConnections(); 191 DBConnection connections[] = new DBConnection[maxConnections]; 192 System.out.println("Maximum connections in the pool is: " + maxConnections); 193 194 for (int j = 0; j < maxConnections; j++) { 195 connections[j] = myPool.getConnection("Test " + j); 196 } 197 198 try { 199 DBConnection breakConnection = myPool.getConnection("Breaking Connection"); 200 fail("The last connection SHOULD have thrown an exception"); 201 } catch (DBException dbe) { 202 assertTrue("Must receive a PoolFullException", 203 dbe instanceof PoolFullException); 204 } 205 206 for (int j = 0; j < maxConnections; j++) { 207 myPool.release(connections[j]); 208 } 209 } catch (DBException ex) { 210 ex.printStackTrace(); 211 fail("Caught an exception attempting DBConnectionPool Test " + ex.getMessage()); 212 } 213 } 214 215 219 public void testConnectionPoolStressTest() { 220 DBConnectionPoolStressThread theThreads[] = new DBConnectionPoolStressThread[NUM_STRESS_THREADS]; 221 222 for (int i = 0; i < NUM_STRESS_THREADS; i++) { 223 theThreads[i] = new DBConnectionPoolStressThread("DBConnectionPool Thread " + i); 224 } 225 226 for (int i = 0; i < NUM_STRESS_THREADS; i++) { 227 theThreads[i].start(); 228 } 229 230 for (int i = 0; i < NUM_STRESS_THREADS; i++) { 231 try { 232 theThreads[i].join(); 233 } catch (InterruptedException ex) { 234 } 235 } 236 237 boolean failures = false; 238 for (int i = 0; i < NUM_STRESS_THREADS; i++) { 239 System.out.println("Status for thread: " + theThreads[i].getName() + 240 " number of pool fulls: " + theThreads[i].getNumPoolFulls() + 241 " errors: " + theThreads[i].getErrors()); 242 243 if (theThreads[i].getErrors() != null) { 244 failures = true; 245 } 246 } 247 248 assertTrue("There were failures.", failures == false); 249 250 } 251 } | Popular Tags |