1 22 package org.jboss.test.jbossmx.compliance.timer; 23 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.MBeanServerInvocationHandler ; 30 import javax.management.Notification ; 31 import javax.management.NotificationListener ; 32 import javax.management.ObjectName ; 33 import javax.management.timer.TimerMBean ; 34 35 import org.jboss.test.jbossmx.compliance.TestCase; 36 37 38 64 public class PeriodTestCase extends TestCase 65 implements NotificationListener 66 { 67 private final long PERIOD = 300; 68 private final long DELAY = 200; 69 private final long TIMES = 5; 70 private final long FIXED_RATE_TOTAL = PERIOD * TIMES + DELAY; 71 private final long FIXED_DELAY_TOTAL = (PERIOD + DELAY) * TIMES; 72 private final long ALLOWED_DIFFERENCE = 15; 73 74 75 private ObjectName timerName; 76 77 78 private MBeanServer server; 79 80 81 private long startTime; 82 83 84 private ArrayList receivedNotifications = new ArrayList (); 85 86 88 public PeriodTestCase(String s) 89 { 90 super(s); 91 } 92 93 95 98 public void testFixedDelay() throws Exception 99 { 100 try 101 { 102 startTimerService(); 103 TimerMBean timer = (TimerMBean )MBeanServerInvocationHandler.newProxyInstance(server, timerName, TimerMBean .class, false); 104 105 startTime = System.currentTimeMillis(); 107 108 timer.addNotification("timer.notification", null, null, new Date (startTime + PERIOD), PERIOD, TIMES); 111 112 long expectedDuration = FIXED_DELAY_TOTAL; 113 waitForNotifications(TIMES, expectedDuration * 2); 114 115 long testDuration = System.currentTimeMillis() - startTime; 116 checkTimeDifference(expectedDuration, testDuration, ALLOWED_DIFFERENCE); 117 } 118 finally 119 { 120 stopTimerService(); 121 } 122 } 123 124 127 public void testFixedRate() throws Exception 128 { 129 try 130 { 131 startTimerService(); 132 TimerMBean timer = (TimerMBean )MBeanServerInvocationHandler.newProxyInstance(server, timerName, TimerMBean .class, false); 133 134 startTime = System.currentTimeMillis(); 136 137 timer.addNotification("timer.notification", null, null, new Date (startTime + PERIOD), PERIOD, TIMES, true); 140 141 long expectedDuration = FIXED_RATE_TOTAL; 142 waitForNotifications(TIMES, expectedDuration * 2); 143 144 long testDuration = System.currentTimeMillis() - startTime; 145 checkTimeDifference(expectedDuration, testDuration, ALLOWED_DIFFERENCE); 146 } 147 finally 148 { 149 stopTimerService(); 150 } 151 } 152 153 public void handleNotification(Notification notification, Object handback) 154 { 155 try 156 { 157 long time = notification.getTimeStamp() - startTime; 158 long seqNo = notification.getSequenceNumber(); 159 log.debug("#" + seqNo + " (" + time + "ms) - " + notification); 160 161 Thread.sleep(DELAY); 163 } 164 catch (InterruptedException ignore) {} 165 166 synchronized (receivedNotifications) 167 { 168 receivedNotifications.add(notification); 169 170 if (receivedNotifications.size() >= TIMES) 172 receivedNotifications.notifyAll(); 173 } 174 } 175 176 178 private void checkTimeDifference(long expected, long actual, long percentage) 179 { 180 long actualDiff = (actual - expected) * 100 / expected; 181 log.debug("Actual time: " + actual + " msec, expected time: " + expected + " msecs"); 182 log.debug("Actual difference: " + actualDiff + "%, allowed: +/-" + percentage + "%"); 183 184 long diff = Math.abs(expected - actual); 185 long maxDeviation = expected / percentage; 186 187 if (diff > maxDeviation) 188 fail("Time difference larger than " + percentage + "%"); 189 } 190 191 private void waitForNotifications(long totalExpected, long wait) throws Exception 192 { 193 synchronized (receivedNotifications) 194 { 195 if (receivedNotifications.size() > totalExpected) 196 fail("too many notifications " + receivedNotifications.size()); 197 198 if (receivedNotifications.size() < totalExpected) 199 receivedNotifications.wait(wait); 200 } 201 assertEquals(totalExpected, receivedNotifications.size()); 202 } 203 204 208 private void startTimerService() throws Exception 209 { 210 server = MBeanServerFactory.createMBeanServer("Timer"); 211 timerName = new ObjectName ("Timer:type=TimerService"); 212 server.createMBean("javax.management.timer.Timer", timerName, new Object [0], new String [0]); 213 server.invoke(timerName, "start", new Object [0], new String [0]); 214 server.addNotificationListener(timerName, this, null, null); 215 receivedNotifications.clear(); 216 } 217 218 221 private void stopTimerService() 222 { 223 try 224 { 225 server.invoke(timerName, "removeAllNotifications", new Object [0], new String [0]); 226 server.invoke(timerName, "stop", new Object [0], new String [0]); 227 server.unregisterMBean(timerName); 228 MBeanServerFactory.releaseMBeanServer(server); 229 receivedNotifications.clear(); 230 } 231 catch (Exception ignored) {} 232 } 233 234 } | Popular Tags |