1 8 package org.apache.avalon.excalibur.datasource.test; 9 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 import java.util.Random ; 13 import java.util.LinkedList ; 14 import java.util.Iterator ; 15 import org.apache.avalon.framework.component.Component; 16 import org.apache.avalon.framework.component.ComponentException; 17 import org.apache.avalon.framework.configuration.Configuration; 18 import org.apache.avalon.framework.configuration.ConfigurationException; 19 import org.apache.avalon.framework.configuration.DefaultConfiguration; 20 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; 21 import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError; 22 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 23 import org.apache.avalon.excalibur.datasource.JdbcDataSource; 24 import org.apache.avalon.excalibur.concurrent.ThreadBarrier; 25 26 33 public class DataSourceTestCase 34 extends ExcaliburTestCase 35 { 36 protected boolean isSuccessful; 37 protected ThreadBarrier barrier; 38 protected int connectionCount; 39 40 public DataSourceTestCase(String name) 41 { 42 super(name); 43 } 44 45 public void testOverAllocation() 46 { 47 DataSourceComponent ds = null; 48 this.isSuccessful = false; 49 LinkedList connectionList = new LinkedList (); 50 51 try 52 { 53 ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE ); 54 55 for( int i = 0; i < 11; i++ ) 56 { 57 connectionList.add( ds.getConnection() ); 58 } 59 } 60 catch ( SQLException se ) 61 { 62 this.isSuccessful = true; 63 getLogger().info( "The test was successful" ); 64 } 65 catch ( ComponentException ce ) 66 { 67 if ( getLogger().isDebugEnabled() ) 68 { 69 getLogger().debug( "There was an error in the OverAllocation test", ce ); 70 } 71 72 throw new CascadingAssertionFailedError( "There was an error in the OverAllocation test", ce ); 73 } 74 finally 75 { 76 assertTrue( "The DataSourceComponent could not be retrieved.", null != ds ); 77 78 Iterator connections = connectionList.iterator(); 79 80 while ( connections.hasNext() ) 81 { 82 try 83 { 84 ( (Connection ) connections.next() ).close(); 85 } 86 catch ( SQLException se ) 87 { 88 } 90 } 91 92 connectionList.clear(); 93 94 manager.release( (Component) ds ); 95 } 96 97 assertTrue( "Exception was not thrown when too many datasource components were retrieved.", this.isSuccessful ); 98 } 99 100 public void testNormalUse() 101 { 102 DataSourceComponent ds = null; 103 this.isSuccessful = true; 104 105 try 106 { 107 ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE ); 108 109 this.connectionCount = 0; 110 111 for (int i = 0; i < 10; i++) 112 { 113 (new Thread ( new ConnectionThread( this, ds ) ) ).start(); 114 } 115 116 this.barrier = new ThreadBarrier(11); 117 try 118 { 119 this.barrier.barrierSynchronize(); 120 } 121 catch(InterruptedException ie) 122 { 123 } 125 126 getLogger().info( "The normal use test passed with " + this.connectionCount + " requests and 10 concurrent threads running" ); 127 } 128 catch ( ComponentException ce ) 129 { 130 if ( getLogger().isDebugEnabled() ) 131 { 132 getLogger().debug( "There was an error in the normal use test", ce ); 133 } 134 135 throw new CascadingAssertionFailedError( "There was an error in the normal use test", ce ); 136 } 137 finally 138 { 139 assertTrue( "The DataSourceComponent could not be retrieved.", null != ds ); 140 141 manager.release( (Component) ds ); 142 } 143 144 assertTrue( "Normal use test failed", this.isSuccessful ); 145 } 146 147 class ConnectionThread 148 implements Runnable 149 { 150 protected DataSourceComponent datasource; 151 protected DataSourceTestCase testcase; 152 153 ConnectionThread( DataSourceTestCase testcase, 154 final DataSourceComponent datasource ) 155 { 156 this.datasource = datasource; 157 this.testcase = testcase; 158 } 159 160 public void run() 161 { 162 long end = System.currentTimeMillis() + 5000; Random rnd = new Random (); 164 165 while( System.currentTimeMillis() < end && this.testcase.isSuccessful ) 166 { 167 try 168 { 169 Connection con = this.datasource.getConnection(); 170 Thread.sleep((long) rnd.nextInt(100)); con.close(); 172 this.testcase.connectionCount++; 173 } 174 catch( final SQLException se ) 175 { 176 this.testcase.isSuccessful = false; 177 this.testcase.getLogger().info( "Failed to get Connection, test failed", se ); 178 } 179 catch( final InterruptedException ie ) 180 { 181 } 183 } 184 185 try 186 { 187 this.testcase.barrier.barrierSynchronize(); 188 } 189 catch( final InterruptedException ie ) 190 { 191 } 193 } 194 } 195 } 196 197 | Popular Tags |