1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.sql.Connection ; 12 import java.sql.DriverManager ; 13 import java.sql.SQLException ; 14 import java.util.Properties ; 15 16 25 public class StateListenerTest extends AbstractProxoolTest { 26 27 private static final Log LOG = LogFactory.getLog(StateListenerTest.class); 28 29 32 public StateListenerTest(String s) { 33 super(s); 34 } 35 36 40 public void testAddStateListener() throws Exception { 41 42 String testName = "addStateListener"; 43 String alias = testName; 44 45 String url = TestHelper.buildProxoolUrl(alias, 46 TestConstants.HYPERSONIC_DRIVER, 47 TestConstants.HYPERSONIC_TEST_URL); 48 Properties info = new Properties (); 49 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 50 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 51 info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1"); 52 info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "0"); 53 info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000"); 54 info.setProperty(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY, "6000"); 55 ProxoolFacade.registerConnectionPool(url, info); 56 57 assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount()); 58 59 StateResultMonitor srm = new StateResultMonitor(); 60 ProxoolFacade.addStateListener(alias, srm); 61 62 assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount()); 63 64 Connection c1 = DriverManager.getConnection(url); 65 66 srm.setExpectedUpState(StateListenerIF.STATE_BUSY); 68 assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS, srm.getResult()); 69 70 assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount()); 71 72 try { 73 Connection c2 = DriverManager.getConnection(url); 74 fail("Didn't expect second connection since maximumConnectionCount is 1"); 75 } catch (SQLException e) { 76 LOG.debug("Ignoring expected refusal: " + e.getMessage()); 79 } 80 81 srm.setExpectedUpState(StateListenerIF.STATE_OVERLOADED); 83 assertEquals("Timeout waiting for OVERLOADED", ResultMonitor.SUCCESS, srm.getResult()); 84 85 srm.setExpectedUpState(StateListenerIF.STATE_BUSY); 87 assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS, srm.getResult()); 88 89 c1.close(); 91 srm.setExpectedUpState(StateListenerIF.STATE_QUIET); 92 assertEquals("Timeout waiting for QUIET", ResultMonitor.SUCCESS, srm.getResult()); 93 94 try { 96 ProxoolFacade.updateConnectionPool("proxool." + alias + ":blah:foo", null); 97 } catch (ProxoolException e) { 98 LOG.debug("Ignoring expected exception when trying to register a pool with a bogus driver", e); 99 } 100 srm.setExpectedUpState(StateListenerIF.STATE_DOWN); 101 assertEquals("Timeout waiting for DOWN", ResultMonitor.SUCCESS, srm.getResult()); 102 103 } 104 105 class TestStateListener implements StateListenerIF { 106 107 private boolean somethingHappened; 108 109 private int upState; 110 111 public void upStateChanged(int newUpState) { 112 LOG.debug("upState: " + upState + " -> " + newUpState); 113 upState = newUpState; 114 somethingHappened = true; 115 } 116 117 boolean isSomethingHappened() { 118 return somethingHappened; 119 } 120 121 int getUpState() { 122 return upState; 123 } 124 125 void reset() { 126 upState = 0; 127 somethingHappened = false; 128 } 129 130 void waitForSomethingToHappen(int stateToWaitFor) { 131 132 long start = System.currentTimeMillis(); 133 while (!somethingHappened) { 134 if (upState == stateToWaitFor) { 135 if (!somethingHappened) { 136 LOG.error("Waiting for state = " + stateToWaitFor + " but it's already at that state"); 137 break; 138 } 139 } 140 try { 141 Thread.sleep(500); 142 } catch (InterruptedException e) { 143 LOG.error("Awoken", e); 144 } 145 if (System.currentTimeMillis() - start > 30000) { 146 fail("Timeout waiting for something to happen"); 147 } 148 } 149 150 } 151 152 int zgetNextState(int stateToWaitFor) { 153 waitForSomethingToHappen(stateToWaitFor); 154 somethingHappened = false; 155 return upState; 156 } 157 } 158 } 159 160 221 | Popular Tags |