package test.stress.timer;
import java.util.Date;
import java.util.Random;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.timer.Timer;
import junit.framework.TestCase;
public class TimerTestCase
extends TestCase
implements NotificationListener
{
String TIMER_TYPE = "TimerType";
String MESSAGE = "Message";
String USER_DATA = "UserData";
MBeanServer server;
ObjectName timerName;
Timer timer;
int notifications = 0;
int target = 0;
int nextPercentage = 10;
public TimerTestCase(String s)
{
super(s);
}
public void testTortureOne()
throws Exception
{
target = TimerSUITE.TIMERS * TimerSUITE.NOTIFICATIONS;
System.err.println("Timer Torture One: target=" + target);
initTest();
try
{
initTimer();
startTimer();
nextPercentage = 10;
Random random = new Random();
for (int i = 0; i < TimerSUITE.TIMERS; i++)
{
addNotification(TimerSUITE.OFFSET,
random.nextInt(TimerSUITE.PERIOD),
TimerSUITE.NOTIFICATIONS);
}
for (int k = 0; k < target; k++)
{
Integer id = addNotification(Timer.ONE_HOUR, Timer.ONE_HOUR, 1);
timer.getAllNotificationIDs();
timer.getDate(id);
timer.getNbNotifications();;
timer.getNbOccurences(id);
timer.getNotificationIDs(TIMER_TYPE);
timer.getNotificationUserData(id);
timer.getPeriod(id);
timer.getSendPastNotifications();
timer.isActive();
timer.isEmpty();
timer.setSendPastNotifications(true);
removeNotification(id);
}
for (int j = 0; j < TimerSUITE.NOTIFICATIONS; j++)
{
if (notifications >= target)
break;
int lastNotifications = notifications;
sleep(TimerSUITE.PERIOD * 10);
if (lastNotifications == notifications)
{
sleep(TimerSUITE.PERIOD * 10);
if (lastNotifications == notifications)
break;
}
}
assertTrue(notifications == target);
}
finally
{
endTest();
}
}
private void initTest()
{
notifications = 0;
server = MBeanServerFactory.createMBeanServer();
}
private void endTest()
throws Exception
{
server.removeNotificationListener(timerName, this);
stopTimer();
MBeanServerFactory.releaseMBeanServer(server);
}
private void initTimer()
{
try
{
timer = new Timer();
timerName = new ObjectName("test:type=timer");
server.registerMBean(timer, timerName);
server.addNotificationListener(timerName, this, null, null);
}
catch (Exception e)
{
fail(e.toString());
}
}
private void startTimer()
{
timer.start();
}
private void stopTimer()
{
timer.removeAllNotifications();
timer.stop();
}
private Integer addNotification(long offset, long period, long occurs)
{
return timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
timeOffset(offset), period, occurs);
}
private void removeNotification(Integer id)
throws Exception
{
timer.removeNotification(id);
}
public synchronized void handleNotification(Notification n, Object ignored)
{
notifications++;
float percentage = 100 * notifications / target;
if (percentage >= nextPercentage)
{
System.err.println("Done " + nextPercentage + "%");
nextPercentage += 10;
}
}
private Date timeOffset(long offset)
{
return new Date(System.currentTimeMillis() + offset);
}
private void sleep(long time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException ignored)
{
}
}
}