Package test

Examples of test.MutableLong


   }

   public void testNotificationsLost() throws Exception
   {
      final MutableObject holder = new MutableObject(null);
      final MutableLong lost = new MutableLong(0);
      final ObjectName name = ObjectName.getInstance(":name=emitter");
      final long losts = 1;
      RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
      {
         public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
         {
            synchronized (holder)
            {
               if (sequenceNumber < 0) return new NotificationResult(0, 0, new TargetedNotification[0]);

               // Avoid to spin loop the fetcher thread
               sleep(1000);

               // Return a earliest sequence greater than the requested, to test notification lost behavior
               return new NotificationResult(losts, losts, new TargetedNotification[0]);
            }
         }

         protected void sendConnectionNotificationLost(long number)
         {
            synchronized (holder)
            {
               lost.set(number);
            }
         }

         protected int getMaxRetries()
         {
            return 2;
         }

         protected long getRetryPeriod()
         {
            return 1000;
         }
      };

      Integer id = new Integer(1);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
         }
      };

      try
      {
         handler.start();

         handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null));

         synchronized (holder)
         {
            while (lost.get() == 0) holder.wait(10);
            assertEquals(lost.get(), losts);
         }

         handler.removeNotificationListeners(new Integer[]{id});
      }
      finally
View Full Code Here


      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();
View Full Code Here

      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);
            }
         }
      };
View Full Code Here

TOP

Related Classes of test.MutableLong

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.