}
}
public void testNotificationDelivery() throws Exception
{
final MutableObject holder = new MutableObject(null);
final ObjectName name = ObjectName.getInstance(":name=emitter");
final int nextSequence = 1;
RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
{
public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
{
synchronized (holder)
{
Notification notification = new Notification("type", name, 0);
TargetedNotification targeted = new TargetedNotification(notification, new Integer(1));
if (sequenceNumber < 0)
{
// Return nextSequence as next sequence number: next call must have this sequence number
NotificationResult result1 = new NotificationResult(0, nextSequence, new TargetedNotification[]{targeted});
holder.set(result1);
return result1;
}
if (sequenceNumber == nextSequence)
{
NotificationResult result2 = new NotificationResult(1, nextSequence + 1, new TargetedNotification[]{targeted});
holder.set(result2);
return result2;
}
try
{
holder.wait(timeout);
}
catch (InterruptedException x)
{
Thread.currentThread().interrupt();
}
holder.set(null);
return null;
}
}
protected void sendConnectionNotificationLost(long number)
{
}
protected int getMaxRetries()
{
return 2;
}
protected long getRetryPeriod()
{
return 1000;
}
};
final MutableObject notifHolder = new MutableObject(null);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
notifHolder.set(notification);
}
};
try
{
handler.start();
handler.addNotificationListener(new Integer(1), new NotificationTuple(name, listener, null, null));
synchronized (holder)
{
// This wait time is much less than the sleep time for the fetcher thread
while (holder.get() == null) holder.wait(10);
NotificationResult result = (NotificationResult)holder.get();
assertEquals(result.getNextSequenceNumber(), nextSequence);
holder.set(null);
// Wait for the notification to arrive
while (notifHolder.get() == null) holder.wait(10);
Notification notif = (Notification)notifHolder.get();
assertEquals(notif.getSource(), name);
// Wait for the second fetchNotification call
while (holder.get() == null) holder.wait(10);
result = (NotificationResult)holder.get();