Ejemplo n.º 1
0
  @Test
  public void retryAfterConnectionIsDisconnected() throws Exception {

    RedisAsyncConnection<String, String> connection = client.connectAsync();
    RedisChannelHandler<String, String> redisChannelHandler =
        (RedisChannelHandler) connection.getStatefulConnection();
    RedisCommands<String, String> verificationConnection = client.connect().sync();

    connection.set(key, "1").get();

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

    connection.quit();
    while (connection.isOpen()) {
      Thread.sleep(100);
    }

    assertThat(connection.incr(key).await(1, TimeUnit.SECONDS)).isFalse();

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

    assertThat(getQueue(redisChannelHandler)).isEmpty();
    assertThat(getCommandBuffer(redisChannelHandler).size()).isGreaterThan(0);

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

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

    assertThat(connection.get(key).get()).isEqualTo("2");
    assertThat(verificationConnection.get(key)).isEqualTo("2");

    connection.close();
    verificationConnection.close();
  }
Ejemplo n.º 2
0
  @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();
  }