final int occurrences = 100;
final String fdNotifType = "timer-test-fixed-delay";
final String frNotifType = "timer-test-fixed-rate";
final MutableInteger frOccurrences = new MutableInteger(0);
final MutableLong frElapsedTime = new MutableLong(0);
final MutableLong frLastTime = new MutableLong(System.currentTimeMillis());
NotificationListener frListener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
if (frOccurrences.get() < occurrences)
{
long now = System.currentTimeMillis();
frElapsedTime.set(frElapsedTime.get() + (now - frLastTime.get()));
frLastTime.set(now);
frOccurrences.set(frOccurrences.get() + 1);
}
}
};
final MutableInteger fdOccurrences = new MutableInteger(0);
final MutableLong fdElapsedTime = new MutableLong(0);
final MutableLong fdLastTime = new MutableLong(System.currentTimeMillis());
NotificationListener fdListener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
if (fdOccurrences.get() < occurrences)
{
long now = System.currentTimeMillis();
fdElapsedTime.set(fdElapsedTime.get() + (now - fdLastTime.get()));
fdLastTime.set(now);
fdOccurrences.set(fdOccurrences.get() + 1);
}
}
};
m_server.addNotificationListener(m_timerName, fdListener, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return notification.getType().equals(fdNotifType);
}
}, null);
m_server.addNotificationListener(m_timerName, frListener, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return notification.getType().equals(frNotifType);
}
}, null);
// Testing fixed delay/fixed rate
long now = System.currentTimeMillis();
// Notify in one second
Date date = new Date(now + Timer.ONE_SECOND);
// Register to happen 10 times
m_timer.addNotification(fdNotifType, "timer-message", "user-data", date, 10, occurrences, false);
m_timer.addNotification(frNotifType, "timer-message", "user-data", date, 10, occurrences, true);
// Sleep some time
while (frOccurrences.get() < occurrences || fdOccurrences.get() < occurrences)
{
sleep(10);
System.gc();
}
assertEquals(frOccurrences.get(), occurrences);
assertEquals(fdOccurrences.get(), occurrences);
if (((1.0f * frElapsedTime.get()) / fdElapsedTime.get()) > 0.95)
fail("Fixed rate and fixed delay exhibit no execution rate differences");
}