Package test

Examples of test.MutableObject


      // No modulus, no offset

      counter.setIntegerCounter(initThreshold.intValue() - 1);

      final MutableInteger times = new MutableInteger(0);
      final MutableObject holder = new MutableObject(null);
      monitor.addNotificationListener(new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            times.set(times.get() + 1);
            holder.set(notification);
         }
      }, null, null);
      monitor.start();

      try
      {
         // Below threshold, no notifications should be sent
         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());

         // Above threshold, just one notification should be sent
         counter.setIntegerCounter(initThreshold.intValue() + 1);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);

         times.set(0);
         holder.set(null);
         sleep(period * 3);
         assertEquals(times.get(), 0);
      }
      finally
      {
View Full Code Here


      // No modulus

      counter.setIntegerCounter(initThreshold.intValue() - 1);

      final MutableInteger times = new MutableInteger(0);
      final MutableObject holder = new MutableObject(null);
      monitor.addNotificationListener(new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            times.set(times.get() + 1);
            holder.set(notification);
         }
      }, null, null);
      monitor.start();

      try
      {
         // Below threshold, no notifications should be sent
         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());

         // Above threshold, just one notification should be sent
         counter.setIntegerCounter(initThreshold.intValue() + 1);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
         // The threshold should have offset
         Number threshold = monitor.getThreshold(counterName);
         assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue());

         times.set(0);
         holder.set(null);
         sleep(period * 3);
         assertEquals(times.get(), 0);

         // Above threshold by more than 1 offset
         counter.setIntegerCounter(initThreshold.intValue() + offset.intValue() * 2 + 1);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
         // The threshold should have offset correctly
         threshold = monitor.getThreshold(counterName);
         assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue() * 3);

         times.set(0);
         holder.set(null);
         sleep(period * 3);
         assertEquals(times.get(), 0);
      }
      finally
      {
View Full Code Here

      monitor.setNotify(true);
      // No modulus, no offset

      counter.setIntegerCounter(initThreshold.intValue() - 1);

      final MutableObject monitorThread = new MutableObject(null);
      monitor.addNotificationListener(new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            monitorThread.set(Thread.currentThread());
         }
      }, null, null);
      monitor.start();

      // Below threshold, no notifications should be sent
      sleep(period * 3);

      // Above threshold, just one notification should be sent
      counter.setIntegerCounter(initThreshold.intValue() + 1);
      sleep(period * 3);

      Thread thread = (Thread)monitorThread.get();
      assertNotNull(thread);
      assertNotSame(thread, Thread.currentThread());

      monitor.stop();

View Full Code Here

      }
   }

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

      }
   }

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

   }

   public void testListenerRegistrationUnregistrationDuringCallbacks() throws Exception
   {
      MBeanServer server = newMBeanServer();
      MutableObject holder = new MutableObject(null);
      Object mbean = new RegistrationSupport.ListenerRegistrar(holder);
      ObjectName name = ObjectName.getInstance("test:type=notifications");
      server.registerMBean(mbean, name);

      // Register a new MBean, the holder must be notified
      ObjectName mlet = ObjectName.getInstance("test:type=mlet");
      server.createMBean(MLet.class.getName(), mlet, null);

      Notification notification = (Notification)holder.get();
      assertNotNull(notification);
      assertEquals(notification.getType(), MBeanServerNotification.REGISTRATION_NOTIFICATION);
      holder.set(null);

      server.unregisterMBean(mlet);

      notification = (Notification)holder.get();
      assertNotNull(notification);
      assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
      holder.set(null);

      // Unregisters also the listeners (in postDeregister)
      server.unregisterMBean(name);
      notification = (Notification)holder.get();
      assertNotNull(notification);
      assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
      holder.set(null);

      server.createMBean(MLet.class.getName(), mlet, null);
      notification = (Notification)holder.get();
      assertNull(notification);

      server.unregisterMBean(mlet);
      notification = (Notification)holder.get();
      assertNull(notification);
   }
View Full Code Here

      MonitorTarget target = new MonitorTarget();
      target.setString(reference);
      server.registerMBean(target, name);

      final MutableInteger times = new MutableInteger(0);
      final MutableObject holder = new MutableObject(null);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            times.set(times.get() + 1);
            holder.set(notification);
         }
      };
      server.addNotificationListener(monitorName, listener, null, null);

      monitor.start();

      try
      {
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);

         times.set(0);
         holder.set(null);
         target.setString("xx");

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);

         times.set(0);
         holder.set(null);
         target.setString(reference);

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);

         times.set(0);
         holder.set(null);
         target.setString("yyyy");

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);

         times.set(0);
         holder.set(null);
         target.setString("zzzzz");

         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());
      }
      finally
      {
         monitor.stop();
      }
View Full Code Here

      RemoteMBeanProxy proxy = new RemoteMBeanProxy(remoteDelegateName, cntor, null, null);
      ObjectName proxyName = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteDelegateName.getCanonicalName()));
      localServer.registerMBean(proxy, proxyName);

      // Register a listener to the MBean proxy for the remote delegate
      final MutableObject holder = new MutableObject(null);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            synchronized (holder)
            {
               holder.set(notification);
            }
         }
      };
      localServer.addNotificationListener(proxyName, listener, null, null);

      // Sleep to make sure the remote notifications threads started before we emit the notification
      sleep(1000);

      // Add an MBean to the remote MBeanServer: this will trigger a notification from the remote delegate,
      // that should be dispatched transparently the the listener above
      MLet remoteMLet = new MLet();
      ObjectName remoteMLetName = ObjectName.getInstance(":type=mlet");
      remoteServer.registerMBean(remoteMLet, remoteMLetName);

      synchronized (holder)
      {
         while (holder.get() == null) holder.wait(10);
         assertNotNull(holder.get());
         holder.set(null);
      }

      // Remove the listener
      localServer.removeNotificationListener(proxyName, listener);

      // Unregister the MLet: this will trigger a notification
      remoteServer.unregisterMBean(remoteMLetName);

      assertNull(holder.get());
   }
View Full Code Here

   {
      private final MutableObject holder;

      public InteroperabilityListener()
      {
         this.holder = new MutableObject(null);
      }
View Full Code Here

TOP

Related Classes of test.MutableObject

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.