1 6 package org.logicalcobwebs.proxool.admin; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.AbstractProxoolTest; 11 import org.logicalcobwebs.proxool.ProxoolConstants; 12 import org.logicalcobwebs.proxool.ProxoolFacade; 13 import org.logicalcobwebs.proxool.TestConstants; 14 import org.logicalcobwebs.proxool.TestHelper; 15 16 import java.sql.DriverManager ; 17 import java.text.DateFormat ; 18 import java.text.SimpleDateFormat ; 19 import java.util.Date ; 20 import java.util.Properties ; 21 22 30 public class StatisticsListenerTest extends AbstractProxoolTest { 31 32 private static final Log LOG = LogFactory.getLog(StatisticsListenerTest.class); 33 34 37 private static final DateFormat TIME_FORMAT = new SimpleDateFormat ("mm:ss"); 38 39 42 public StatisticsListenerTest(String s) { 43 super(s); 44 } 45 46 49 public void testListener() throws Exception { 50 51 String testName = "listener"; 52 String alias = testName; 53 String url = TestHelper.buildProxoolUrl(alias, 54 TestConstants.HYPERSONIC_DRIVER, 55 TestConstants.HYPERSONIC_TEST_URL); 56 Properties info = new Properties (); 57 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 58 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 59 info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "5s"); 60 61 info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG); 63 64 ProxoolFacade.registerConnectionPool(url, info); 66 67 TestListener testListener = new TestListener(); 69 ProxoolFacade.addStatisticsListener(alias, testListener); 70 Date lap0 = new Date (); 71 72 testListener.getNextStatistics(); 75 Date lap1 = new Date (); 76 77 DriverManager.getConnection(url).close(); 78 Date lap2 = new Date (); 79 StatisticsIF statistics1 = testListener.getNextStatistics(); 80 Date lap3 = new Date (); 81 StatisticsIF statistics2 = testListener.getNextStatistics(); 82 Date lap4 = new Date (); 83 84 StringBuffer detail = new StringBuffer (); 85 detail.append("lap0:"); 86 detail.append(TIME_FORMAT.format(lap0)); 87 detail.append(", lap1:"); 88 detail.append(TIME_FORMAT.format(lap1)); 89 detail.append(", lap2:"); 90 detail.append(TIME_FORMAT.format(lap2)); 91 detail.append(", lap3:"); 92 detail.append(TIME_FORMAT.format(lap3)); 93 detail.append("("); 94 detail.append(statistics1.getServedCount()); 95 detail.append("), lap4:"); 96 detail.append(TIME_FORMAT.format(lap4)); 97 detail.append("("); 98 detail.append(statistics2.getServedCount()); 99 detail.append(")"); 100 assertEquals("servedCount - " + detail, 1L, statistics1.getServedCount()); 101 102 } 103 104 class TestListener implements StatisticsListenerIF { 105 106 private StatisticsIF statistics; 107 108 private boolean somethingHappened; 109 110 public void statistics(String alias, StatisticsIF statistics) { 111 this.statistics = statistics; 112 somethingHappened = true; 113 } 114 115 void reset() { 116 statistics = null; 117 somethingHappened = false; 118 } 119 120 public StatisticsIF getStatistics() { 121 return statistics; 122 } 123 124 void waitForSomethingToHappen() { 125 126 long start = System.currentTimeMillis(); 127 while (!somethingHappened) { 128 try { 129 Thread.sleep(500); 130 } catch (InterruptedException e) { 131 LOG.error("Awoken", e); 132 } 133 if (System.currentTimeMillis() - start > 30000) { 134 fail("Timeout waiting for something to happen"); 135 } 136 } 137 138 } 139 140 StatisticsIF getNextStatistics() { 141 somethingHappened = false; 142 waitForSomethingToHappen(); 143 return statistics; 144 } 145 146 } 147 } 148 149 210 | Popular Tags |