Package test

Examples of test.MutableInteger


      MonitorTarget target = new MonitorTarget();
      int value = low.intValue() - 1;
      target.setInteger(value);
      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(), 0);
         assertNull(holder.get());

         // Set gauge above high threshold
         value = value + high.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);

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

         // Set gauge inside threshold
         value = value + low.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());

         // Set gauge above threshold again
         value = value + high.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
      }
      finally
      {
View Full Code Here


      MonitorTarget target = new MonitorTarget();
      int value = low.intValue() + 1;
      target.setInteger(value);
      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
      {
         // Inside the thresholds, be sure low notification
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);

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

         // Monitoring takes time, so I disable low notification to be sure to get only the high one
         // The monitor is in difference mode, so the first time will get the high notification, but
         // the second time will get zero, since the gauge did not change, which will triggers a low notification
         monitor.setNotifyLow(false);
         // Set gauge above high threshold
         value = value + high.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);

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

         monitor.setNotifyHigh(false);
         monitor.setNotifyLow(true);
         // Set gauge above high threshold, so just after goes below low threshold
         value = value + high.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);

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

         // Set gauge inside threshold
         value = value + low.intValue() + 1;
         target.setInteger(value);
         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());
      }
      finally
      {
         monitor.stop();
View Full Code Here

      monitor.addObservedObject(counterName);
      monitor.setGranularityPeriod(1000);
      monitor.setObservedAttribute("ObjectCounter");

      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
      {
         // Wait for notification to arrive
         while (holder.get() == null) sleep(10);

         // Be sure only one arrived
         sleep(5000);
         assertEquals(times.get(), 1);

         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
      }
      finally
View Full Code Here

      monitor.setNotify(true);
      // 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
      {
         monitor.stop();
      }
View Full Code Here

      monitor.setOffset(offset);
      // 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
      {
         monitor.stop();
      }
View Full Code Here

      assertTrue(m_timer.isEmpty());
   }

   public void testSendPastNotifications4() throws Exception
   {
      final MutableInteger count = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            count.set(count.get() + 1);
         }
      };
      m_server.addNotificationListener(m_timerName, listener, null, null);

      long now = System.currentTimeMillis();

      // This periodic notification started in the past, sendPastNotifications is true
      // so all notifications must be emitted
      long occurrences = 10;
      long skip = 4;
      Date date = new Date(now - Timer.ONE_SECOND * skip);
      m_timer.setSendPastNotifications(true);
      m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
      m_timer.start();

      // Wait for the notifications to happen
      sleep(Timer.ONE_SECOND * (occurrences + 1));

      assertEquals(count.get(), occurrences);
      assertTrue(m_timer.isEmpty());
   }
View Full Code Here

      assertTrue(m_timer.isEmpty());
   }

   public void testSendPastNotifications5() throws Exception
   {
      final MutableInteger count = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            count.set(count.get() + 1);
         }
      };
      m_server.addNotificationListener(m_timerName, listener, null, null);

      long now = System.currentTimeMillis();

      // This periodic notification is started, sendPastNotifications is false
      // the Timer is started, then stopped, then restarted
      long occurrences = 10;
      long pre = 2;
      long skip = 4;
      Date date = new Date(now + Timer.ONE_SECOND);
      m_timer.setSendPastNotifications(false);
      m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
      m_timer.start();

      // Wait for the notifications to happen
      sleep(Timer.ONE_SECOND * pre);
      m_timer.stop();

      // Sometimes we loose one notification because we're not that fast, it's ok.
      if (count.get() != pre && count.get() != pre - 1)
         fail("Expected notifications not emitted: expecting " + pre + " got " + count.get());
      assertEquals(m_timer.getNbNotifications(), 1);

      // Wait to skip some notification
      sleep(Timer.ONE_SECOND * skip);

      // Restart the Timer
      m_timer.start();

      // Wait for the remaining notifications to happen
      sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1));

      m_timer.stop();

      // Sometimes we loose one notification because we're not that fast, it's ok.
      long expected = occurrences - skip;
      if (count.get() != expected && count.get() != expected - 1)
         fail("Expected notifications not emitted.  Expected " + expected + " or " + (expected - 1) + ". got " + count.get());
      assertTrue(m_timer.isEmpty());
   }
View Full Code Here

      assertTrue(m_timer.isEmpty());
   }

   public void testSendPastNotifications6() throws Exception
   {
      final MutableInteger count = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            count.set(count.get() + 1);
         }
      };
      m_server.addNotificationListener(m_timerName, listener, null, null);

      long now = System.currentTimeMillis();

      // This periodic notification is started, sendPastNotifications is true
      // the Timer is started, then stopped, then restarted
      long occurrences = 10;
      long pre = 2;
      long skip = 4;
      Date date = new Date(now + Timer.ONE_SECOND);
      m_timer.setSendPastNotifications(true);
      m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences, true);
      m_timer.start();

      // Wait for the notifications to happen
      sleep(Timer.ONE_SECOND * pre);
      m_timer.stop();

      // Sometimes we loose one notification because we're not that fast, it's ok.
      if (count.get() != pre && count.get() != pre - 1)
         fail("Expected notifications not emitted: expecting " + pre + " got " + count.get());
      assertFalse(m_timer.isEmpty());

      // Wait to skip some notification
      sleep(Timer.ONE_SECOND * skip);

      // Restart the Timer
      m_timer.start();

      // Wait for the remaining notifications to happen
      sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1));

      m_timer.stop();
      assertEquals(count.get(), occurrences);
      assertTrue(m_timer.isEmpty());
   }
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);
            }
         }
      };

      m_server.addNotificationListener(m_timerName, fdListener, new NotificationFilter()
      {
         public boolean isNotificationEnabled(Notification notification)
         {
            return notification.getType().equals(fdNotifType);
         }
      }, null);

      m_server.addNotificationListener(m_timerName, frListener, new NotificationFilter()
      {
         public boolean isNotificationEnabled(Notification notification)
         {
            return notification.getType().equals(frNotifType);
         }
      }, null);

      // Testing fixed delay/fixed rate
      long now = System.currentTimeMillis();
      // Notify in one second
      Date date = new Date(now + Timer.ONE_SECOND);
      // Register to happen 10 times
      m_timer.addNotification(fdNotifType, "timer-message", "user-data", date, 10, occurrences, false);
      m_timer.addNotification(frNotifType, "timer-message", "user-data", date, 10, occurrences, true);

      // Sleep some time
      while (frOccurrences.get() < occurrences || fdOccurrences.get() < occurrences)
      {
         sleep(10);
         System.gc();
      }

      assertEquals(frOccurrences.get(), occurrences);
      assertEquals(fdOccurrences.get(), occurrences);

      if (((1.0f * frElapsedTime.get()) / fdElapsedTime.get()) > 0.95)
         fail("Fixed rate and fixed delay exhibit no execution rate differences");
   }
View Full Code Here

      System.setProperty(property, "mx4j.server.CachingReflectionMBeanInvoker");
      MBeanServer server = newMBeanServer();
      ObjectName name = new ObjectName("BCEL:test=performance,type=reflection");
      try
      {
         MutableInteger integer = new MutableInteger(0);
         BCELPerformance mbean = new BCELPerformance(integer);
         server.registerMBean(mbean, name);

         long[] results = new long[m_reps];
         for (int i = 0; i < m_reps; ++i)
         {
            long start = System.currentTimeMillis();
            for (int j = 0; j < m_calls; ++j)
            {
               server.getAttribute(name, "Test");
            }
            long end = System.currentTimeMillis();
            results[i] = end - start;
            System.out.println("Reflection result: " + results[i]);
         }

         if (integer.get() != m_calls * m_reps)
         {
            fail("MBean not called !");
         }

         long reflectionAverage = 0;
View Full Code Here

TOP

Related Classes of test.MutableInteger

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.