Search in sources :

Example 1 with IntegerOutput

use of io.lettuce.core.output.IntegerOutput in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method commandFailsWhenFailOnEncode.

@Test
void commandFailsWhenFailOnEncode() {
    RedisCommands<String, String> connection = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection.getStatefulConnection());
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    connection.set(key, "1");
    AsyncCommand<String, String, String> working = new AsyncCommand<>(new Command<>(CommandType.INCR, new IntegerOutput(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).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(new Command<>(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);
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(ConnectionTestUtil.getStack(connection.getStatefulConnection())).isNotEmpty();
    connection.getStatefulConnection().close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) IntegerOutput(io.lettuce.core.output.IntegerOutput) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) ByteBuf(io.netty.buffer.ByteBuf) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 2 with IntegerOutput

use of io.lettuce.core.output.IntegerOutput 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 3 with IntegerOutput

use of io.lettuce.core.output.IntegerOutput 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

AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)3 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)3 IntegerOutput (io.lettuce.core.output.IntegerOutput)3 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)3 ByteBuf (io.netty.buffer.ByteBuf)3 Test (org.junit.jupiter.api.Test)3 Channel (io.netty.channel.Channel)1 CountDownLatch (java.util.concurrent.CountDownLatch)1