@Test
  public void testIntersectingGroups() {
    final LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    final ServerLocation l1 = new ServerLocation("localhost", 1);
    final ServerLocation l2 = new ServerLocation("localhost", 2);
    final ServerLocation l3 = new ServerLocation("localhost", 3);
    final String uniqueId1 = new InternalDistributedMember("localhost", 1).getUniqueId();
    final String uniqueId2 = new InternalDistributedMember("localhost", 2).getUniqueId();
    final String uniqueId3 = new InternalDistributedMember("localhost", 3).getUniqueId();
    sn.addServer(l1, uniqueId1, new String[] {"a",}, new ServerLoad(0, 1, 0, 1),
        LOAD_POLL_INTERVAL);
    sn.addServer(l2, uniqueId2, new String[] {"a", "b"}, new ServerLoad(0, 1, 0, 1),
        LOAD_POLL_INTERVAL);
    sn.addServer(l3, uniqueId3, new String[] {"b"}, new ServerLoad(0, 1, 0, 1), LOAD_POLL_INTERVAL);

    // Test with interleaving requests for either group
    for (int i = 0; i < 60; i++) {
      ServerLocation l = sn.getServerForConnection("a", Collections.emptySet());
      assertThat(l1.equals(l) || l2.equals(l)).isTrue();
      l = sn.getServerForConnection("b", Collections.emptySet());
      assertThat(l2.equals(l) || l3.equals(l)).isTrue();
    }

    Map<ServerLocation, ServerLoad> expected = new HashMap<>();
    ServerLoad expectedLoad = new ServerLoad(40f, 1f, 0f, 1f);
    expected.put(l1, expectedLoad);
    expected.put(l2, expectedLoad);
    expected.put(l3, expectedLoad);
    assertThat(sn.getLoadMap()).isEqualTo(expected);

    sn.updateLoad(l1, uniqueId1, new ServerLoad(0, 1, 0, 1));
    sn.updateLoad(l2, uniqueId2, new ServerLoad(0, 1, 0, 1));
    sn.updateLoad(l3, uniqueId3, new ServerLoad(0, 1, 0, 1));


    // Now do the same test, but make all the requests for one group first,
    // then the second group.
    for (int i = 0; i < 60; i++) {
      ServerLocation l = sn.getServerForConnection("a", Collections.emptySet());
      assertThat(l1.equals(l) || l2.equals(l)).isTrue();
    }

    expected = new HashMap<>();
    expected.put(l1, new ServerLoad(30f, 1f, 0f, 1f));
    expected.put(l2, new ServerLoad(30f, 1f, 0f, 1f));
    expected.put(l3, new ServerLoad(0f, 1f, 0f, 1f));
    assertThat(sn.getLoadMap()).isEqualTo(expected);

    for (int i = 0; i < 60; i++) {
      ServerLocation l = sn.getServerForConnection("b", Collections.emptySet());
      assertThat(l2.equals(l) || l3.equals(l)).isTrue();
    }

    // The load can't be completely balanced, because
    // We already had 30 connections from group a on server l2.
    // But we expect that l3 should have received most of the connections
    // for group b, because it started out with 0.
    expected = new HashMap<>();
    expected.put(l1, new ServerLoad(30f, 1f, 0f, 1f));
    expected.put(l2, new ServerLoad(45f, 1f, 0f, 1f));
    expected.put(l3, new ServerLoad(45f, 1f, 0f, 1f));
    assertThat(sn.getLoadMap()).isEqualTo(expected);

  }
Test to make sure that we balancing three servers with interecting groups correctly.
geode-core/src/test/java/org/apache/geode/distributed/internal/LocatorLoadSnapshotTest.java