1 22 package org.jboss.test.jmx.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.Notification ; 30 import javax.management.NotificationListener ; 31 import javax.management.ObjectName ; 32 import javax.management.timer.TimerNotification ; 33 34 import junit.framework.TestCase; 35 36 44 public class TimerTest 45 extends TestCase 46 implements NotificationListener 47 { 48 50 public static final long PERIOD = 100; 51 public static final long WAIT = 10000; 52 54 public static final long REPEATS = 2; 55 56 58 61 ObjectName timerName; 62 63 66 MBeanServer server; 67 68 71 ArrayList receivedNotifications = new ArrayList (); 72 73 75 public TimerTest(String s) 76 { 77 super(s); 78 } 79 80 82 85 public void testSingleNotification() 86 throws Exception 87 { 88 try 89 { 90 startTimerService(); 91 92 addNotification("test", "hello", "data", calcTime(PERIOD), 93 0, 1); 94 expectNotifications(1); 95 96 } 100 finally 101 { 102 stopTimerService(); 103 } 104 } 105 106 109 public void testRepeatedNotification() 110 throws Exception 111 { 112 try 113 { 114 startTimerService(); 115 addNotification("test", "hello", "data", calcTime(PERIOD), 116 PERIOD, REPEATS); 117 expectNotifications(1); 118 expectNotifications(2); 119 120 } 124 finally 125 { 126 stopTimerService(); 127 } 128 } 129 130 133 public void testInfiniteNotification() 134 throws Exception 135 { 136 try 137 { 138 startTimerService(); 139 140 Integer id = addNotification("test", "hello", "data", calcTime(PERIOD), 141 PERIOD, 0); 142 expectNotifications(1); 143 expectNotifications(2); 144 145 if (getNotificationType(id) == null) 146 fail("Infinite notification not registered"); 147 } 148 finally 149 { 150 stopTimerService(); 151 } 152 } 153 154 157 public void testTwoNotificationProducers() 158 throws Exception 159 { 160 try 161 { 162 startTimerService(); 163 long lTimeOne = 5 * 1000; 164 long lTimeTwo = 12 * 1000; 165 long lWait = 2 * lTimeTwo; 166 long lStart = calcTime( lTimeOne ); 167 Integer lIdOne = addNotification( "test-2", "hello", "data", lStart + lTimeOne, 168 lTimeOne, 0); 169 Integer lIdTwo = addNotification( "test-2", "hello", "data", lStart + lTimeTwo, 170 lTimeTwo, 0); 171 172 expectNotifications( 1, lWait ); 173 expectNotifications( 2, lWait ); 174 TimerNotification lNotificationOne = (TimerNotification ) receivedNotifications.get( 0 ); 176 TimerNotification lNotificationTwo = (TimerNotification ) receivedNotifications.get( 1 ); 177 checkNotificationID( lNotificationOne, lIdOne ); 178 checkNotificationID( lNotificationTwo, lIdOne ); 179 checkTimeDifference( lNotificationOne, lNotificationTwo, lTimeOne ); 180 181 expectNotifications( 3, lWait ); 182 lNotificationOne = lNotificationTwo; 183 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 2 ); 184 checkNotificationID( lNotificationTwo, lIdTwo ); 185 checkTimeDifference( lNotificationOne, lNotificationTwo, ( lTimeTwo - ( 2 * lTimeOne ) ) ); 186 187 expectNotifications( 4, lWait ); 188 lNotificationOne = lNotificationTwo; 189 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 3 ); 190 checkNotificationID( lNotificationTwo, lIdOne ); 191 checkTimeDifference( lNotificationOne, lNotificationTwo, ( ( 3 * lTimeOne ) - lTimeTwo ) ); 192 193 expectNotifications( 5, lWait ); 194 lNotificationOne = lNotificationTwo; 195 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 4 ); 196 checkNotificationID( lNotificationTwo, lIdOne ); 197 checkTimeDifference( lNotificationOne, lNotificationTwo, lTimeOne ); 198 199 expectNotifications( 6, lWait ); 200 lNotificationOne = lNotificationTwo; 201 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 5 ); 202 checkNotificationID( lNotificationTwo, lIdTwo ); 203 checkTimeDifference( lNotificationOne, lNotificationTwo, ( ( 2 * lTimeTwo ) - ( 4 * lTimeOne ) ) ); 204 } 205 finally 206 { 207 stopTimerService(); 208 } 209 } 210 211 213 217 private void startTimerService() 218 throws Exception 219 { 220 server = MBeanServerFactory.createMBeanServer("Timer"); 221 222 timerName = new ObjectName ("Timer:type=TimerService"); 223 server.createMBean("javax.management.timer.Timer", timerName, 224 new Object [0], new String [0]); 225 server.invoke(timerName, "start", new Object [0], new String [0]); 226 227 receivedNotifications.clear(); 228 server.addNotificationListener(timerName, this, null, null); 229 } 230 231 236 private void stopTimerService() 237 { 238 try 239 { 240 server.invoke(timerName, "removeAllNotifications", new Object [0], new String [0]); 241 server.invoke(timerName, "stop", new Object [0], new String [0]); 242 server.unregisterMBean(timerName); 243 MBeanServerFactory.releaseMBeanServer(server); 244 } 245 catch (Exception ignored) {} 246 } 247 248 254 public void handleNotification(Notification notification, Object handback) 255 { 256 synchronized (receivedNotifications) 257 { 258 receivedNotifications.add(notification); 259 receivedNotifications.notifyAll(); 260 } 261 } 262 263 270 public void expectNotifications(int expected) 271 throws Exception 272 { 273 expectNotifications( expected, WAIT ); 274 } 275 276 284 public void expectNotifications(int expected, long wait) 285 throws Exception 286 { 287 synchronized (receivedNotifications) 288 { 289 if (receivedNotifications.size() > expected) 290 fail("too many notifications"); 291 if (receivedNotifications.size() < expected) 292 { 293 receivedNotifications.wait( wait ); 294 } 295 assertEquals(expected, receivedNotifications.size()); 296 } 297 } 298 299 306 public void checkNotificationID( TimerNotification pNotification, Integer pNotificationID ) { 307 if( pNotification == null ) { 308 fail( "Notification is null" ); 309 } 310 if( !pNotification.getNotificationID().equals( pNotificationID ) ) { 311 fail( "Wrong Notification ID received: " + pNotification.getNotificationID() + 312 ", expected: " + pNotificationID ); 313 } 314 } 315 316 324 public void checkTimeDifference( 325 TimerNotification pNotificationOne, 326 TimerNotification pNotificationTwo, 327 long pTimeDiffernce 328 ) { 329 long lDiff = pNotificationTwo.getTimeStamp() - pNotificationOne.getTimeStamp(); 330 if( lDiff < ( pTimeDiffernce - ( pTimeDiffernce / 10 ) ) || 331 lDiff > ( pTimeDiffernce + ( pTimeDiffernce / 10 ) ) 332 ) { 333 fail( "Time between first two notification is too small or too big: " + pTimeDiffernce ); 334 } 335 } 336 337 348 private Integer addNotification(String type, String message, String data, 349 long time, long period, long occurs) 350 throws Exception 351 { 352 return (Integer ) server.invoke(timerName, "addNotification", 353 new Object [] { type, message, data, new Date (time), new Long (period), 354 new Long (occurs) }, 355 new String [] { "java.lang.String", "java.lang.String", "java.lang.Object", 356 "java.util.Date", "long", "long" } ); 357 } 358 359 365 private String getNotificationType(Integer id) 366 throws Exception 367 { 368 Thread.yield(); 373 374 return (String ) server.invoke(timerName, "getNotificationType", 375 new Object [] { id }, 376 new String [] { "java.lang.Integer" }); 377 } 378 379 384 private long calcTime(long offset) 385 { 386 return System.currentTimeMillis() + offset; 387 } 388 } 389 | Popular Tags |