Example #1
0
  @Test
  public void noCommandsExecutedAfterConnectionIsDisconnected() throws Exception {

    RedisCommands<String, String> connection = client.connect().sync();
    connection.quit();

    Wait.untilTrue(() -> !connection.isOpen()).waitOrTimeout();

    try {
      connection.incr(key);
    } catch (RedisException e) {
      assertThat(e).isInstanceOf(RedisException.class);
    }

    connection.close();

    RedisCommands<String, String> connection2 = client.connect().sync();
    connection2.quit();

    try {

      Wait.untilTrue(() -> !connection.isOpen()).waitOrTimeout();

      connection2.incr(key);
    } catch (Exception e) {
      assertThat(e).isExactlyInstanceOf(RedisException.class).hasMessageContaining("not connected");
    }

    connection2.close();
  }
  @Test
  public void commandCancelledOverSyncAPIAfterConnectionIsDisconnected() throws Exception {

    RedisCommands<String, String> connection = client.connect().sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();

    connection.set(key, "1");

    ConnectionWatchdog connectionWatchdog =
        Connections.getConnectionWatchdog(connection.getStatefulConnection());
    connectionWatchdog.setListenOnChannelInactive(false);

    connection.quit();
    Wait.untilTrue(() -> !connection.isOpen()).waitOrTimeout();

    try {
      connection.incr(key);
    } catch (RedisException e) {
      assertThat(e).isExactlyInstanceOf(RedisCommandTimeoutException.class);
    }

    assertThat(verificationConnection.get("key")).isEqualTo("1");

    assertThat(getQueue(getRedisChannelHandler(connection))).isEmpty();
    assertThat(getCommandBuffer(getRedisChannelHandler(connection)).size()).isGreaterThan(0);

    connectionWatchdog.setListenOnChannelInactive(true);
    connectionWatchdog.scheduleReconnect();

    while (!getCommandBuffer(getRedisChannelHandler(connection)).isEmpty()
        || !getQueue(getRedisChannelHandler(connection)).isEmpty()) {
      Thread.sleep(10);
    }

    assertThat(connection.get(key)).isEqualTo("1");

    connection.close();
    verificationConnection.close();
  }
  @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);
  }