@Test
public void testClusterChosenWithRightProbability() throws YarnException {

  ApplicationSubmissionContext context =
      mock(ApplicationSubmissionContext.class);
  when(context.getQueue()).thenReturn("queue1");
  setApplicationSubmissionContext(context);
  Map<SubClusterId, AtomicLong> counter = new HashMap<>();
  for (SubClusterIdInfo id : getPolicyInfo().getRouterPolicyWeights()
      .keySet()) {
    counter.put(id.toId(), new AtomicLong(0));
  }
  float numberOfDraws = 10000;
  for (float i = 0; i < numberOfDraws; i++) {
    SubClusterId chosenId = ((FederationRouterPolicy) getPolicy())
        .getHomeSubcluster(getApplicationSubmissionContext(), null);
    counter.get(chosenId).incrementAndGet();
  }
  float totalActiveWeight = 0;
  for (SubClusterId id : getActiveSubclusters().keySet()) {
    SubClusterIdInfo idInfo = new SubClusterIdInfo(id);
    if (getPolicyInfo().getRouterPolicyWeights().containsKey(idInfo)) {
      totalActiveWeight +=
          getPolicyInfo().getRouterPolicyWeights().get(idInfo);
    }
  }
  for (Map.Entry<SubClusterId, AtomicLong> counterEntry : counter
      .entrySet()) {
    float expectedWeight = getPolicyInfo().getRouterPolicyWeights()
        .get(new SubClusterIdInfo(counterEntry.getKey())) / totalActiveWeight;
    float actualWeight = counterEntry.getValue().floatValue() / numberOfDraws;
    // make sure that the weights is respected among active subclusters
    // and no jobs are routed to inactive subclusters.
    if (getActiveSubclusters().containsKey(counterEntry.getKey())) {
      Assert.assertTrue(
          "Id " + counterEntry.getKey() + " Actual weight: " + actualWeight
              + " expected weight: " + expectedWeight,
          Math.abs(actualWeight - expectedWeight) < 0.01);
    } else {
      Assert
          .assertTrue(
              "Id " + counterEntry.getKey() + " Actual weight: "
                  + actualWeight + " expected weight: " + expectedWeight,
              actualWeight == 0);
    }
  }
}