1 22 package test.stress.timer; 23 24 import java.util.Date ; 25 import java.util.Random ; 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.Timer ; 33 34 import junit.framework.TestCase; 35 36 50 public class TimerTestCase 51 extends TestCase 52 implements NotificationListener 53 { 54 56 String TIMER_TYPE = "TimerType"; 57 String MESSAGE = "Message"; 58 String USER_DATA = "UserData"; 59 60 62 65 MBeanServer server; 66 67 70 ObjectName timerName; 71 72 75 Timer timer; 76 77 80 int notifications = 0; 81 82 85 int target = 0; 86 87 90 int nextPercentage = 10; 91 92 94 97 public TimerTestCase(String s) 98 { 99 super(s); 100 } 101 102 104 107 public void testTortureOne() 108 throws Exception 109 { 110 target = TimerSUITE.TIMERS * TimerSUITE.NOTIFICATIONS; 111 System.err.println("Timer Torture One: target=" + target); 112 113 initTest(); 114 try 115 { 116 initTimer(); 117 startTimer(); 118 119 nextPercentage = 10; 121 Random random = new Random (); 122 for (int i = 0; i < TimerSUITE.TIMERS; i++) 123 { 124 addNotification(TimerSUITE.OFFSET, 125 random.nextInt(TimerSUITE.PERIOD), 126 TimerSUITE.NOTIFICATIONS); 127 } 128 129 for (int k = 0; k < target; k++) 131 { 132 Integer id = addNotification(Timer.ONE_HOUR, Timer.ONE_HOUR, 1); 133 timer.getAllNotificationIDs(); 134 timer.getDate(id); 135 timer.getNbNotifications();; 136 timer.getNbOccurences(id); 137 timer.getNotificationIDs(TIMER_TYPE); 138 timer.getNotificationUserData(id); 139 timer.getPeriod(id); 140 timer.getSendPastNotifications(); 141 timer.isActive(); 142 timer.isEmpty(); 143 timer.setSendPastNotifications(true); 144 removeNotification(id); 145 } 146 147 for (int j = 0; j < TimerSUITE.NOTIFICATIONS; j++) 149 { 150 if (notifications >= target) 151 break; 152 int lastNotifications = notifications; 153 sleep(TimerSUITE.PERIOD * 10); 154 if (lastNotifications == notifications) 155 { 156 sleep(TimerSUITE.PERIOD * 10); 157 if (lastNotifications == notifications) 158 break; 159 } 160 } 161 162 assertTrue(notifications == target); 164 } 165 finally 166 { 167 endTest(); 168 } 169 } 170 171 173 176 private void initTest() 177 { 178 notifications = 0; 179 server = MBeanServerFactory.createMBeanServer(); 180 } 181 182 185 private void endTest() 186 throws Exception 187 { 188 server.removeNotificationListener(timerName, this); 189 stopTimer(); 190 MBeanServerFactory.releaseMBeanServer(server); 191 } 192 193 196 private void initTimer() 197 { 198 try 199 { 200 timer = new Timer (); 201 timerName = new ObjectName ("test:type=timer"); 202 server.registerMBean(timer, timerName); 203 server.addNotificationListener(timerName, this, null, null); 204 } 205 catch (Exception e) 206 { 207 fail(e.toString()); 208 } 209 } 210 211 214 private void startTimer() 215 { 216 timer.start(); 217 } 218 219 222 private void stopTimer() 223 { 224 timer.removeAllNotifications(); 225 timer.stop(); 226 } 227 228 231 private Integer addNotification(long offset, long period, long occurs) 232 { 233 return timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA, 234 timeOffset(offset), period, occurs); 235 } 236 237 240 private void removeNotification(Integer id) 241 throws Exception 242 { 243 timer.removeNotification(id); 244 } 245 246 249 public synchronized void handleNotification(Notification n, Object ignored) 250 { 251 notifications++; 252 float percentage = 100 * notifications / target; 253 if (percentage >= nextPercentage) 254 { 255 System.err.println("Done " + nextPercentage + "%"); 256 nextPercentage += 10; 257 } 258 } 259 260 263 private Date timeOffset(long offset) 264 { 265 return new Date (System.currentTimeMillis() + offset); 266 } 267 268 271 private void sleep(long time) 272 { 273 try 274 { 275 Thread.sleep(time); 276 } 277 catch (InterruptedException ignored) 278 { 279 } 280 } 281 } 282 | Popular Tags |