m_timer.start();
final String notifType = "timer-test";
final String periodicNotifType = "timer-test-periodic";
final MutableInteger occurrencesCount = new MutableInteger(0);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
occurrencesCount.set(occurrencesCount.get() + 1);
}
};
final MutableInteger periodicOccurrences = new MutableInteger(0);
NotificationListener periodicListener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
periodicOccurrences.set(periodicOccurrences.get() + 1);
}
};
m_server.addNotificationListener(m_timerName, listener, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return notification.getType().equals(notifType);
}
}, null);
m_server.addNotificationListener(m_timerName, periodicListener, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return notification.getType().equals(periodicNotifType);
}
}, null);
// Register to happen 3 times on the first listener
long now = System.currentTimeMillis();
// Notify in one second
Date date = new Date(now + Timer.ONE_SECOND);
String message = "timer-message";
Integer id = m_timer.addNotification(notifType, message, "user-data", date, Timer.ONE_SECOND, 3L);
// Register to happen periodically
// Notify in one second
date = new Date(now + Timer.ONE_SECOND);
String userDataPeriodic = "user-data-periodic";
Integer periodicID = m_timer.addNotification(periodicNotifType, "timer-message-periodic", userDataPeriodic, date, Timer.ONE_SECOND);
// Sleep some time
sleep(Timer.ONE_SECOND);
Vector v = m_timer.getAllNotificationIDs();
assertEquals(v.size(), 2);
assertTrue(v.contains(id));
assertTrue(v.contains(periodicID));
v = m_timer.getNotificationIDs(periodicNotifType);
assertEquals(v.size(), 1);
assertTrue(v.contains(periodicID));
assertEquals(m_timer.getNotificationMessage(id), message);
assertEquals(m_timer.getNotificationUserData(periodicID), userDataPeriodic);
// Sleep till the end of the three-time notification
sleep(Timer.ONE_SECOND * 6);
// Check that was called the right number of times
assertEquals(occurrencesCount.get(), 3);
// The three-time notification is expired now
v = m_timer.getAllNotificationIDs();
assertEquals(v.size(), 1);
assertTrue(v.contains(periodicID));
Long p = m_timer.getPeriod(periodicID);
assertEquals(p.longValue(), Timer.ONE_SECOND);
assertEquals(m_timer.getNotificationType(periodicID), periodicNotifType);
// Removing non existing notification
try
{
m_timer.removeNotifications("dummy");
fail("Removed non-existing notification");
}
catch (InstanceNotFoundException ignored)
{
}
// Should have already been removed, was the three-shot notification
try
{
m_timer.removeNotification(id);
fail("Removed non-existing notification");
}
catch (InstanceNotFoundException ignored)
{
}
// Some more wait
sleep(Timer.ONE_SECOND * 3);
// Removing existing notification
m_timer.removeNotification(periodicID);
// Check that none are still present
assertTrue(m_timer.isEmpty());
// Wait some more to be sure the periodic listener is not notified anymore
int periodTimes = periodicOccurrences.get();
assertTrue(periodTimes > 0);
sleep(Timer.ONE_SECOND * 5);
assertEquals(periodicOccurrences.get(), periodTimes);
}