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();
  }
Example #2
0
  @Test
  public void connectionIsConnectedAfterConnect() throws Exception {

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

    assertThat(getConnectionState(getRedisChannelHandler(connection)));

    connection.close();
  }
Example #3
0
  @Test
  public void basicOperations() throws Exception {

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

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

    connection.close();
  }
Example #4
0
  @Test
  public void noReconnectHandler() throws Exception {

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

    assertThat(getHandler(RedisChannelWriter.class, getRedisChannelHandler(connection)))
        .isNotNull();
    assertThat(getHandler(ConnectionWatchdog.class, getRedisChannelHandler(connection))).isNull();

    connection.close();
  }
Example #5
0
  @Test
  public void noBufferedCommandsAfterExecute() throws Exception {

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

    connection.set(key, "1");

    assertThat(getQueue(getRedisChannelHandler(connection))).isEmpty();
    assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty();

    connection.close();
  }
Example #6
0
  @Before
  public void before() throws Exception {
    client.setOptions(new ClientOptions.Builder().autoReconnect(false).build());

    // needs to be increased on slow systems...perhaps...
    client.setDefaultTimeout(3, TimeUnit.SECONDS);

    RedisCommands<String, String> connection = client.connect().sync();
    connection.flushall();
    connection.flushdb();
    connection.close();
  }
  @Test
  public void reconnectIsActiveHandler() throws Exception {

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

    ConnectionWatchdog connectionWatchdog =
        Connections.getConnectionWatchdog(connection.getStatefulConnection());
    assertThat(connectionWatchdog).isNotNull();
    assertThat(connectionWatchdog.isListenOnChannelInactive()).isTrue();
    assertThat(connectionWatchdog.isReconnectSuspended()).isFalse();

    connection.close();
  }
Example #8
0
  @Test
  public void commandNotExecutedFailsOnEncode() throws Exception {

    RedisCommands<String, String> connection = client.connect().sync();
    RedisChannelWriter<String, String> channelWriter =
        getRedisChannelHandler(connection).getChannelWriter();

    connection.set(key, "1");
    AsyncCommand<String, String, String> working =
        new AsyncCommand<>(
            new Command<String, String, String>(
                CommandType.INCR,
                new IntegerOutput(CODEC),
                new CommandArgs<String, String>(CODEC).addKey(key)));
    channelWriter.write(working);
    assertThat(working.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(connection.get(key)).isEqualTo("2");

    AsyncCommand<String, String, Object> command =
        new AsyncCommand<String, String, Object>(
            new Command<String, String, Object>(
                CommandType.INCR,
                new IntegerOutput(CODEC),
                new CommandArgs<String, String>(CODEC).addKey(key))) {

          @Override
          public void encode(ByteBuf buf) {
            throw new IllegalStateException("I want to break free");
          }
        };

    channelWriter.write(command);

    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();
    assertThat(getException(command)).isInstanceOf(EncoderException.class);

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

    assertThat(getQueue(getRedisChannelHandler(connection))).isEmpty();
    assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty();

    connection.close();
  }
  @Test
  public void commandFailsDuringDecode() throws Exception {

    RedisCommands<String, String> connection = client.connect().sync();
    RedisChannelWriter<String, String> channelWriter =
        getRedisChannelHandler(connection).getChannelWriter();
    RedisCommands<String, String> verificationConnection = client.connect().sync();

    connection.set(key, "1");

    AsyncCommand<String, String, String> command =
        new AsyncCommand(
            new Command<>(
                CommandType.INCR, new StatusOutput<>(CODEC), new CommandArgs<>(CODEC).addKey(key)));

    channelWriter.write(command);

    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();
    assertThat(getException(command)).isInstanceOf(IllegalStateException.class);

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

    connection.close();
    verificationConnection.close();
  }
  @Test
  public void testTtlIsSet() throws InterruptedException {
    // given
    phoneNumberService.createPhoneNumber("Michael", "123-456");

    RedisDatastoreProvider provider = getProvider();
    RedisCommands<String, String> connection = provider.getConnection();

    // when
    String key = "PhoneNumber:Michael";
    Boolean exists = connection.exists(key);
    String value = connection.get(key);
    Long pttl = connection.pttl(key);

    // then
    assertTrue(exists);
    assertTrue(value.length() > 10);
    assertTrue(pttl.longValue() > 3500);
  }
Example #11
0
  @Test
  public void commandIsExecutedOnce() throws Exception {

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

    connection.set(key, "1");
    connection.incr(key);
    assertThat(connection.get(key)).isEqualTo("2");

    connection.incr(key);
    assertThat(connection.get(key)).isEqualTo("3");

    connection.incr(key);
    assertThat(connection.get(key)).isEqualTo("4");

    connection.close();
  }
Example #12
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();
  }
Example #13
0
  @Test
  public void commandRetriedChannelClosesWhileFlush() throws Exception {

    assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2"));

    RedisCommands<String, String> connection = client.connect().sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    RedisChannelWriter<String, String> channelWriter =
        getRedisChannelHandler(connection).getChannelWriter();

    connection.set(key, "1");
    assertThat(verificationConnection.get(key)).isEqualTo("1");

    final CountDownLatch block = new CountDownLatch(1);

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

    AsyncCommand<String, String, Object> command = getBlockOnEncodeCommand(block);

    channelWriter.write(command);

    connectionWatchdog.setReconnectSuspended(true);

    Channel channel = getChannel(getRedisChannelHandler(connection));
    channel.unsafe().disconnect(channel.newPromise());

    assertThat(channel.isOpen()).isFalse();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isFalse();
    block.countDown();
    assertThat(command.await(2, TimeUnit.SECONDS)).isFalse();

    connectionWatchdog.setReconnectSuspended(false);
    connectionWatchdog.scheduleReconnect();

    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();

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

    assertThat(getQueue(getRedisChannelHandler(connection))).isEmpty();
    assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty();

    connection.close();
    verificationConnection.close();
  }
Example #14
0
  @Test
  public void commandNotExecutedChannelClosesWhileFlush() throws Exception {

    RedisCommands<String, String> connection = client.connect().sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    RedisChannelWriter<String, String> channelWriter =
        getRedisChannelHandler(connection).getChannelWriter();

    connection.set(key, "1");
    assertThat(verificationConnection.get(key)).isEqualTo("1");

    final CountDownLatch block = new CountDownLatch(1);

    AsyncCommand<String, String, Object> command =
        new AsyncCommand<String, String, Object>(
            new Command<>(
                CommandType.INCR, new IntegerOutput(CODEC), new CommandArgs<>(CODEC).addKey(key))) {

          @Override
          public void encode(ByteBuf buf) {
            try {
              block.await();
            } catch (InterruptedException e) {
            }
            super.encode(buf);
          }
        };

    channelWriter.write(command);

    Channel channel = getChannel(getRedisChannelHandler(connection));
    channel.unsafe().disconnect(channel.newPromise());

    assertThat(channel.isOpen()).isFalse();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isFalse();
    block.countDown();
    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();

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

    assertThat(getQueue(getRedisChannelHandler(connection))).isEmpty();
    assertThat(getCommandBuffer(getRedisChannelHandler(connection))).isEmpty();

    connection.close();
  }
Example #15
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();
  }