Search in sources :

Example 1 with StatusOutput

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

the class CommandHandlerUnitTests method shouldCallPolicyToDiscardReadBytes.

@Test
void shouldCallPolicyToDiscardReadBytes() throws Exception {
    DecodeBufferPolicy policy = mock(DecodeBufferPolicy.class);
    CommandHandler commandHandler = new CommandHandler(ClientOptions.builder().decodeBufferPolicy(policy).build(), clientResources, endpoint);
    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    channelPromise.setSuccess();
    commandHandler.channelRegistered(context);
    commandHandler.channelActive(context);
    commandHandler.getStack().add(new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8)));
    ByteBuf msg = context.alloc().buffer(100);
    msg.writeBytes("*1\r\n+OK\r\n".getBytes());
    commandHandler.channelRead(context, msg);
    commandHandler.channelUnregistered(context);
    verify(policy).afterCommandDecoded(any());
}
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 2 with StatusOutput

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

the class CommandHandlerUnitTests method shouldNotDiscardReadBytes.

@Test
void shouldNotDiscardReadBytes() 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)));
    // 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());
    sut.channelRead(context, msg);
    assertThat(internalBuffer.readerIndex()).isEqualTo(9);
    assertThat(internalBuffer.writerIndex()).isEqualTo(9);
    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 3 with StatusOutput

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

the class DefaultEndpointUnitTests method shouldWrapActivationCommands.

@Test
void shouldWrapActivationCommands() {
    when(channel.isActive()).thenReturn(true);
    doAnswer(i -> {
        sut.write(new Command<>(CommandType.AUTH, new StatusOutput<>(StringCodec.UTF8)));
        sut.write(Collections.singletonList(new Command<>(CommandType.SELECT, new StatusOutput<>(StringCodec.UTF8))));
        return null;
    }).when(connectionFacade).activated();
    sut.notifyChannelActive(channel);
    DefaultChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    when(channel.writeAndFlush(any())).thenAnswer(invocation -> {
        if (invocation.getArguments()[0] instanceof RedisCommand) {
            queue.add((RedisCommand) invocation.getArguments()[0]);
        }
        if (invocation.getArguments()[0] instanceof Collection) {
            queue.addAll((Collection) invocation.getArguments()[0]);
        }
        return promise;
    });
    assertThat(queue).hasSize(2).hasOnlyElementsOfTypes(DefaultEndpoint.ActivationCommand.class);
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) StatusOutput(io.lettuce.core.output.StatusOutput) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 4 with StatusOutput

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

the class DefaultEndpointUnitTests method shouldNotReplayActivationCommands.

@Test
void shouldNotReplayActivationCommands() {
    when(channel.isActive()).thenReturn(true);
    ConnectionTestUtil.getDisconnectedBuffer(sut).add(new DefaultEndpoint.ActivationCommand<>(new Command<>(CommandType.SELECT, new StatusOutput<>(StringCodec.UTF8))));
    ConnectionTestUtil.getDisconnectedBuffer(sut).add(new LatencyMeteredCommand<>(new DefaultEndpoint.ActivationCommand<>(new Command<>(CommandType.SUBSCRIBE, new StatusOutput<>(StringCodec.UTF8)))));
    doAnswer(i -> {
        sut.write(new Command<>(CommandType.AUTH, new StatusOutput<>(StringCodec.UTF8)));
        return null;
    }).when(connectionFacade).activated();
    sut.notifyChannelActive(channel);
    DefaultChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    when(channel.writeAndFlush(any())).thenAnswer(invocation -> {
        if (invocation.getArguments()[0] instanceof RedisCommand) {
            queue.add((RedisCommand) invocation.getArguments()[0]);
        }
        if (invocation.getArguments()[0] instanceof Collection) {
            queue.addAll((Collection) invocation.getArguments()[0]);
        }
        return promise;
    });
    assertThat(queue).hasSize(1).extracting(RedisCommand::getType).containsOnly(CommandType.AUTH);
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) StatusOutput(io.lettuce.core.output.StatusOutput) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 5 with StatusOutput

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

the class AtMostOnceTest method commandFailsDuringDecode.

@Test
void commandFailsDuringDecode() {
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = connection.sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    sync.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(getException(command)).isInstanceOf(UnsupportedOperationException.class);
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(sync.get(key)).isEqualTo("2");
    connection.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