Search in sources :

Example 11 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand 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 12 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand 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

AsyncCommand (io.lettuce.core.protocol.AsyncCommand)12 Test (org.junit.jupiter.api.Test)10 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)5 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)5 IntegerOutput (io.lettuce.core.output.IntegerOutput)3 ByteBuf (io.netty.buffer.ByteBuf)3 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)2 StatusOutput (io.lettuce.core.output.StatusOutput)2 Command (io.lettuce.core.protocol.Command)2 Tag (brave.Tag)1 Tracer (brave.Tracer)1 Tracing (brave.Tracing)1 MutableSpan (brave.handler.MutableSpan)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 RedisConnectionException (io.lettuce.core.RedisConnectionException)1 RedisURI (io.lettuce.core.RedisURI)1 TestSupport (io.lettuce.core.TestSupport)1 TransactionResult (io.lettuce.core.TransactionResult)1 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)1 CommandBatching (io.lettuce.core.dynamic.batch.CommandBatching)1