@Test
  public void testCreateObjectName() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();

    ObjectName name = stats.createObjectName();
    assertEquals("Wrong name.", RoundRobinSchedulerStats.OBJECT_NAME, name.getCanonicalName());
  }
  @Test
  public void testStop() throws Exception {
    RoundRobinSchedulerStats stats =
        EasyMock.createMockBuilder(RoundRobinSchedulerStats.class)
            .withConstructor()
            .addMockedMethod("unregisterMBean")
            .createStrictMock();

    MovingAverage mavg = EasyMock.createStrictMock(MovingAverage.class);
    stats.m_addMavg = mavg;
    stats.m_addMavg.stopTimer();
    EasyMock.expectLastCall();
    stats.unregisterMBean();
    EasyMock.expectLastCall();

    EasyMock.replay(stats, mavg);
    stats.stop();
    EasyMock.verify(stats, mavg);
    assertNull("Mavg should be null.", stats.m_addMavg);

    // with no avg
    EasyMock.reset(stats);
    EasyMock.replay(stats);
    stats.stop();
    EasyMock.verify(stats);
  }
  @Test
  public void testProcessed() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
    assertEquals("Wrong default value.", 0, stats.getProcessedItemCount());

    stats.incrementProcessed();
    assertEquals("Wrong incremented value.", 1, stats.getProcessedItemCount());
    stats.incrementProcessed();
    assertEquals("Wrong incremented value.", 2, stats.getProcessedItemCount());
    stats.incrementProcessed();
    assertEquals("Wrong incremented value.", 3, stats.getProcessedItemCount());
  }
  @Test
  public void testStart() throws Exception {
    RoundRobinSchedulerStats stats =
        EasyMock.createMockBuilder(RoundRobinSchedulerStats.class)
            .withConstructor()
            .addMockedMethod("registerMBean")
            .addMockedMethod("createMovingAverage")
            .createStrictMock();

    MovingAverage mavg = EasyMock.createStrictMock(MovingAverage.class);
    EasyMock.expect(stats.createMovingAverage()).andReturn(mavg);
    mavg.startTimer("RoundRobinAddMavg", stats.getAddMavgPeriod(), TimeUnit.MILLISECONDS);
    EasyMock.expectLastCall();

    stats.registerMBean();
    EasyMock.expectLastCall();

    EasyMock.replay(stats, mavg);
    stats.start();
    EasyMock.verify(stats, mavg);
    assertEquals("Mavg not set.", mavg, stats.m_addMavg);

    // with already called
    EasyMock.reset(stats, mavg);
    EasyMock.replay(stats, mavg);
    stats.start();
    EasyMock.verify(stats, mavg);
  }
  @Test
  public void testUnregisterMBean() throws Exception {
    RoundRobinSchedulerStats stats =
        EasyMock.createMockBuilder(RoundRobinSchedulerStats.class)
            .withConstructor()
            .addMockedMethod("getMBeanServer")
            .createStrictMock();

    // do nothing
    EasyMock.replay(stats);
    stats.unregisterMBean();
    EasyMock.verify(stats);

    // now work
    stats.m_mbeanName = stats.createObjectName();
    EasyMock.reset(stats);
    MBeanServer mbs = EasyMock.createStrictMock(MBeanServer.class);
    EasyMock.expect(stats.getMBeanServer()).andReturn(mbs);
    mbs.unregisterMBean(stats.m_mbeanName);
    EasyMock.expectLastCall();

    EasyMock.replay(stats, mbs);
    stats.unregisterMBean();
    EasyMock.verify(stats, mbs);
    assertNull("Object name should be null.", stats.m_mbeanName);

    // with exception that is just logged
    stats.m_mbeanName = stats.createObjectName();
    EasyMock.reset(stats, mbs);
    EasyMock.expect(stats.getMBeanServer()).andReturn(mbs);
    mbs.unregisterMBean(stats.m_mbeanName);
    EasyMock.expectLastCall().andThrow(new MBeanRegistrationException(new Exception()));

    EasyMock.replay(stats, mbs);
    stats.unregisterMBean();
    EasyMock.verify(stats, mbs);
    assertNull("Object name should be null.", stats.m_mbeanName);
  }
  @Test
  public void testConstructor() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
    assertFalse("JMX should not be enabled by default.", stats.isUseJMX());

    stats.setUseJMX(true);
    assertTrue("JMX should be enabled.", stats.isUseJMX());
    assertEquals("Wrong default mavg period.", 300000, stats.getAddMavgPeriod());
    assertNull("Mavg should not be created.", stats.m_addMavg);
    assertEquals("Wrong default mavg sample size.", 360, stats.getAddMavgSampleSize());
  }
  @Test
  public void testTimedOut() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
    assertEquals("Wrong default timed out value.", 0, stats.getTimedOutCount());

    stats.incrementTimedOut(3);
    assertEquals("Wrong timed out value.", 3, stats.getTimedOutCount());

    stats.incrementTimedOut(2);
    assertEquals("Wrong timed out value.", 5, stats.getTimedOutCount());
  }
  @Test
  public void testLeaseTime() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
    assertEquals("Wrong default lease time.", 0, stats.getTotalLeaseTime());
    assertEquals("Wrong default lease count.", 0, stats.getTotalLeaseCount());

    assertEquals("Wrong average lease time.", 0.0f, stats.getAverageLeaseTime());

    stats.addLeaseTime(3000);
    stats.addLeaseTime(1500);
    assertEquals("Wrong average lease time.", 4500 / 2.0f, stats.getAverageLeaseTime());
  }
  @Test
  public void testRegisterMBean() throws Exception {
    RoundRobinSchedulerStats stats =
        EasyMock.createMockBuilder(RoundRobinSchedulerStats.class)
            .withConstructor()
            .addMockedMethod("getMBeanServer")
            .addMockedMethod("createObjectName")
            .createStrictMock();

    // do nothing
    stats.setUseJMX(false);
    EasyMock.replay(stats);
    stats.registerMBean();
    EasyMock.verify(stats);
    assertNull("Object name should still be null.", stats.m_mbeanName);

    // now work
    EasyMock.reset(stats);
    stats.setUseJMX(true);
    MBeanServer mbs = EasyMock.createStrictMock(MBeanServer.class);
    ObjectName objectName = EasyMock.createStrictMock(ObjectName.class);
    EasyMock.expect(stats.getMBeanServer()).andReturn(mbs);
    EasyMock.expect(stats.createObjectName()).andReturn(objectName);
    EasyMock.expect(mbs.registerMBean(stats, objectName)).andReturn(null);

    EasyMock.replay(stats, mbs, objectName);
    stats.registerMBean();
    EasyMock.verify(stats, mbs, objectName);
    assertEquals("Object name should not be null.", objectName, stats.m_mbeanName);

    // with exception that is just logged
    EasyMock.reset(stats, mbs, objectName);
    stats.m_mbeanName = null;
    EasyMock.expect(stats.getMBeanServer()).andReturn(mbs);
    EasyMock.expect(stats.createObjectName()).andReturn(objectName);
    EasyMock.expect(mbs.registerMBean(stats, objectName))
        .andThrow(new NotCompliantMBeanException("blah"));

    EasyMock.replay(stats, mbs, objectName);
    stats.registerMBean();
    EasyMock.verify(stats, mbs, objectName);
    assertNull("Object name should still be null.", stats.m_mbeanName);
  }
Пример #10
0
 @Test
 public void testGetMBeanServer() throws Exception {
   RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
   assertNotNull("Should have returned server.", stats.getMBeanServer());
 }
Пример #11
0
  @Test
  public void testItemConsumed() throws Exception {
    RoundRobinSchedulerStats stats = new RoundRobinSchedulerStats();
    stats.incrementLeased();
    stats.incrementLeased();
    stats.incrementScheduled();
    stats.incrementScheduled();

    assertEquals("Wrong processed value.", 0, stats.getProcessedItemCount());
    assertEquals("Wrong leased value.", 2, stats.getLeasedItemCount());
    assertEquals("Wrong scheduled value.", 2, stats.getScheduledItemCount());

    stats.itemConsumed();

    assertEquals("Wrong processed value.", 1, stats.getProcessedItemCount());
    assertEquals("Wrong leased value.", 1, stats.getLeasedItemCount());
    assertEquals("Wrong scheduled value.", 1, stats.getScheduledItemCount());

    stats.itemConsumed();

    assertEquals("Wrong processed value.", 2, stats.getProcessedItemCount());
    assertEquals("Wrong leased value.", 0, stats.getLeasedItemCount());
    assertEquals("Wrong scheduled value.", 0, stats.getScheduledItemCount());
  }