1 package org.smartlib.pool.core; 2 3 import org.apache.log4j.Logger; 4 5 import java.sql.Connection ; 6 7 14 public class MultiPoolImplTest extends PoolTestFixture { 15 16 private static Logger logger = Logger.getLogger(MultiPoolImplTest.class); 17 18 MultiPool multiPool = null; 19 protected void setUp() throws Exception { 20 super.setUp(); 21 logger.debug("=====================Start=============================="); 22 } 23 24 protected void tearDown() throws Exception { 25 logger.debug("======================End==============================="); 26 super.tearDown(); 27 try { 28 multiPool.shutDown(); 29 } catch (Exception e) { 30 logger.debug("Exception in tear down",e); 31 } 32 33 } 34 35 public void testBasicConnection() throws Exception { 36 PoolConfig config = getBasicPoolConfig(); 37 multiPool = new MultiPoolImpl(config); 38 Connection conn = multiPool.getConnection(); 39 assertNotNull("Connection cannot be null", conn); 40 testConnection(conn); 41 conn.close(); 42 } 43 44 public void testIfRandomConnectionsAreObtained() throws Exception { 45 PoolConfig config = getPoolConfigWithMultiplePools(); 46 multiPool = new MultiPoolImpl(config); 47 Connection conn = multiPool.getConnection(); 48 testConnection(conn); 49 assertNotNull("Connection cannot be null", conn); 50 String subPoolName = ((SmartConnection)conn).getPoolName(); 51 logger.debug("Connection was picked from: " + subPoolName); 52 conn.close(); 53 54 conn = multiPool.getConnection(); 55 testConnection(conn); 56 assertNotNull("Connection cannot be null", conn); 57 String subPoolName1 = ((SmartConnection)conn).getPoolName(); 58 logger.debug("Connection was picked from: " + subPoolName1); 59 conn.close(); 60 assertNotSame("The two pools cannot be same", subPoolName, subPoolName1); 61 62 } 63 64 public void testIfThreadSkickinessIsMaintained() throws Exception { 65 66 PoolConfig config = getPoolConfigWithMultiplePools(); 67 config.setThreadStickiness(true); 68 multiPool = new MultiPoolImpl(config); 69 Connection conn = multiPool.getConnection(); 70 testConnection(conn); 71 assertNotNull("Connection cannot be null", conn); 72 String subPoolName = ((SmartConnection)conn).getPoolName(); 73 logger.debug("Connection 1 was picked from pool: " + subPoolName); 74 String insanceName = getInstanceName(conn); 75 logger.debug("Connection 1 was picked from instance: " + insanceName); 76 conn.close(); 77 78 conn = multiPool.getConnection(); 79 testConnection(conn); 80 assertNotNull("Connection cannot be null", conn); 81 String subPoolName1 = ((SmartConnection)conn).getPoolName(); 82 logger.debug("Connection 2 was picked from pool: " + subPoolName1); 83 String insanceName1 = getInstanceName(conn); 84 logger.debug("Connection 2 was picked from instance: " + insanceName1); 85 conn.close(); 86 assertEquals("The two pools should be same", subPoolName, subPoolName1); 87 assertEquals("The two instances should be same", insanceName, insanceName1); 88 89 } 90 91 92 public void testIfRandomConnectionsAreObtainedForExternalPooling() throws Exception { 93 94 PoolConfig config = getPoolConfigWithMultipleExternalPools(); 95 multiPool = new MultiPoolImpl(config); 96 Connection conn = multiPool.getConnection(); 97 testConnection(conn); 98 assertNotNull("Connection cannot be null", conn); 99 String subPoolName = ((SmartConnection)conn).getPoolName(); 100 logger.debug("Connection was picked from: " + subPoolName); 101 String insanceName = getInstanceName(conn); 102 logger.debug("Connection 1 was picked from instance: " + insanceName); 103 conn.close(); 104 105 conn = multiPool.getConnection(); 106 testConnection(conn); 107 assertNotNull("Connection cannot be null", conn); 108 String subPoolName1 = ((SmartConnection)conn).getPoolName(); 109 logger.debug("Connection was picked from: " + subPoolName1); 110 String insanceName1 = getInstanceName(conn); 111 logger.debug("Connection 2 was picked from instance: " + insanceName1); 112 conn.close(); 113 assertNotSame("The two pools cannot be same", subPoolName, subPoolName1); 114 assertNotSame("The two instances should be same", insanceName, insanceName1); 115 116 } 117 118 public void testIfThreadSkickinessIsMaintainedForExternalPooling() throws Exception { 119 120 PoolConfig config = getPoolConfigWithMultipleExternalPools(); 121 config.setThreadStickiness(true); 122 multiPool = new MultiPoolImpl(config); 123 Connection conn = multiPool.getConnection(); 124 testConnection(conn); 125 assertNotNull("Connection cannot be null", conn); 126 String subPoolName = ((SmartConnection)conn).getPoolName(); 127 logger.debug("Connection 1 was picked from pool: " + subPoolName); 128 String insanceName = getInstanceName(conn); 129 logger.debug("Connection 1 was picked from instance: " + insanceName); 130 conn.close(); 131 132 conn = multiPool.getConnection(); 133 testConnection(conn); 134 assertNotNull("Connection cannot be null", conn); 135 String subPoolName1 = ((SmartConnection)conn).getPoolName(); 136 logger.debug("Connection 2 was picked from pool: " + subPoolName1); 137 String insanceName1 = getInstanceName(conn); 138 logger.debug("Connection 2 was picked from instance: " + insanceName1); 139 conn.close(); 140 assertEquals("The two pools should be same", subPoolName, subPoolName1); 141 assertEquals("The two instances should be same", insanceName, insanceName1); 142 143 } 144 145 146 } 147 | Popular Tags |