@Test
public void testApplicationQueuePercent()
    throws Exception {

  Resource res = Resource.newInstance(10 * 1024, 10);
  CapacityScheduler scheduler = mock(CapacityScheduler.class);
  when(scheduler.getClusterResource()).thenReturn(res);
  when(scheduler.getResourceCalculator())
      .thenReturn(new DefaultResourceCalculator());
  ApplicationAttemptId appAttId = createAppAttemptId(0, 0);
  RMContext rmContext = mock(RMContext.class);
  when(rmContext.getEpoch()).thenReturn(3L);
  when(rmContext.getScheduler()).thenReturn(scheduler);
  when(rmContext.getRMApps())
    .thenReturn(new ConcurrentHashMap<ApplicationId, RMApp>());
  RMNodeLabelsManager nlm = mock(RMNodeLabelsManager.class);
  when(nlm.getResourceByLabel(any(), any())).thenReturn(res);
  when(rmContext.getNodeLabelManager()).thenReturn(nlm);
  when(rmContext.getYarnConfiguration()).thenReturn(csConf);
  // Queue "test" consumes 100% of the cluster, so its capacity and absolute
  // capacity are both 1.0f.
  Queue queue = createQueue("test", null, 1.0f, 1.0f, res);
  final String user = "user1";
  FiCaSchedulerApp app =
      new FiCaSchedulerApp(appAttId, user, queue,
          queue.getAbstractUsersManager(), rmContext);
  // Resource request
  Resource requestedResource = Resource.newInstance(1536, 2);
  app.getAppAttemptResourceUsage().incUsed(requestedResource);
  // In "test" queue, 1536 used is 15% of both the queue and the cluster
  assertEquals(15.0f, app.getResourceUsageReport().getQueueUsagePercentage(),
      0.01f);
  assertEquals(15.0f,
      app.getResourceUsageReport().getClusterUsagePercentage(), 0.01f);
  // Queue "test2" is a child of root and its capacity is 50% of root. As a
  // child of root, its absolute capaicty is also 50%.
  queue = createQueue("test2", null, 0.5f, 0.5f,
      Resources.divideAndCeil(dominantResourceCalculator, res, 2));
  app = new FiCaSchedulerApp(appAttId, user, queue,
      queue.getAbstractUsersManager(), rmContext);
  app.getAppAttemptResourceUsage().incUsed(requestedResource);
  // In "test2" queue, 1536 used is 30% of "test2" and 15% of the cluster.
  assertEquals(30.0f, app.getResourceUsageReport().getQueueUsagePercentage(),
      0.01f);
  assertEquals(15.0f,
      app.getResourceUsageReport().getClusterUsagePercentage(), 0.01f);
  // Queue "test2.1" is 50% of queue "test2", which is 50% of the cluster.
  // Therefore, "test2.1" capacity is 50% and absolute capacity is 25%.
  AbstractCSQueue qChild = createQueue("test2.1", queue, 0.5f, 0.25f,
      Resources.divideAndCeil(dominantResourceCalculator, res, 4));
  app = new FiCaSchedulerApp(appAttId, user, qChild,
      qChild.getAbstractUsersManager(), rmContext);
  app.getAppAttemptResourceUsage().incUsed(requestedResource);
  // In "test2.1" queue, 1536 used is 60% of "test2.1" and 15% of the cluster.
  assertEquals(60.0f, app.getResourceUsageReport().getQueueUsagePercentage(),
      0.01f);
  assertEquals(15.0f,
      app.getResourceUsageReport().getClusterUsagePercentage(), 0.01f);
  // test that queueUsagePercentage returns neither NaN nor Infinite
  AbstractCSQueue zeroQueue = createQueue("test2.2", null,
      Float.MIN_VALUE, Float.MIN_VALUE,
      Resources.multiply(res, Float.MIN_VALUE));
  app = new FiCaSchedulerApp(appAttId, user, zeroQueue,
      qChild.getAbstractUsersManager(), rmContext);
  app.getAppAttemptResourceUsage().incUsed(requestedResource);
  assertEquals(0.0f, app.getResourceUsageReport().getQueueUsagePercentage(),
      0.01f);
}