1 22 package org.jboss.test.jbossmx.compliance.timer; 23 24 import org.jboss.test.jbossmx.compliance.TestCase; 25 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.Notification ; 32 import javax.management.NotificationListener ; 33 import javax.management.ObjectName ; 34 import javax.management.timer.TimerNotification ; 35 36 44 public class BasicTestCase 45 extends TestCase 46 implements NotificationListener 47 { 48 50 53 ObjectName timerName; 54 55 58 MBeanServer server; 59 60 63 ArrayList receivedNotifications = new ArrayList (); 64 65 67 public BasicTestCase(String s) 68 { 69 super(s); 70 } 71 72 74 77 public void testSingleNotification() 78 throws Exception 79 { 80 try 81 { 82 startTimerService(); 83 84 Integer id = addNotification("test", "hello", "data", calcTime(PERIOD), 85 0, 1); 86 expectNotifications(1); 87 88 } 92 finally 93 { 94 stopTimerService(); 95 } 96 } 97 98 101 public void testRepeatedNotification() 102 throws Exception 103 { 104 try 105 { 106 startTimerService(); 107 Integer id = addNotification("test", "hello", "data", calcTime(PERIOD), 108 PERIOD, REPEATS); 109 expectNotifications(1); 110 expectNotifications(2); 111 112 } 116 finally 117 { 118 stopTimerService(); 119 } 120 } 121 122 125 public void testInfiniteNotification() 126 throws Exception 127 { 128 try 129 { 130 startTimerService(); 131 132 Integer id = addNotification("test", "hello", "data", calcTime(PERIOD), 133 PERIOD, 0); 134 expectNotifications(1); 135 expectNotifications(2); 136 137 if (getNotificationType(id) == null) 138 fail("Infinite notification not registered"); 139 } 140 finally 141 { 142 stopTimerService(); 143 } 144 } 145 146 149 public void testTwoNotificationProducers() 150 throws Exception 151 { 152 try 153 { 154 startTimerService(); 155 long lTimeOne = 5 * 1000; 156 long lTimeTwo = 12 * 1000; 157 long lWait = 2 * lTimeTwo; 158 long lStart = calcTime( lTimeOne ); 159 Integer lIdOne = addNotification( "test-2", "hello", "data", lStart + lTimeOne, 160 lTimeOne, 0); 161 Integer lIdTwo = addNotification( "test-2", "hello", "data", lStart + lTimeTwo, 162 lTimeTwo, 0); 163 164 expectNotifications( 1, lWait ); 165 expectNotifications( 2, lWait ); 166 TimerNotification lNotificationOne = (TimerNotification ) receivedNotifications.get( 0 ); 168 TimerNotification lNotificationTwo = (TimerNotification ) receivedNotifications.get( 1 ); 169 checkNotificationID( lNotificationOne, lIdOne ); 170 checkNotificationID( lNotificationTwo, lIdOne ); 171 checkTimeDifference( lNotificationOne, lNotificationTwo, lTimeOne ); 172 173 expectNotifications( 3, lWait ); 174 lNotificationOne = lNotificationTwo; 175 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 2 ); 176 checkNotificationID( lNotificationTwo, lIdTwo ); 177 checkTimeDifference( lNotificationOne, lNotificationTwo, ( lTimeTwo - ( 2 * lTimeOne ) ) ); 178 179 expectNotifications( 4, lWait ); 180 lNotificationOne = lNotificationTwo; 181 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 3 ); 182 checkNotificationID( lNotificationTwo, lIdOne ); 183 checkTimeDifference( lNotificationOne, lNotificationTwo, ( ( 3 * lTimeOne ) - lTimeTwo ) ); 184 185 expectNotifications( 5, lWait ); 186 lNotificationOne = lNotificationTwo; 187 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 4 ); 188 checkNotificationID( lNotificationTwo, lIdOne ); 189 checkTimeDifference( lNotificationOne, lNotificationTwo, lTimeOne ); 190 191 expectNotifications( 6, lWait ); 192 lNotificationOne = lNotificationTwo; 193 lNotificationTwo = (TimerNotification ) receivedNotifications.get( 5 ); 194 checkNotificationID( lNotificationTwo, lIdTwo ); 195 checkTimeDifference( lNotificationOne, lNotificationTwo, ( ( 2 * lTimeTwo ) - ( 4 * lTimeOne ) ) ); 196 } 197 finally 198 { 199 stopTimerService(); 200 } 201 } 202 203 205 209 private void startTimerService() 210 throws Exception 211 { 212 server = MBeanServerFactory.createMBeanServer("Timer"); 213 214 timerName = new ObjectName ("Timer:type=TimerService"); 215 server.createMBean("javax.management.timer.Timer", timerName, 216 new Object [0], new String [0]); 217 server.invoke(timerName, "start", new Object [0], new String [0]); 218 219 receivedNotifications.clear(); 220 server.addNotificationListener(timerName, this, null, null); 221 } 222 223 228 private void stopTimerService() 229 { 230 try 231 { 232 server.invoke(timerName, "removeAllNotifications", new Object [0], new String [0]); 233 server.invoke(timerName, "stop", new Object [0], new String [0]); 234 server.unregisterMBean(timerName); 235 MBeanServerFactory.releaseMBeanServer(server); 236 } 237 catch (Exception ignored) {} 238 } 239 240 246 public void handleNotification(Notification notification, Object handback) 247 { 248 synchronized (receivedNotifications) 249 { 250 receivedNotifications.add(notification); 251 receivedNotifications.notifyAll(); 252 } 253 } 254 255 262 public void expectNotifications(int expected) 263 throws Exception 264 { 265 expectNotifications( expected, WAIT ); 266 } 267 268 276 public void expectNotifications(int expected, long wait) 277 throws Exception 278 { 279 synchronized (receivedNotifications) 280 { 281 if (receivedNotifications.size() > expected) 282 fail("too many notifications"); 283 if (receivedNotifications.size() < expected) 284 { 285 receivedNotifications.wait( wait ); 286 } 287 assertEquals(expected, receivedNotifications.size()); 288 } 289 } 290 291 298 public void checkNotificationID( TimerNotification pNotification, Integer pNotificationID ) { 299 if( pNotification == null ) { 300 fail( "Notification is null" ); 301 } 302 if( !pNotification.getNotificationID().equals( pNotificationID ) ) { 303 fail( "Wrong Notification ID received: " + pNotification.getNotificationID() + 304 ", expected: " + pNotificationID ); 305 } 306 } 307 308 316 public void checkTimeDifference( 317 TimerNotification pNotificationOne, 318 TimerNotification pNotificationTwo, 319 long pTimeDiffernce 320 ) { 321 long lDiff = pNotificationTwo.getTimeStamp() - pNotificationOne.getTimeStamp(); 322 if( lDiff < ( pTimeDiffernce - ( pTimeDiffernce / 10 ) ) || 323 lDiff > ( pTimeDiffernce + ( pTimeDiffernce / 10 ) ) 324 ) { 325 fail( "Time between first two notification is too small or too big: " + pTimeDiffernce ); 326 } 327 } 328 329 340 private Integer addNotification(String type, String message, String data, 341 long time, long period, long occurs) 342 throws Exception 343 { 344 return (Integer ) server.invoke(timerName, "addNotification", 345 new Object [] { type, message, data, new Date (time), new Long (period), 346 new Long (occurs) }, 347 new String [] { "java.lang.String", "java.lang.String", "java.lang.Object", 348 "java.util.Date", "long", "long" } ); 349 } 350 351 357 private String getNotificationType(Integer id) 358 throws Exception 359 { 360 Thread.yield(); 365 366 return (String ) server.invoke(timerName, "getNotificationType", 367 new Object [] { id }, 368 new String [] { "java.lang.Integer" }); 369 } 370 371 376 private long calcTime(long offset) 377 { 378 return System.currentTimeMillis() + offset; 379 } 380 } 381 | Popular Tags |