@Test
  public void sentinelConnect() throws Exception {

    RedisClient client =
        new RedisClient(RedisURI.Builder.redis(TestSettings.host(), TestSettings.port()).build());

    RedisSentinelAsyncCommands<String, String> connection = client.connectSentinelAsync();
    assertThat(connection.ping().get()).isEqualTo("PONG");

    connection.close();
    FastShutdown.shutdown(client);
  }
  @Test
  public void sentinelConnectWrongMaster() throws Exception {

    RedisClient client =
        new RedisClient(
            RedisURI.Builder.sentinel(TestSettings.host(), 1234, "nonexistent")
                .withSentinel(TestSettings.host())
                .build());
    try {
      client.connect();
      fail("missing RedisConnectionException");
    } catch (RedisConnectionException e) {
    }

    FastShutdown.shutdown(client);
  }
  @Test
  public void testMaster() throws Exception {

    String info =
        "# Replication\r\n"
            + "role:master\r\n"
            + "connected_slaves:1\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    List<RedisNodeDescription> result = sut.getNodesFromInfo(info);
    assertThat(result).hasSize(1);

    RedisNodeDescription redisNodeDescription = result.get(0);

    assertThat(redisNodeDescription.getRole()).isEqualTo(RedisInstance.Role.MASTER);
    assertThat(redisNodeDescription.getUri().getHost()).isEqualTo(TestSettings.host());
    assertThat(redisNodeDescription.getUri().getPort()).isEqualTo(TestSettings.port());
  }
  @Before
  public void openConnection() throws Exception {
    super.openConnection();

    try {
      sentinel.master(MASTER_ID);
    } catch (Exception e) {
      sentinelRule.monitor(MASTER_ID, hostAddr(), TestSettings.port(3), 1, true);
    }
  }
  @Test
  public void sentinelConnectWith() throws Exception {

    RedisClient client =
        new RedisClient(
            RedisURI.Builder.sentinel(TestSettings.host(), 1234, MASTER_ID)
                .withSentinel(TestSettings.host())
                .build());

    RedisSentinelAsyncCommands<String, String> sentinelConnection = client.connectSentinelAsync();
    assertThat(sentinelConnection.ping().get()).isEqualTo("PONG");

    sentinelConnection.close();

    RedisConnection<String, String> connection2 = client.connect().sync();
    assertThat(connection2.ping()).isEqualTo("PONG");
    connection2.quit();

    Wait.untilTrue(connection2::isOpen).waitOrTimeout();

    assertThat(connection2.ping()).isEqualTo("PONG");
    connection2.close();
    FastShutdown.shutdown(client);
  }
/** @author <a href="mailto:[email protected]">Mark Paluch</a> */
public class MasterSlaveTopologyProviderTest {

  private MasterSlaveTopologyProvider sut =
      new MasterSlaveTopologyProvider(
          null, RedisURI.Builder.redis(TestSettings.host(), TestSettings.port()).build());

  @Test
  public void testMaster() throws Exception {

    String info =
        "# Replication\r\n"
            + "role:master\r\n"
            + "connected_slaves:1\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    List<RedisNodeDescription> result = sut.getNodesFromInfo(info);
    assertThat(result).hasSize(1);

    RedisNodeDescription redisNodeDescription = result.get(0);

    assertThat(redisNodeDescription.getRole()).isEqualTo(RedisInstance.Role.MASTER);
    assertThat(redisNodeDescription.getUri().getHost()).isEqualTo(TestSettings.host());
    assertThat(redisNodeDescription.getUri().getPort()).isEqualTo(TestSettings.port());
  }

  @Test
  public void testMasterIsASlave() throws Exception {

    String info =
        "# Replication\r\n"
            + "role:slave\r\n"
            + "connected_slaves:1\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    List<RedisNodeDescription> result = sut.getNodesFromInfo(info);
    assertThat(result).hasSize(1);

    RedisNodeDescription redisNodeDescription = result.get(0);

    assertThat(redisNodeDescription.getRole()).isEqualTo(RedisInstance.Role.SLAVE);
  }

  @Test(expected = IllegalStateException.class)
  public void noRole() throws Exception {

    String info =
        "# Replication\r\n"
            + "connected_slaves:1\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    sut.getNodesFromInfo(info);
  }

  @Test(expected = IllegalStateException.class)
  public void noInvalidRole() throws Exception {

    String info =
        "# Replication\r\n"
            + "role:abc\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    sut.getNodesFromInfo(info);
  }

  @Test
  public void testSlaves() throws Exception {

    String info =
        "# Replication\r\n"
            + "role:master\r\n"
            + "slave0:ip=127.0.0.1,port=6483,state=online,offset=56276,lag=0\r\n"
            + "slave1:ip=127.0.0.1,port=6484,state=online,offset=56276,lag=0\r\n"
            + "master_repl_offset:56276\r\n"
            + "repl_backlog_active:1\r\n";

    List<RedisNodeDescription> result = sut.getNodesFromInfo(info);
    assertThat(result).hasSize(3);

    RedisNodeDescription slave1 = result.get(1);

    assertThat(slave1.getRole()).isEqualTo(RedisInstance.Role.SLAVE);
    assertThat(slave1.getUri().getHost()).isEqualTo("127.0.0.1");
    assertThat(slave1.getUri().getPort()).isEqualTo(6483);

    RedisNodeDescription slave2 = result.get(2);

    assertThat(slave2.getRole()).isEqualTo(RedisInstance.Role.SLAVE);
    assertThat(slave2.getUri().getHost()).isEqualTo("127.0.0.1");
    assertThat(slave2.getUri().getPort()).isEqualTo(6484);
  }
}
 @Test
 public void getMasterAddr() throws Exception {
   SocketAddress result = sentinel.getMasterAddrByName(MASTER_ID);
   InetSocketAddress socketAddress = (InetSocketAddress) result;
   assertThat(socketAddress.getHostName()).contains(TestSettings.host());
 }
 @BeforeClass
 public static void setupClient() {
   sentinelClient =
       new RedisClient(RedisURI.Builder.sentinel(TestSettings.host(), MASTER_ID).build());
 }