@Test
  public void testJoinMessageReceivedForDisconnectedHost() throws Exception {
    final AtomicInteger counter1 = new AtomicInteger(0);
    final AtomicInteger counter2 = new AtomicInteger(0);

    connector1.subscribe(String.class.getName(), new CountingCommandHandler(counter1));
    connector1.connect(20);
    assertTrue(
        "Expected connector 1 to connect within 10 seconds",
        connector1.awaitJoined(10, TimeUnit.SECONDS));

    connector2.subscribe(String.class.getName(), new CountingCommandHandler(counter2));
    connector2.connect(80);
    assertTrue("Connector 2 failed to connect", connector2.awaitJoined());

    // wait for both connectors to have the same view
    waitForConnectorSync();

    ConsistentHash hashBefore = connector1.getConsistentHash();
    // secretly insert an illegal message
    channel1
        .getReceiver()
        .receive(
            new Message(
                channel1.getAddress(),
                new IpAddress(12345),
                new JoinMessage(10, Collections.<String>emptySet())));
    ConsistentHash hash2After = connector1.getConsistentHash();
    assertEquals("That message should not have changed the ring", hashBefore, hash2After);
  }
 private void waitForConnectorSync() throws InterruptedException {
   int t = 0;
   while (ConsistentHash.emptyRing().equals(connector1.getConsistentHash())
       || !connector1.getConsistentHash().equals(connector2.getConsistentHash())) {
     // don't have a member for String yet, which means we must wait a little longer
     if (t++ > 1500) {
       assertEquals(
           "Connectors did not synchronize within 30 seconds.",
           connector1.getConsistentHash().toString(),
           connector2.getConsistentHash().toString());
     }
     Thread.sleep(20);
   }
 }
  @Test(timeout = 300000)
  public void testHashChangeNotification() throws Exception {
    connector1.connect(10);
    connector2.connect(10);

    // wait for both connectors to have the same view
    waitForConnectorSync();

    // connector 1 joined
    ConsistentHash notify1 = hashChangeListener.notifications.poll(5, TimeUnit.SECONDS);

    // connector 2 joined
    ConsistentHash notify2 = hashChangeListener.notifications.poll(5, TimeUnit.SECONDS);
    // Self and other node have joined
    assertEquals(connector1.getConsistentHash(), notify2);

    channel2.close();

    // Other node has left
    ConsistentHash notify3 = hashChangeListener.notifications.poll(5, TimeUnit.SECONDS);
    assertEquals(connector1.getConsistentHash(), notify3);
  }
  @Test(timeout = 30000)
  public void testRingsProperlySynchronized_ChannelAlreadyConnected() throws Exception {
    final AtomicInteger counter1 = new AtomicInteger(0);
    final AtomicInteger counter2 = new AtomicInteger(0);

    connector1.subscribe(String.class.getName(), new CountingCommandHandler(counter1));
    channel1.connect(clusterName);
    connector1.connect(20);
    assertTrue(
        "Expected connector 1 to connect within 10 seconds",
        connector1.awaitJoined(10, TimeUnit.SECONDS));

    connector2.subscribe(Long.class.getName(), new CountingCommandHandler(counter2));
    channel2.connect(clusterName);
    connector2.connect(80);

    assertTrue("Connector 2 failed to connect", connector2.awaitJoined());

    waitForConnectorSync();

    FutureCallback<Object, Object> callback1 = new FutureCallback<>();
    connector1.send("1", new GenericCommandMessage<>("Hello"), callback1);
    FutureCallback<Object, Object> callback2 = new FutureCallback<>();
    connector1.send("1", new GenericCommandMessage<>(1L), callback2);

    FutureCallback<Object, Object> callback3 = new FutureCallback<>();
    connector2.send("1", new GenericCommandMessage<>("Hello"), callback3);
    FutureCallback<Object, Object> callback4 = new FutureCallback<>();
    connector2.send("1", new GenericCommandMessage<>(1L), callback4);

    assertEquals("The Reply!", callback1.get());
    assertEquals("The Reply!", callback2.get());
    assertEquals("The Reply!", callback3.get());
    assertEquals("The Reply!", callback4.get());

    assertTrue(connector1.getConsistentHash().equals(connector2.getConsistentHash()));
  }