@Test(timeout=5000)
public void testGetQueueResource() throws Exception {

  Resource clusterResource = Resource.newInstance(9999, 1);
  
  /*
   * Node->Labels:
   *   host1 : red
   *   host2 : blue
   *   host3 : yellow
   *   host4 :
   */
  mgr.addToCluserNodeLabelsWithDefaultExclusivity(toSet("red", "blue", "yellow"));
  mgr.replaceLabelsOnNode(ImmutableMap.of(toNodeId("host1"), toSet("red")));
  mgr.replaceLabelsOnNode(ImmutableMap.of(toNodeId("host2"), toSet("blue")));
  mgr.replaceLabelsOnNode(ImmutableMap.of(toNodeId("host3"), toSet("yellow")));
  
  // active two NM to n1, one large and one small
  mgr.activateNode(NodeId.newInstance("host1", 1), SMALL_RESOURCE);
  mgr.activateNode(NodeId.newInstance("host2", 1), SMALL_RESOURCE);
  mgr.activateNode(NodeId.newInstance("host3", 1), SMALL_RESOURCE);
  mgr.activateNode(NodeId.newInstance("host4", 1), SMALL_RESOURCE);
  
  // reinitialize queue
  Set<String> q1Label = toSet("red", "blue");
  Set<String> q2Label = toSet("blue", "yellow");
  Set<String> q3Label = toSet("yellow");
  Set<String> q4Label = RMNodeLabelsManager.EMPTY_STRING_SET;
  Set<String> q5Label = toSet(RMNodeLabelsManager.ANY);
  
  Map<String, Set<String>> queueToLabels = new HashMap<String, Set<String>>();
  queueToLabels.put("Q1", q1Label);
  queueToLabels.put("Q2", q2Label);
  queueToLabels.put("Q3", q3Label);
  queueToLabels.put("Q4", q4Label);
  queueToLabels.put("Q5", q5Label);
  mgr.reinitializeQueueLabels(queueToLabels);
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 1),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
  
  mgr.removeLabelsFromNode(ImmutableMap.of(toNodeId("host2"), toSet("blue")));
  /*
   * Check resource after changes some labels
   * Node->Labels:
   *   host1 : red
   *   host2 : (was: blue)
   *   host3 : yellow
   *   host4 :
   */
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
  
  /*
   * Check resource after deactive/active some nodes 
   * Node->Labels:
   *   (deactived) host1 : red
   *   host2 :
   *   (deactived and then actived) host3 : yellow
   *   host4 :
   */
  mgr.deactivateNode(NodeId.newInstance("host1", 1));
  mgr.deactivateNode(NodeId.newInstance("host3", 1));
  mgr.activateNode(NodeId.newInstance("host3", 1), SMALL_RESOURCE);
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
  
  /*
   * Check resource after refresh queue:
   *    Q1: blue
   *    Q2: red, blue
   *    Q3: red
   *    Q4:
   *    Q5: ANY
   */
  q1Label = toSet("blue");
  q2Label = toSet("blue", "red");
  q3Label = toSet("red");
  q4Label = RMNodeLabelsManager.EMPTY_STRING_SET;
  q5Label = toSet(RMNodeLabelsManager.ANY);
  
  queueToLabels.clear();
  queueToLabels.put("Q1", q1Label);
  queueToLabels.put("Q2", q2Label);
  queueToLabels.put("Q3", q3Label);
  queueToLabels.put("Q4", q4Label);
  queueToLabels.put("Q5", q5Label);
  mgr.reinitializeQueueLabels(queueToLabels);
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 2),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
  
  /*
   * Active NMs in nodes already have NM
   * Node->Labels:
   *   host2 :
   *   host3 : yellow (3 NMs)
   *   host4 : (2 NMs)
   */
  mgr.activateNode(NodeId.newInstance("host3", 2), SMALL_RESOURCE);
  mgr.activateNode(NodeId.newInstance("host3", 3), SMALL_RESOURCE);
  mgr.activateNode(NodeId.newInstance("host4", 2), SMALL_RESOURCE);
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 3),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
  
  /*
   * Deactive NMs in nodes already have NMs
   * Node->Labels:
   *   host2 :
   *   host3 : yellow (2 NMs)
   *   host4 : (0 NMs)
   */
  mgr.deactivateNode(NodeId.newInstance("host3", 3));
  mgr.deactivateNode(NodeId.newInstance("host4", 2));
  mgr.deactivateNode(NodeId.newInstance("host4", 1));
  
  // check resource
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 1),
      mgr.getQueueResource("Q1", q1Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 1),
      mgr.getQueueResource("Q2", q2Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 1),
      mgr.getQueueResource("Q3", q3Label, clusterResource));
  Assert.assertEquals(Resources.multiply(SMALL_RESOURCE, 1),
      mgr.getQueueResource("Q4", q4Label, clusterResource));
  Assert.assertEquals(clusterResource,
      mgr.getQueueResource("Q5", q5Label, clusterResource));
}