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());
}
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);
}
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);
}
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);
}
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();
}
Aggregations