Search in sources :

Example 6 with RedisChannelWriter

use of io.lettuce.core.RedisChannelWriter in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method commandNotFailedChannelClosesWhileFlush.

@Test
void commandNotFailedChannelClosesWhileFlush() {
    assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2"));
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    RedisCommands<String, String> sync = connection.sync();
    sync.set(key, "1");
    assertThat(verificationConnection.get(key)).isEqualTo("1");
    final CountDownLatch block = new CountDownLatch(1);
    ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(connection);
    AsyncCommand<String, String, Object> command = getBlockOnEncodeCommand(block);
    channelWriter.write(command);
    connectionWatchdog.setReconnectSuspended(true);
    Channel channel = ConnectionTestUtil.getChannel(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();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isFalse();
    assertThat(verificationConnection.get(key)).isEqualTo("1");
    assertThat(ConnectionTestUtil.getStack(connection)).isEmpty();
    assertThat(ConnectionTestUtil.getCommandBuffer(connection)).isNotEmpty().contains(command);
    connection.close();
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) Channel(io.netty.channel.Channel) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 7 with RedisChannelWriter

use of io.lettuce.core.RedisChannelWriter in project lettuce-core by lettuce-io.

the class AtMostOnceTest method commandNotExecutedChannelClosesWhileFlush.

@Test
void commandNotExecutedChannelClosesWhileFlush() {
    assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2"));
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = connection.sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    sync.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(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key))) {

        @Override
        public void encode(ByteBuf buf) {
            try {
                block.await();
            } catch (InterruptedException e) {
            }
            super.encode(buf);
        }
    };
    channelWriter.write(command);
    Channel channel = ConnectionTestUtil.getChannel(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(getStack(connection)).isEmpty();
    assertThat(getCommandBuffer(connection)).isEmpty();
    connection.close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Channel(io.netty.channel.Channel) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) IntegerOutput(io.lettuce.core.output.IntegerOutput) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 8 with RedisChannelWriter

use of io.lettuce.core.RedisChannelWriter in project lettuce-core by lettuce-io.

the class AtMostOnceTest method commandNotExecutedFailsOnEncode.

@Test
void commandNotExecutedFailsOnEncode() {
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    sync.set(key, "1");
    AsyncCommand<String, String, String> working = new AsyncCommand<>(new Command<String, String, String>(CommandType.INCR, new IntegerOutput(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key)));
    channelWriter.write(working);
    assertThat(working.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(sync.get(key)).isEqualTo("2");
    AsyncCommand<String, String, Object> command = new AsyncCommand<String, String, Object>(new Command<String, String, Object>(CommandType.INCR, new IntegerOutput(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).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(getException(command)).isInstanceOf(EncoderException.class);
    Wait.untilTrue(() -> !ConnectionTestUtil.getStack(connection).isEmpty()).waitOrTimeout();
    assertThat(ConnectionTestUtil.getStack(connection)).isNotEmpty();
    ConnectionTestUtil.getStack(connection).clear();
    assertThat(sync.get(key)).isEqualTo("2");
    assertThat(ConnectionTestUtil.getStack(connection)).isEmpty();
    assertThat(ConnectionTestUtil.getCommandBuffer(connection)).isEmpty();
    connection.close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) ByteBuf(io.netty.buffer.ByteBuf) IntegerOutput(io.lettuce.core.output.IntegerOutput) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Aggregations

RedisChannelWriter (io.lettuce.core.RedisChannelWriter)8 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)7 Test (org.junit.jupiter.api.Test)7 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)5 IntegerOutput (io.lettuce.core.output.IntegerOutput)3 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 StatusOutput (io.lettuce.core.output.StatusOutput)2 ConnectionWatchdog (io.lettuce.core.protocol.ConnectionWatchdog)2 RedisChannelHandler (io.lettuce.core.RedisChannelHandler)1 RedisException (io.lettuce.core.RedisException)1 Intent (io.lettuce.core.cluster.ClusterConnectionProvider.Intent)1 DefaultEndpoint (io.lettuce.core.protocol.DefaultEndpoint)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1