Search in sources :

Example 6 with StatusOutput

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

the class ClientOptionsIntegrationTests method authenticatedPingBeforeConnectWithQueuedCommandsAndReconnect.

@Test
void authenticatedPingBeforeConnectWithQueuedCommandsAndReconnect() {
    WithPassword.run(client, () -> {
        RedisURI redisURI = RedisURI.Builder.redis(host, port).withPassword(passwd).withDatabase(5).build();
        StatefulRedisConnection<String, String> controlConnection = client.connect(redisURI);
        StatefulRedisConnection<String, String> redisConnection = client.connect(redisURI);
        redisConnection.async().set("key1", "value1");
        redisConnection.async().set("key2", "value2");
        RedisFuture<String> sleep = (RedisFuture<String>) controlConnection.dispatch(new AsyncCommand<>(new Command<>(CommandType.DEBUG, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).add("SLEEP").add(2))));
        sleep.await(100, TimeUnit.MILLISECONDS);
        Channel channel = getChannel(redisConnection);
        ConnectionWatchdog connectionWatchdog = getConnectionWatchdog(redisConnection);
        connectionWatchdog.setReconnectSuspended(true);
        TestFutures.awaitOrTimeout(channel.close());
        TestFutures.awaitOrTimeout(sleep);
        redisConnection.async().get(key).cancel(true);
        RedisFuture<String> getFuture1 = redisConnection.async().get("key1");
        RedisFuture<String> getFuture2 = redisConnection.async().get("key2");
        getFuture1.await(100, TimeUnit.MILLISECONDS);
        connectionWatchdog.setReconnectSuspended(false);
        connectionWatchdog.scheduleReconnect();
        assertThat(TestFutures.getOrTimeout(getFuture1)).isEqualTo("value1");
        assertThat(TestFutures.getOrTimeout(getFuture2)).isEqualTo("value2");
        controlConnection.close();
        redisConnection.close();
    });
}
Also used : ConnectionTestUtil.getConnectionWatchdog(io.lettuce.test.ConnectionTestUtil.getConnectionWatchdog) ConnectionTestUtil.getChannel(io.lettuce.test.ConnectionTestUtil.getChannel) Channel(io.netty.channel.Channel) StatusOutput(io.lettuce.core.output.StatusOutput) Test(org.junit.jupiter.api.Test)

Example 7 with StatusOutput

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

the class ClientOptionsIntegrationTests method requestQueueSizeOvercommittedReconnect.

@Test
void requestQueueSizeOvercommittedReconnect() {
    client.setOptions(ClientOptions.builder().requestQueueSize(10).build());
    StatefulRedisConnection<String, String> connection = client.connect();
    ConnectionWatchdog watchdog = getConnectionWatchdog(connection);
    watchdog.setListenOnChannelInactive(false);
    Queue<Object> buffer = getStack(connection);
    List<RedisFuture<String>> pings = new ArrayList<>();
    for (int i = 0; i < 11; i++) {
        AsyncCommand<String, String, String> command = new AsyncCommand<>(new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8)));
        pings.add(command);
        buffer.add(command);
    }
    getChannel(connection).disconnect();
    Wait.untilTrue(() -> !connection.isOpen()).waitOrTimeout();
    watchdog.setListenOnChannelInactive(true);
    watchdog.scheduleReconnect();
    for (int i = 0; i < 10; i++) {
        assertThat(TestFutures.getOrTimeout(pings.get(i))).isEqualTo("PONG");
    }
    assertThatThrownBy(() -> TestFutures.awaitOrTimeout(pings.get(10))).hasCauseInstanceOf(IllegalStateException.class).hasMessage("java.lang.IllegalStateException: Queue full");
    connection.close();
}
Also used : ArrayList(java.util.ArrayList) StatusOutput(io.lettuce.core.output.StatusOutput) ConnectionTestUtil.getConnectionWatchdog(io.lettuce.test.ConnectionTestUtil.getConnectionWatchdog) Test(org.junit.jupiter.api.Test)

Example 8 with StatusOutput

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

the class ClientOptionsIntegrationTests method pingBeforeConnectWithQueuedCommandsAndReconnect.

@Test
void pingBeforeConnectWithQueuedCommandsAndReconnect() throws Exception {
    StatefulRedisConnection<String, String> controlConnection = client.connect();
    StatefulRedisConnection<String, String> redisConnection = client.connect(RedisURI.create("redis://localhost:6479/5"));
    redisConnection.async().set("key1", "value1");
    redisConnection.async().set("key2", "value2");
    RedisFuture<String> sleep = (RedisFuture<String>) controlConnection.dispatch(new AsyncCommand<>(new Command<>(CommandType.DEBUG, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).add("SLEEP").add(2))));
    sleep.await(100, TimeUnit.MILLISECONDS);
    Channel channel = getChannel(redisConnection);
    ConnectionWatchdog connectionWatchdog = getConnectionWatchdog(redisConnection);
    connectionWatchdog.setReconnectSuspended(true);
    TestFutures.awaitOrTimeout(channel.close());
    TestFutures.awaitOrTimeout(sleep);
    redisConnection.async().get(key).cancel(true);
    RedisFuture<String> getFuture1 = redisConnection.async().get("key1");
    RedisFuture<String> getFuture2 = redisConnection.async().get("key2");
    getFuture1.await(100, TimeUnit.MILLISECONDS);
    connectionWatchdog.setReconnectSuspended(false);
    connectionWatchdog.scheduleReconnect();
    assertThat(TestFutures.getOrTimeout(getFuture1)).isEqualTo("value1");
    assertThat(TestFutures.getOrTimeout(getFuture2)).isEqualTo("value2");
    controlConnection.close();
    redisConnection.close();
}
Also used : ConnectionTestUtil.getConnectionWatchdog(io.lettuce.test.ConnectionTestUtil.getConnectionWatchdog) ConnectionTestUtil.getChannel(io.lettuce.test.ConnectionTestUtil.getChannel) Channel(io.netty.channel.Channel) StatusOutput(io.lettuce.core.output.StatusOutput) Test(org.junit.jupiter.api.Test)

Example 9 with StatusOutput

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

the class CommandHandlerUnitTests method shouldDiscardReadBytes.

@Test
void shouldDiscardReadBytes() throws Exception {
    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    channelPromise.setSuccess();
    sut.channelRegistered(context);
    sut.channelActive(context);
    sut.getStack().add(new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8)));
    sut.getStack().add(new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8)));
    sut.getStack().add(new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8)));
    // set the command handler buffer capacity to 30, make it easy to test
    ByteBuf internalBuffer = context.alloc().buffer(30);
    sut.setBuffer(internalBuffer);
    // mock a multi reply, which will reach the buffer usage ratio
    ByteBuf msg = context.alloc().buffer(100);
    msg.writeBytes("*1\r\n+OK\r\n".getBytes());
    msg.writeBytes("*1\r\n+OK\r\n".getBytes());
    msg.writeBytes("*1\r\n+OK\r\n".getBytes());
    sut.channelRead(context, msg);
    assertThat(internalBuffer.readerIndex()).isEqualTo(0);
    assertThat(internalBuffer.writerIndex()).isEqualTo(0);
    sut.channelUnregistered(context);
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) StatusOutput(io.lettuce.core.output.StatusOutput) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 10 with StatusOutput

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

the class AtLeastOnceTest method commandFailsDuringDecode.

@Test
void commandFailsDuringDecode() {
    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> command = new AsyncCommand(new Command<>(CommandType.INCR, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key)));
    channelWriter.write(command);
    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();
    assertThat(getException(command)).isInstanceOf(UnsupportedOperationException.class);
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(connection.get(key)).isEqualTo("2");
    connection.getStatefulConnection().close();
    verificationConnection.getStatefulConnection().close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) StatusOutput(io.lettuce.core.output.StatusOutput) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Aggregations

StatusOutput (io.lettuce.core.output.StatusOutput)10 Test (org.junit.jupiter.api.Test)10 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)5 ConnectionTestUtil.getConnectionWatchdog (io.lettuce.test.ConnectionTestUtil.getConnectionWatchdog)3 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelPromise (io.netty.channel.ChannelPromise)3 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)2 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)2 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)2 ConnectionTestUtil.getChannel (io.lettuce.test.ConnectionTestUtil.getChannel)2 Channel (io.netty.channel.Channel)2 Collection (java.util.Collection)2 ArrayList (java.util.ArrayList)1