mx4j/src/test/test/mx4j/remote RemoteNotificationClientHandlerTest.java,1.6,1.7

Simone Bordet <[email protected]>
Newsgroups gmane.comp.java.mx4j.cvs
Message-ID <[email protected]>
Update of /cvsroot/mx4j/mx4j/src/test/test/mx4j/remote
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31629/src/test/test/mx4j/remote

Modified Files:
	RemoteNotificationClientHandlerTest.java 
Log Message:
Added test for bug #1004412: put a limit to client notification queue

Index: RemoteNotificationClientHandlerTest.java
===================================================================
RCS file: /cvsroot/mx4j/mx4j/src/test/test/mx4j/remote/RemoteNotificationClientHandlerTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** RemoteNotificationClientHandlerTest.java	1 Mar 2004 18:24:16 -0000	1.6
--- RemoteNotificationClientHandlerTest.java	10 Aug 2004 09:23:09 -0000	1.7
***************
*** 10,13 ****
--- 10,15 ----
  
  import java.io.IOException;
+ import java.util.HashMap;
+ import java.util.Map;
  import javax.management.Notification;
  import javax.management.NotificationListener;
***************
*** 17,23 ****
--- 19,27 ----
  
  import mx4j.remote.AbstractRemoteNotificationClientHandler;
+ import mx4j.remote.MX4JRemoteConstants;
  import mx4j.remote.NotificationTuple;
  import mx4j.remote.RemoteNotificationClientHandler;
  import test.MX4JTestCase;
+ import test.MutableBoolean;
  import test.MutableLong;
  import test.MutableObject;
***************
*** 43,47 ****
        AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          protected NotificationResult fetchNotifications(long sequence, int maxNumber, long timeout) throws IOException
           {
              sleep(timeout);
--- 47,51 ----
        AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          protected NotificationResult fetchNotifications(long sequence, int maxNumber, long timeout)
           {
              sleep(timeout);
***************
*** 100,104 ****
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) throws IOException
           {
              sleep(timeout);
--- 104,108 ----
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
           {
              sleep(timeout);
***************
*** 160,164 ****
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) throws IOException
           {
              synchronized (holder)
--- 164,168 ----
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
           {
              synchronized (holder)
***************
*** 260,264 ****
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) throws IOException
           {
              synchronized (holder)
--- 264,268 ----
        RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
        {
!          public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
           {
              synchronized (holder)
***************
*** 371,373 ****
--- 375,486 ----
        }
     }
+ 
+    public void testQueueOverflow() throws Exception
+    {
+       final Object lock = new Object();
+       int queueCapacity = 10;
+       final int count = 4;
+       final long sleep = 500;
+       final Integer id = new Integer(1);
+       final ObjectName name = ObjectName.getInstance(":name=emitter");
+       final MutableBoolean notify = new MutableBoolean(true);
+       final MutableLong queued = new MutableLong(0);
+       final MutableLong delivered = new MutableLong(0);
+ 
+       Map environment = new HashMap();
+       environment.put(MX4JRemoteConstants.NOTIFICATION_QUEUE_CAPACITY, new Integer(queueCapacity));
+       RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, environment)
+       {
+          protected NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
+          {
+             if (sequenceNumber < 0) return new NotificationResult(0, 0, new TargetedNotification[0]);
+ 
+             boolean doNotify = false;
+             synchronized (lock)
+             {
+                doNotify = notify.get();
+             }
+ 
+             if (doNotify)
+             {
+                // Avoid spin looping the fetcher thread, but don't sleep too much, we have to fill the client's queue
+                sleep(sleep);
+                TargetedNotification[] notifications = new TargetedNotification[count];
+                for (int i = 0; i < count; ++i) notifications[i] = new TargetedNotification(new Notification("type", name, sequenceNumber + i), id);
+                long nextSequence = sequenceNumber + count;
+                NotificationResult result = new NotificationResult(0, nextSequence, notifications);
+                synchronized (lock)
+                {
+                   queued.set(getNotificationsCount());
+                }
+                return result;
+             }
+             else
+             {
+                sleep(timeout);
+                return new NotificationResult(0, sequenceNumber, new TargetedNotification[0]);
+             }
+          }
+ 
+          protected long getRetryPeriod()
+          {
+             return 1000;
+          }
+ 
+          protected int getMaxRetries()
+          {
+             return 5;
+          }
+ 
+          protected void sendConnectionNotificationLost(long number)
+          {
+             System.out.println("Lost notifications: " + number);
+             // Stop sending notifications
+             synchronized (lock)
+             {
+                notify.set(false);
+                // Deliver notifications until the last we queued on the client
+                queued.set(getNotificationsCount());
+             }
+          }
+       };
+ 
+       NotificationListener listener = new NotificationListener()
+       {
+          public void handleNotification(Notification notification, Object handback)
+          {
+             long sequence = notification.getSequenceNumber();
+             synchronized (lock)
+             {
+                delivered.set(sequence);
+             }
+             System.out.println("Received notification, sequence is " + sequence);
+             // Sleep longer than notification emission, to fill the client's queue
+             sleep(sleep * 2);
+             System.out.println("Handled notification, sequence is " + sequence);
+          }
+       };
+ 
+       try
+       {
+          handler.start();
+          handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null));
+          // Wait until we empty the client's queue
+          synchronized (lock)
+          {
+             while (notify.get())
+             {
+                lock.wait(50);
+                if (queued.get() > queueCapacity) fail("Queued notifications " + queued.get() + " must not pass max capacity " + queueCapacity);
+             }
+ 
+             // Test timeouts if we don't deliver everything
+             while (delivered.get() < queued.get()) lock.wait(10);
+          }
+       }
+       finally
+       {
+          handler.stop();
+       }
+    }
  }



-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.