@Test
  @WithRunningServer({@RunningServer(name = CONTAINER1), @RunningServer(name = CONTAINER2)})
  public void testRebalanceSwitch() throws Exception {

    // Verifying that the rebalance is enabled by default.
    checkRebalanceStatus(true, provider(0), provider(1));

    checkRpcManagerStatistics(
        new String[] {"null"}, OWNERS_2_MEMBERS_NODE0_NODE1, provider(0), provider(1));

    // Disabling rebalance
    setAttribute(provider(0), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, false);
    checkRebalanceStatus(false, provider(0), provider(1));

    putDataIntoCache(NUMBER_ENTRIES);

    checkRpcManagerStatistics(
        new String[] {"null"}, OWNERS_2_MEMBERS_NODE0_NODE1, provider(0), provider(1));

    // Activating the rebalance, and checking that the state transfer happens.
    setAttribute(provider(1), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, true);
    checkRebalanceStatus(true, provider(0), provider(1));

    checkRpcManagerStatistics(
        new String[] {"null"}, OWNERS_2_MEMBERS_NODE0_NODE1, provider(0), provider(1));
  }
  @Test
  @WithRunningServer({@RunningServer(name = CONTAINER1), @RunningServer(name = CONTAINER2)})
  public void testRebalanceWithFirstNodeStop() throws Exception {

    try {
      verifyRebalanceWith3rdNode();

      // Disabling rebalance.
      setAttribute(provider(0), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, false);
      controller.stop(CONTAINER1);
      checkRpcManagerStatistics(
          new String[] {"null", OWNERS_2_MEMBERS_NODE1_NODE2},
          OWNERS_2_MEMBERS_NODE1_NODE2,
          provider(1),
          provider(2));
      checkRebalanceStatus(false, provider(1), provider(2));

      assertTrue(
          server(1)
                  .getCacheManager(getCacheManagerName())
                  .getCache(getCacheName())
                  .getNumberOfEntries()
              < NUMBER_ENTRIES);
      assertTrue(
          server(2)
                  .getCacheManager(getCacheManagerName())
                  .getCache(getCacheName())
                  .getNumberOfEntries()
              < NUMBER_ENTRIES);

      // Enabling rebalance
      setAttribute(provider(1), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, true);
      checkRebalanceStatus(true, provider(1), provider(2));

      checkRpcManagerStatistics(
          new String[] {"null", OWNERS_2_MEMBERS_NODE1_NODE2},
          OWNERS_2_MEMBERS_NODE1_NODE2,
          provider(1),
          provider(2));

      assertTrue(
          server(1)
                  .getCacheManager(getCacheManagerName())
                  .getCache(getCacheName())
                  .getNumberOfEntries()
              == NUMBER_ENTRIES);
      assertTrue(
          server(2)
                  .getCacheManager(getCacheManagerName())
                  .getCache(getCacheName())
                  .getNumberOfEntries()
              == NUMBER_ENTRIES);
    } finally {
      controller.stop(CONTAINER3);
    }
  }
  private void verifyRebalanceWith3rdNode() throws Exception {
    // Disabling Rebalance for verifying the join of the 3rd node.
    setAttribute(provider(0), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, false);

    // putting data into cache before adding new node
    putDataIntoCache(NUMBER_ENTRIES);

    // Verifying that the rebalance is disabled.
    checkRebalanceStatus(false, provider(0), provider(1));
    checkRpcManagerStatistics(
        new String[] {"null"}, OWNERS_2_MEMBERS_NODE0_NODE1, provider(0), provider(1));

    controller.start(CONTAINER3);
    createNewProvider(2);

    checkRebalanceStatus(false, provider(2));

    checkRpcManagerStatistics(
        new String[] {"null"}, OWNERS_2_MEMBERS_NODE0_NODE1, provider(0), provider(1), provider(2));

    assertTrue(
        "The cache on server(2) should be empty.",
        server(2)
                .getCacheManager(getCacheManagerName())
                .getCache(getCacheName())
                .getNumberOfEntries()
            == 0);

    // Enabling the Rebalance and verifying that the consistent rehash takes place.
    setAttribute(provider(0), LOCAL_TOPOLOGY_MANAGER, REBALANCE_ENABLED_ATTR_NAME, true);
    checkRebalanceStatus(true, provider(0), provider(1), provider(2));

    checkRpcManagerStatistics(
        new String[] {"null", OWNERS_2_MEMBERS_NODE0_NODE1_NODE2},
        null,
        provider(0),
        provider(1),
        provider(2));

    // Waiting for rehash take place.
    checkRpcManagerStatistics(
        new String[] {"null"},
        OWNERS_2_MEMBERS_NODE0_NODE1_NODE2,
        provider(0),
        provider(1),
        provider(2));

    assertTrue(
        server(0)
                .getCacheManager(getCacheManagerName())
                .getCache(getCacheName())
                .getNumberOfEntries()
            < NUMBER_ENTRIES);
    assertTrue(
        server(1)
                .getCacheManager(getCacheManagerName())
                .getCache(getCacheName())
                .getNumberOfEntries()
            < NUMBER_ENTRIES);
    assertTrue(
        server(2)
                .getCacheManager(getCacheManagerName())
                .getCache(getCacheName())
                .getNumberOfEntries()
            < NUMBER_ENTRIES);
  }